Go Down

Topic: Re: noob here need help on 8 led sequence (Read 170 times) previous topic - next topic

Roadie3rd

Hi all
I hope someone can help i am very new to arduino and have yet to attempt this as i don't know where to start. I'm one of "those guys" who knows what they want to do but doesn't know how to do it. Sorry.

Here is what I'm trying to create:
I have 8 LED's in sequence that initially all 8 LED's are on (when power is connected)
When you press a switch/button all of the LED's will flash a few times then one on the end will turn off
when you click/push again all of the remaining LED's that are lit up will flash a few times then the next one on the end will turn off and so on till all 8 have turned off
then to reset the sequence either click again or double click/push

This is for a cosplay item for a friend and I don't know where to start, what i need or how to program this as i said I'm very new to all this and i think I've bitten off more than I can chew.
Can some one help or at least point me in the right direction on where to start
So sorry for this, any help is greatly appreciated

Many thanks in advance
Mark

dougp

Start by grasping the concepts in the first five demos in: IDE/file/examples/digital.  It's like building a house, you've got to have a foundation to put it on.

Break it into pieces.  Start with debouncing the button, then blink one LED, then make that LED flash when you press the button, etc.

Which Arduino is used?
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

SchnoppDog

Since I am a newbie too I can tell you what helped me.
As dougp asked: Which arduino is used? Would be a helpful Information.
Referring to arduino Boards like Uno. You should get to know the concepts of LEDs first. With that i mean how to programm them and how to interact with different parts like buttons.

You can find the commands for arduino here:
https://www.arduino.cc/reference/en/#page-title

You can find some useful information about LEDs right here:
https://learn.adafruit.com/all-about-leds/overview

Commands you might need for this project:
digitalRead(Pin_number, OUTPUT_or_INPUT)
analogRead(Pin_number, value)

And of course if you're not familiar with coding you maybe need:
if-Statement
while-Statement
for-Statement

Things you might need for your project:
An arduino (of course)
Breadboard
Cables (maybe jumper cables)
Resistors
LEDs

I hope this is a bit useful for you

maxim2511

#3
Nov 16, 2018, 08:39 pm Last Edit: Nov 16, 2018, 08:43 pm by maxim2511 Reason: Make mayor mistake in code...
Firts You need build testing circuit, like I did. I didn't include resistors in this circuit, But on Thinkercad, more important for me is first build code witch will work for me. Later I will tweek less important stuff...

atachment nr 1.

Code I will begin at defining pins ass outputs atached to diodes.
I will use array because is faster way to work with and is much easier to work with multiple data / pins...

Code: [Select]

short how_many_leds = 9;
const short arrayLedsPin[9]={ 2, 3, 4, 5, 6, 7, 8, 9, 10 };


Ok. Now I have group of Pins. Now second importand thing is to define pins as OUTPUT.

Code: [Select]

void setup()
{
  for ( short i = 0, i < how_many_leds, i++ ){
    pinMode( arrayLedsPin[i], OUTPUT );
  }
}




For loop it makes pin mode for me 9 times. Like You see, for loop is beatiful to use in code, its makes life so easy. Now arduino make hard work for You, and You need type only few comands to do 9 times this same thing.

Next thing is to begin with turning on leds. More new peoples in arduino comunity will do it all in loop() but
cleaner way is group all your mayor functions.
Lets sey .. turn on all leds on, 1 by 1 begins from 1 led.


For that is good to use function.
I chouse this one

Code: [Select]

void Leds_start(){

  for (short i = 0, i < how_many_leds, i++){
    digitalWrite(arrayLedsPin[i], HIGH);
    delay(1000); // 1 second for each led
  }
}




On thinkercad site You could check if Your code is working correctly, and If You didn't make any mistake. I did many times even now with more complicated codes.

https://www.tinkercad.com/things/1iodiLJDDDu-sizzling-bojo/editel?tenant=circuits?sharecode=ad1pr5ktql7fAq3QLrk2elcobW-9ugEhB2A6iRNwYoo=

Awter checking code and tested, I foun few mistakes. Like in code :smiley-confuse:

