Saturday, November 20, 2021

Failing Forward with Arduino Coding

In this week's Arduino challenge, we were asked to tackle Practice Circuits #3 and #4.  Circuit #3 involved programming the RGB light which is an LED light that can make multiple colors when coded correctly.  I had always wondered how Christmas lights could be multiple colors in one bulb; at least now I have some idea.  When I showed my husband the RGB circuit and colorful light, he asked two questions:   1) What class are you taking that you have to do that? 2)  When are you going to program our Christmas lights?  (Ha-ha - I think I need a little more practice). Circuit #4 was dancing LEDs.  This was fun, once I figured out how to make get everything set up and the code running correctly.  Although I am not ready to tackle coding our Christmas light display to music, I am gaining a better understanding of how it could be done.

RGB LED

In the RGB coding the clear LED will light up multiple colors based on the coding of high or low for the blue, red, and green pins.  It's all about combing the colors to get the desired color.  On my first attempt, the code was correct as I could tell by the blinking light on the Arduino, but my RGB wasn't working.


When I realized that the code was running, but my light wasn't changing colors, I started over and reset everything back up.  I believe I had the center wire in the wrong place, but I didn't take a before and after picture.  I did, however, take a video of the working program.




Here are some pictures of the board and the working code:

const int RED_PIN = 9;

const int GREEN_PIN = 10;

const int BLUE_PIN = 11;


int DISPLAY_TIME = 100;  


void setup() {

 

  pinMode(RED_PIN, OUTPUT);

  pinMode(GREEN_PIN, OUTPUT);

  pinMode(BLUE_PIN, OUTPUT); }


void loop() {

  

  mainColors();


showSpectrum(); }


void mainColors() {


  digitalWrite(RED_PIN, LOW);

  digitalWrite(GREEN_PIN, LOW);

  digitalWrite(BLUE_PIN, LOW);


  delay(1000);


  digitalWrite(RED_PIN, HIGH);

  digitalWrite(GREEN_PIN, LOW);

  digitalWrite(BLUE_PIN, LOW);


  delay(1000);


  digitalWrite(RED_PIN, LOW);

  


digitalWrite(GREEN_PIN, HIGH);

  digitalWrite(BLUE_PIN, LOW);


  delay(1000);


  digitalWrite(RED_PIN, LOW);

  digitalWrite(GREEN_PIN, LOW);

  digitalWrite(BLUE_PIN, HIGH);


   delay(1000);


  digitalWrite(RED_PIN, HIGH);

  digitalWrite(GREEN_PIN, HIGH);

  digitalWrite(BLUE_PIN, LOW);


  delay(1000);


  digitalWrite(RED_PIN, LOW);

  digitalWrite(GREEN_PIN, HIGH);

  digitalWrite(BLUE_PIN, HIGH);


  delay(1000);


  digitalWrite(RED_PIN, HIGH);

  digitalWrite(GREEN_PIN, LOW);

  digitalWrite(BLUE_PIN, HIGH);


  delay(1000);


  digitalWrite(RED_PIN, HIGH);

  digitalWrite(GREEN_PIN, HIGH);

  digitalWrite(BLUE_PIN, HIGH);


  delay(1000); }


