Week 7
Out, Out I Say!: Output Devices
Topics Covered:
  1. How To
    • Implement Output Devices
    • Integrate Input and Output Devices On Microcontrollers
  2. Explorations
    • Use New Output Devices: LED Strips
    • Write a Microcontroller Program with Input and Output


The assignment:
  1. Use an output device that you haven't used before today.
  2. Write a microcontroller program that integrates at least one input device and one output device. Avoid the delay() function and use the C++ class structure.
  3. Use an oscilloscope to discover the time domain at which your output device is operating. Is it on a fixed clock? What's its speed? Share images and describe your findings.
  4. Prepare CAD files for CNC (next class). Consider either 2D DXF files for routing sheet material (like plywood or OSB), or 3D STL files to mill out a (2.5D) shape. We will also cover molding & casting, so you may want to consider milling a mold.

1. Use New Output Devices: LED Strips

Code:

        #include 

          #define LED_PIN   9   //The pin connected to the NeoPixels.
          #define LED_COUNT 10  //The number of NeoPixels attached.
          #define DELAY_VAL 300
          
          //Declare our NeoPixel strip object:
          //Arg 1: # of pixels; Arg 2: Arduino pin #; Arg 3: pixel type flags, add together as needed
          //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
          //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
          //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
          //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
          //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
          
          Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
          
          void setup() {
            // put your setup code here, to run once:
            strip.begin();
            strip.show(); //Initialize all pixels to off.
            strip.setBrightness(20);
          }
          
          void loop() {
            // put your main code here, to run repeatedly:
            strip.fill(strip.Color(255,0,255,50));
            strip.show();
            delay(100);
          
          for(int i=0; i < LED_COUNT; i++) {
              strip.setPixelColor(i, 0, min(20*i, 255),0);
              strip.show();
              delay(DELAY_VAL);
              strip.setPixelColor(i, min(20*i, 255),0,0);
              strip.show();
              delay(DELAY_VAL);
            }
          }
      

Pixel Strip:



Code:

        #include 

          #define LED_PIN   9   //The pin connected to the NeoPixels.
          #define LED_COUNT 10  //The number of NeoPixels attached.
          #define DELAY_VAL 300
          
          //Declare our NeoPixel strip object:
          //Arg 1: # of pixels; Arg 2: Arduino pin #; Arg 3: pixel type flags, add together as needed
          
          Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
          
          void setup() {
            strip.begin();
            strip.show(); //Initialize all pixels to off.
            strip.setBrightness(12);
          }
          
          void loop() {
            strip.clear();
            for(int i=0; i < LED_COUNT; i++) {
                strip.setPixelColor(i, min(50+10*i,255),min(255-20*i,255),min(20+15*i,255));
                strip.show();
                delay(DELAY_VAL);
            }
          }
      

Pixel Strip:



Code:
 
        #include 

          #define LED_PIN   9   //The pin connected to the NeoPixels.
          #define LED_COUNT 10  //The number of NeoPixels attached.
          #define DELAY_VAL 300
          
          //Declare our NeoPixel strip object:
          //Arg 1: # of pixels; Arg 2: Arduino pin #; Arg 3: pixel type flags, add together as needed
          
          Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
          
          void setup() {
            strip.begin();
            strip.show(); //Initialize all pixels to off.
            strip.setBrightness(12);
          }
          
          void loop() {
            for(int i=0; i < LED_COUNT; i++) {
              strip.rainbow(65535-(i*6553),1,255,255,true); //Replace the first value with i*6553 to get the spectrum moving down the strip rather than up the strip.
              strip.show();
              delay(100);
            }
          }
      

Pixel Strip:




2. Write a Microcontroller Program with an Input AND Output
The following code uses the MEMS Mic to take audio as an input, and uses a relay and pump to make the flow of water the output.

When any reasonably loud noise is being made, the current will flow! And at an ambient room-level of noise, the current will not flow.


        /*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-relay-module-ac-web-server/
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/
const int relay = 40;

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
 unsigned int sample;

 void setup() 
 {
    Serial.begin(9600);
    pinMode(relay, OUTPUT); //set relay pin as an OUTPUT
 }


 void loop() 
 {
    unsigned long startMillis= millis();  // Start of sample window
    unsigned int peakToPeak = 0;   // peak-to-peak level

    unsigned int signalMax = 0;
    unsigned int signalMin = 1024;

    // collect data for 50 mS
    while (millis() - startMillis < sampleWindow)
    {
       sample = analogRead(0);   //reading DC pin from pin A0
       if (sample < 1024)  // toss out spurious readings
       {
          if (sample > signalMax)
          {
             signalMax = sample;  // save just the max levels
          }
          else if (sample < signalMin)
          {
             signalMin = sample;  // save just the min levels
          }
       }
    }
    peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude

    Serial.println(peakToPeak);
    
    //If there's some noise, let the current flow.
    if (peakToPeak > 20) {
      digitalWrite(relay, LOW);
      Serial.println("Current Flowing");
      delay(5000); 
    }
    //If there's no loud noises, don't let the current flow.
    else {
      digitalWrite(relay, HIGH);
      Serial.println("Current not Flowing");
      delay(1000);
    }
 }
      
  • Download my Arduino Code