Code: [Select]
(short i = 0 -->, <-- i < how_many_leds --> , <-- i++)


it should be

Code: [Select]
(short i = 0 --> ; <-- i < how_many_leds --> ; <-- i++)



common mistakes even inter mediate peoples in code. :(
Ok, now I will post full code to check how is working testing code.
Also I atached resistors to leds because thinkercad show me that there is to high volgage for leds. :)

atachment nr 2.


Code: [Select]

short how_many_leds = 9;
const short arrayLedsPin[9]={2,3,4,5,6,7,8,9,10};

void Leds_start();

void setup()
{
  for (short i = 0; i < how_many_leds; i++){
    pinMode(arrayLedsPin[i], OUTPUT);
  }

  Leds_start(); // Your first function

}

void loop(){
}

//************************************************
void Leds_start(){
  for (short i = 0; i < how_many_leds; i++){
    digitalWrite(arrayLedsPin[i], HIGH);
    delay(1000);
  }
}



maxim2511

Second part is to add push button, connect it to pin and define this pin as input.

First in Your circuit You need connect push button. Attachment nr 1.

Now part of the coding.
I conected to the pin A0, first thing is to define this pin ass input to be able to read state of that pin.
Code: [Select]
pinMode(A0, INPUT_PULLUP);
INPUT_PULLUPP is important with switches, Otherwise Your pin will be on state LOW and You will be not able to read any signal on that input.
This line is included in setup() section.

Now I want turn off 1 led from the end of array. I will use next function.
Code: [Select]
void PressButton(){
}

and in side I need some testing code to check when i will bush this button and turn of 1 led from the end.
Code: [Select]
void PressButton(){
 
  if (digitalRead(A0) == LOW){
   
      digitalWrite(arrayLedsPin[0], LOW); // turn of the last led.
    }
}


Ps, array is beginning from 0, to 8, but total values are 9. You need remember that.

And next thing to do is call this function to be able checking my button all the time. This I need put in loop()
Code: [Select]
void loop(){
  PressButton();
}


Now run it to check if this code don't have any mistakes.

Total code looks like that:
Code: [Select]
short how_many_leds = 9;
const short arrayLedsPin[9]={2,3,4,5,6,7,8,9,10};

void Leds_start();

void setup()
{
  for (short i = 0; i < how_many_leds; i++){
    pinMode(arrayLedsPin[i], OUTPUT);
  }
  Leds_start(); // Your first function

  pinMode(A0, INPUT_PULLUP);
}

void loop(){
  PressButton();
}

//************************************************
void Leds_start(){
  for (short i = 0; i < how_many_leds; i++){
   
    digitalWrite(arrayLedsPin[i], HIGH);
   
    delay(1000);
  }
}


void PressButton(){
 
  if (digitalRead(A0) == LOW){
   
      digitalWrite(arrayLedsPin[0], LOW); // turn off the last led, 0 is the last one at pin 2.
    }
}

maxim2511

Next You need blink all leds few times. I choose 5 times.
I create new function for that. If You notice, I am using functions as puzzle, or more accurate word for that will be modules, where I can with short word do complicated thing. many times in many other places.
Code: [Select]
  void blinkAllLeds(){
    for (short i = 0; i < 5; i++){
        for (short i = 0; i < how_many_leds; i++){
          digitalWrite(arrayLedsPin[i], LOW);
        }
      delay(250);
      for (short i = 0; i < how_many_leds; i++){
          digitalWrite(arrayLedsPin[i], HIGH);
        }
      delay(250);
    }
  }


And call this function I putt in:
Code: [Select]
void PressButton(){
 
  if (digitalRead(A0) == LOW){
   
      blinkAllLeds(); // <---
   
      digitalWrite(arrayLedsPin[0], LOW);
    }
}

Now Your code looks more like You want to do. With few little steps, You can make 1 complicated things.
Next post will be  about how figure out how turn of next diode after previous one. like counting them.

maxim2511

Next step is to add counting turned off leds.
This will push You to make changes in Your code. Now You are sure that code is still working and You can change it.

