Go Down

Topic: how to control 10 leds (Read 195 times) previous topic - next topic

samanshams

Hi
I have 10 leds  i want to turn them off and on by time which i can adjust 

I wrote this program for 5 leds but is there any easier and simple program that i can use for 10 leds

by the way I am using arduino mega

this what i wrote

int led = 22;
int led1 = 24;
int led2 = 26;
int led3 = 28;
int led4 = 52;
// the setup routine runs once when you press reset:
void setup() {
   // initialize the digital pin as an output.
   pinMode(led, OUTPUT);
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   pinMode(led3, OUTPUT);
   pinMode(led4, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
   digitalWrite(led1, HIGH);
   digitalWrite(led2, HIGH);
   digitalWrite(led3, HIGH);
        digitalWrite(led4,HIGH);
   delay(2500);               // wait for a second
   digitalWrite(led, LOW);
        digitalWrite(led4,LOW);// turn the LED off by making the voltage LOW
   delay(1000);
        digitalWrite(led4,HIGH);
        delay(1000);
        digitalWrite (led1,LOW);
        digitalWrite(led4,LOW);
   delay(1000);
digitalWrite(led4,HIGH);
        delay(1000);
   digitalWrite (led2,LOW);
        digitalWrite(led4,LOW);
   delay(1000);
        digitalWrite (led3,LOW);
        digitalWrite(led4,HIGH);
        delay(5000);
   // wait for a second
}

thanks

AWOL

Quote
is there any easier and simple program that i can use for 10 leds
Yes, read up on arrays.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Paul__B

Check out this sketch which blinks a couple of LEDs at independent timings and includes a pushbutton debounce routine to switch something (a LED to start with) on and off.  You simply duplicate the relevant parts to add more and more blink functions.  It could be modified to use an array, but it is just as easy to have one function defined per LED so you can "tweak" them individually.  I may actually make a more "function-ised" version specifically for blinking on and off.

Code: [Select]
// Blink without "delay()" - multi!

const int led1Pin =  13;    // LED pin number
const int led2Pin =  10;
const int led3Pin =  11;
const int button1 =  4;
int led1State = LOW;        // initialise the LED
int led2State = LOW;
int led3State = LOW;
char bstate1 = 0;
unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) {
    *marker += interval;    // move on ready for next interval
    return true;       
  }
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far,
    if (button == HIGH) return false; // Nothing happening!
    else {
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far,
    if (button == LOW) return false; // Nothing happening!
    else {
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else {
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else {
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    { 
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);     
  pinMode(led2Pin, OUTPUT);     
  pinMode(led3Pin, OUTPUT);     
  pinMode(button1, INPUT);     
  digitalWrite(button1,HIGH);        // internal pullup all versions
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW;
    }
    digitalWrite(led1Pin, led1State);
  }

  // Act if the latter time (ms) has now passed on this particular counter,
  if (timeout(&count2, 300UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
    }
    else {
      led2State = LOW;
    }
    digitalWrite(led2Pin, led2State);
  }

  if (timeout(&count3, 77UL )) {
    if (led3State == LOW) {
      led3State = HIGH;
    }
    else {
      led3State = LOW;
    }
    digitalWrite(led3Pin, led3State);
  }
}

marco_c

#4
Oct 27, 2014, 11:55 pm Last Edit: Oct 27, 2014, 11:58 pm by marco_c
I have a sketch called MultiBlink in my libraries repository (link below) that allows user defined cycles for LEDs. It uses a data table to change the way the LED animates rather than changing the code. You may be interested either using it or seeing how it works.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

BulldogLowell

check out this recent thread, which demonstrates a few ways of manipulating an array of LEDs including a little bit shifting, using bitRead() on a pattern stored in a byte, flashing using BlinkWithoutDelay and a state machine.  It would be rewarding stuff to learn a few (all?) of the techniques that were used in this little example.  

samanshams

thanks everyone i don't have any push button
i just want to turn on and off leds one by one  by 10 sec and then turn all on and turn all off . like loading bar start from one led and then back


BulldogLowell

a spare jumper plugged into the pin header and touched against any ground is a fine substitute for a switch.

samanshams

i use this code
how can I change it

int pins [10] = { 22,24,26,28,30,32,34,36,38,40 };

void setup() {
 
  for(int i=0; i<10 ; i++) pinMode(pins, OUTPUT);
 
}

void loop () {
  for(int i=0; i<10; i++) digitalWrite(pins, OUTPUT);
 
}

BulldogLowell

#9
Oct 28, 2014, 01:05 am Last Edit: Oct 28, 2014, 01:08 am by BulldogLowell
you can start here:

Code: [Select]
int pins [10] = {22,24,26,28,30,32,34,36,38,40};

void setup()
{
  for(int i=0; i<10 ; i++)
  {
    pinMode(pins[i], OUTPUT);
  }
}

void loop ()
{
  for(int i=0; i<10; i++)
  {
    digitalWrite(pins[i], HIGH);   
  }
  delay(100);
  for(int i=0; i<10; i++)
  {
    digitalWrite(pins[i], LOW);   
  }
  delay(100);
}



samanshams

you can start here:

Code: [Select]
int pins [10] = {22,24,26,28,30,32,34,36,38,40};

void setup()
{
  for(int i=0; i<10 ; i++)
  {
    pinMode(pins[i], OUTPUT);
  }
}

void loop ()
{
  for(int i=0; i<10; i++)
  {
    digitalWrite(pins[i], HIGH);   
  }
  delay(100);
  for(int i=0; i<10; i++)
  {
    digitalWrite(pins[i], LOW);   
  }
  delay(100);
}



thanks I tried that code and it worked
how can i write code for turn on/off leds one by one .

BulldogLowell

here is one way:

Code: [Select]
const int pins [10] =
  22,24,26,28,30,32,34,36,38,40};
byte index = 0;
byte myDirection = 1;

void setup()
{
  Serial.begin(115200);
  for(int i=0; i<10 ; i++)
  {
    pinMode(pins[i], OUTPUT);
  }
}

void loop ()
{
  Serial.println(index);
  for(int i = 0; i < 10; i++)
  {
    digitalWrite(pins[i], (i == index? HIGH : LOW));   
  }
  delay(100);
  index += myDirection;
  if (index <= 0 || index >= 9)
  {
    myDirection = - myDirection;
  }
}

samanshams

here is one way:

Code: [Select]
const int pins [10] =
  22,24,26,28,30,32,34,36,38,40};
byte index = 0;
byte myDirection = 1;

void setup()
{
  Serial.begin(115200);
  for(int i=0; i<10 ; i++)
  {
    pinMode(pins[i], OUTPUT);
  }
}

void loop ()
{
  Serial.println(index);
  for(int i = 0; i < 10; i++)
  {
    digitalWrite(pins[i], (i == index? HIGH : LOW));   
  }
  delay(100);
  index += myDirection;
  if (index <= 0 || index >= 9)
  {
    myDirection = - myDirection;
  }
}

Can i ask you do you have any good source to help me for writing program ?


Go Up