void showSpectrum() {

  int x;  // define an integer variable called "x"


  for (x = 0; x < 768; x++)  {

    showRGB(x);  // Call RGBspectrum() with our new x

    delay(10);   // Delay for 10 ms (1/100th of a second)  } }


void showRGB(int color) {

  int redIntensity;

  int greenIntensity;

  int blueIntensity;


  if (color <= 255)  {

    redIntensity = 255 - color;   

    greenIntensity = color;        

    blueIntensity = 0;  }

  else if (color <= 511) {

    redIntensity = 0;                     

    greenIntensity = 255 - (color - 256); 

    blueIntensity = (color - 256); }

else // color >= 512 {

    redIntensity = (color - 512);         

    greenIntensity = 0;                   

    blueIntensity = 255 - (color - 512); }

  

  analogWrite(RED_PIN, redIntensity);

  analogWrite(BLUE_PIN, blueIntensity);

  analogWrite(GREEN_PIN, greenIntensity); }


Dancing Lights


Our second programmed circuit for the week was to program a row of lights to blink in different combinations.  To make this variety happen there was a lot of coding, that involved the speed of the lights and where the blinking started and finished.  Below is a video of dancing Arduino lights.  If only I could make my Christmas lights do this to music.  I can't even imagine the amount of programming that takes.  


In this coding sequence, the lights are numbered according to their pin location.  This is what programs the lights when to be on or off. Each LED needs a jumper wire and a resistor.  The jumper wires connect to the digital pins.  Mine does not look as pretty as the drawing, but it worked on the first try.  YAY!




I am proud of myself for getting the circuits built and coding them to work.  I am slowly beginning to understand how things work, but I still have a lot to learn.  The learning curve is huge, but I am building a lot of dendrites as  I tackle each week's challenge.

Code for Dancing Lights.

int ledPins[] = {2,3,4,5,6,7,8,9};


void setup() {

  int index;

  for(index = 0; index <= 7; index++); {

    pinMode(ledPins[index],OUTPUT) }

} void loop()

{

//oneAfterAnotherNoLoop();  // Light up all the LEDs in turn  //oneAfterAnotherLoop();  // Same as oneAfterAnotherNoLoop,

                      // but with much less typing

   //oneOnAtATime();         // Turn on one LED at a time,

                             // scrolling down the lin

  //pingPong();             // Light the LEDs middle to the edges

  marquee();              // Chase lights like you see on signs

//randomLED();            // Blink LEDs randomly

} void oneAfterAnotherNoLoop() {

  int delayTime = 100; // time (milliseconds) to pause between LEDs

                       // make this smaller for faster switching

 // turn all the LEDs on:


  digitalWrite(ledPins[0], HIGH);  //Turns on LED #0 (pin 2)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[1], HIGH);  //Turns on LED #1 (pin 3)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[2], HIGH);  //Turns on LED #2 (pin 4)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[3], HIGH);  //Turns on LED #3 (pin 5)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[4], HIGH);  //Turns on LED #4 (pin 6)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[5], HIGH);  //Turns on LED #5 (pin 7)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[6], HIGH);  //Turns on LED #6 (pin 8)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[7], HIGH);  //Turns on LED #7 (pin 9)

  delay(delayTime);                //wait delayTime milliseconds  

 

  // turn all the LEDs off:

  

  digitalWrite(ledPins[7], LOW);   //Turn off LED #7 (pin 9)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[6], LOW);   //Turn off LED #6 (pin 8)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[5], LOW);   //Turn off LED #5 (pin 7)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[4], LOW);   //Turn off LED #4 (pin 6)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[3], LOW);   //Turn off LED #3 (pin 5)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[2], LOW);   //Turn off LED #2 (pin 4)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[1], LOW);   //Turn off LED #1 (pin 3)

  delay(delayTime);                //wait delayTime milliseconds

  digitalWrite(ledPins[0], LOW);   //Turn off LED #0 (pin 2)

  delay(delayTime);                //wait delayTime milliseconds  

}

void oneAfterAnotherLoop()

{

  int index;

  int delayTime = 100; // milliseconds to pause between LEDs

                       // make this smaller for faster switching

  for(index = 0; index <= 7; index++)

{

    digitalWrite(ledPins[index], HIGH);

    delay(delayTime);                }                                  

  for(index = 7; index >= 0; index--)

 {

    digitalWrite(ledPins[index], LOW);

    delay(delayTime);

  }               

}

void oneOnAtATime()

{

  int index;

  int delayTime = 100; // milliseconds to pause between LEDs

                       // make this smaller for faster switching

   // step through the LEDs, from 0 to 7

  for(index = 0; index <= 7; index++)

  {

    digitalWrite(ledPins[index], HIGH);  // turn LED on

    delay(delayTime);                    // pause to slow down

    digitalWrite(ledPins[index], LOW);   // turn LED off

  }

}

void pingPong()

{

  int index;

  int delayTime = 100; // milliseconds to pause between LEDs

                       // make this smaller for faster switching

  // step through the LEDs, from 0 to 7


  for(index = 0; index <= 7; index++)

  {

    digitalWrite(ledPins[index], HIGH);  // turn LED on

    delay(delayTime);                    // pause to slow down

    digitalWrite(ledPins[index], LOW);   // turn LED off

  }


  // step through the LEDs, from 7 to 0

  

  for(index = 7; index >= 0; index--)

  {

    digitalWrite(ledPins[index], HIGH);  // turn LED on

    delay(delayTime);                    // pause to slow down

    digitalWrite(ledPins[index], LOW);   // turn LED off

  }

}

void marquee()

{

  int index;

  int delayTime = 200; // milliseconds to pause between LEDs

                       // Make this smaller for faster switching

  // Step through the first four LEDs

  // (We'll light up one in the lower 4 and one in the upper 4)

  for(index = 0; index <= 3; index++) // Step from 0 to 3

  {

    digitalWrite(ledPins[index], HIGH);    // Turn a LED on

    digitalWrite(ledPins[index+4], HIGH);  // Skip four, and turn that LED on

    delay(delayTime);                      // Pause to slow down the sequence

    digitalWrite(ledPins[index], LOW);     // Turn the LED off

    digitalWrite(ledPins[index+4], LOW);   // Skip four, and turn that LED off

  }

}

void randomLED()

{

  int index;

  int delayTime;

  index = random(8); // pick a random number between 0 and 7

  delayTime = 100;

  digitalWrite(ledPins[index], HIGH);  // turn LED on

  delay(delayTime);                    // pause to slow down

  digitalWrite(ledPins[index], LOW);   // turn LED off

}


Week 3 Challenge - Combining what we've learned


For this challenge, I chose to combine the blinking LED lights with the pentiometer.  I had success and would like to play with it more by additional code for the light pattern.  



For this challenge, I understand how the blinking lights work and how the potentiometer controls the speed.  I wanted to also use the RGB, but I couldn't figure out how to get that to work.  I thought I understood the RGB.  The longest lead in the RGB needs to be connected to the ground.  I couldn't think how to set it up and code it.  Perhaps I will try again next week on or solo build.  

As I reflect on my learning this week, I am reminded of students who do well enough to get by in class, but yet might not have a thorough understanding.  I am slowly understanding more, yet there are still things I am confused about.  I am able to complete my assignments, but I don't always feel confident that what I have done is correct.  As an adult, I am pushing through and acknowledging what I know and what I need help with, but do our students do this, or do they just accept defeat and give up on the possibility of understanding fully.

Points to ponder for sure.

Below is my code for the combined circuit challenge.

int ledNum = 6;

int ledPin[] = {4, 5, 6, 7, 8, 9};

int delayTime = 500;

int potPin = A0;


void setup() 

{

for (int i = 0; i < 6; i++) {

    pinMode (ledPin[i], OUTPUT);

  }

}

void loop() {

  for (int i = 0; i < 6; i++) {

    delayTime = analogRead(potPin);

    digitalWrite(ledPin[i], HIGH);

    delay(delayTime);

    digitalWrite(ledPin[i], LOW);

  }

  for (int i = 5; i > 1; i--) {

    delayTime = analogRead(potPin);

    digitalWrite(ledPin[i], HIGH);

    delay(delayTime);

    digitalWrite(ledPin[i], LOW);

}

}

No comments:

Post a Comment