In my case i did it in existing function button, because there i need use counting, and turning of more leds, not only one.

first I need new value.
Code: [Select]
short count = 0; Using short rather int will save some space in memory. I don't use values so big that I will waste unnecessary binary zeros.

Under the function blinkAllLeds I put counting value
Code: [Select]
short count = 0;
void PressButton(){
  if (digitalRead(A0) == LOW){
    blinkAllLeds();
   
    count = count +1;


and make for loop to count how many leds I need to turn it off.
Code: [Select]
for ( short nextLed = 0; nextLed < count; nextLed++){
      digitalWrite(arrayLedsPin[nextLed], LOW); // previouse was digitalWrite(arrayLedsPin[0], LOW);
    }


Did You notice how I change for loop for my needs? I add there extra name to be easier to identify what this for loop is duing.

Later is possible to move that changes to the new function and make Your code more cleaner.

My changes looks like that :
Code: [Select]
short count = 0;
void PressButton(){
  if (digitalRead(A0) == LOW){
    blinkAllLeds();
   
    count = count +1;
   
    for ( short nextLed = 0; nextLed < count; nextLed++){
      digitalWrite(arrayLedsPin[nextLed], LOW);
    }
   
   }
}

Did You also notice why I put short count = 0; straight before this function?
If Your program don't need use this value no where else, then for cleaner look of Your code and be more readable is allowed to do in that place.

maxim2511

At the end You need reset it by pushing button once.
Easier way is to create new function for that.

 I call it resetAll
Code: [Select]
void resetAll(){
}

Now of course You nedd reset counting. This is posible to use counting because is on global values avaible for all functions. To do that You should check if counting go to highest value
Code: [Select]
void resetAll(){
  if (count == 9){
    count = 0;
}

Question is to whwere put that function. You can do it at press button function, but I can do it also il loop function
Code: [Select]
void loop(){
  PressButton();
  resetAll();
}


After testing code You will notice that after last led everything is starting from beginning, but the last led will be turned of.
But You will need have all leds be turned on.
There are few solutions, but easier one will be use function created ealier. Leds_start();
function will look now like that:
Code: [Select]
void resetAll(){
  if (count == 9){
    count = 0;
    Leds_start();
  }
}


Now if You want turn off all leds before resenting everything, add higher value to counting.
Code: [Select]
void resetAll(){
  if (count == 10){
    count = 0;
    Leds_start();
  }
}

Now Your'e code should be finish.
If You want use double button reset everything, You should create special function with timing measurements to check if button was push 2 times in short period of time. But for beggining, try understand this one and play with it.

Full code looks like that:
Code: [Select]
short how_many_leds = 9;
const short arrayLedsPin[9]={2,3,4,5,6,7,8,9,10};

void Leds_start();

void setup()
{
  for (short i = 0; i < how_many_leds; i++){
    pinMode(arrayLedsPin[i], OUTPUT);
  }
  Leds_start(); // Your first function
  pinMode(A0, INPUT_PULLUP);
}

void loop(){
  PressButton();
  resetAll();
}

//************************************************
void Leds_start(){
  for (short i = 0; i < how_many_leds; i++){
    digitalWrite(arrayLedsPin[i], HIGH);
    delay(500);
  }
}
//************************************************
short count = 0;
void PressButton(){
  if (digitalRead(A0) == LOW){
    blinkAllLeds();
   
    count = count +1;
   
    for ( short nextLed = 0; nextLed < count; nextLed++){
      digitalWrite(arrayLedsPin[nextLed], LOW);
    }
   
   }
}
  //************************************************
  void blinkAllLeds(){
    for (short i = 0; i < 5; i++){
        for (short i = 0; i < how_many_leds; i++){
          digitalWrite(arrayLedsPin[i], LOW);
        }
      delay(250);
      for (short i = 0; i < how_many_leds; i++){
          digitalWrite(arrayLedsPin[i], HIGH);
        }
      delay(250);
    }
  }
//************************************************
void resetAll(){
  if (count == 10){
    count = 0;
    Leds_start();
  }
}

I hope that I did help You. Greatings.  :)

Go Up