Sequential Leds

So is it working now?

You still have

random(8,12)

in a few places, that may not be what you want.

a7

i modified to random(8, 12);

There are functions and variables that are there because I was modifying the code several times. but later I will eliminate the ones that are unnecessary

but it looks like you will probably turn an LED on that may already be on, as well as turning an LED off that may already be off.

i had the impression, because of the delays that you wanted to turn on and off the same LED. if you didn't want to do this and do what i describe above, why have the delays?

Please say if it works.

If it doesn't work please…

...say what it is doing that it shouldn't, or doesn't do that it should.

We I have no idea what you are trying to do, so be as clear and complete in your description of the desired behaviour.

That's the biggest problem you have, describing exactly what this should do.

a7

Bad habit to get into. At a minimum, comment out any and all functionality that is not required, then focus on what's left. Even better, delete unused bits, as they only obscure seeing the fundamental.

this the code with the solved problem of the delays.

// 2015/03/29
/*
  Demonstrates the use of an array to hold pin numbers.
  Each time the push button is pressed, next LED in the
  sequence will be turned on while the rest of LEDs are off.
*/
const int buttonPin = A1;
/*const int buttonpin2 = A2;*/ // the number of the pushbutton pin
const int ledPin1 = 8;   // the number of the LED pin
const int ledPin2 = 9;   // the number of the LED pin
const int ledPin3 = 10;   // the number of the LED pin
const int ledPin4 = 11;   // the number of the LED pin
const int ledPin5 = 12;   // the number of the LED pin
// variables will change:
int buttonState = 0;   // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 3;
int time = 2;

int Z = 0;


unsigned long newTime;
unsigned long oldTime = 0;
byte newButtonState;
byte oldButtonState = 0;
boolean ledState = false;
boolean blinkState = false;
boolean state = LOW;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}


void AA(){
  
      digitalWrite(random(8,13), HIGH);
      delay(100); // Wait for 100 millisecond(s)
      digitalWrite(random(8,13), LOW);
      delay(100);
  
}    
  
void BB(){ digitalWrite(random(8,13), LOW); delay(500); // Wait for 1000 millisecond(s) 
          for (int i=8; i<13; i++) { 
            digitalWrite(i, HIGH);
          } delay(300); // Wait for 1000 millisecond(s)
         }



void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    if (buttonPressCount % numberOfLED == 0)
      for(int i = 8; i <= 12; i++){
     digitalWrite(i, LOW); // turn all LEDs on:
      Z = 2;
    } else
      if(buttonPressCount % numberOfLED == 1)
      for(int i =8; i <= 12; i++){
      digitalWrite(i, HIGH);
      Z = 0;
    }
      else
      if(buttonPressCount % numberOfLED == 2)
      Z = 1;
      
        delay(0);
       
    buttonPressCount++;
    delay(300);
  }
  
  


  if (Z == 1) {
     // BlinkWithoutDelay
     unsigned long newTime = millis();
     if (newTime - oldTime >= 100) {
        state = !state;
        oldTime = newTime;
        AA();
     }
  } else {
    if(Z == 2){
    for(int i =8; i <= 12; i++){
      digitalWrite(i, LOW);
    
     // no Blinking : just leave redLed as is
  }
    }
  }
  Serial.println(Z);
}

@alto777

The program has 3 modes: random, which turns on the leds in random order, total, which turns all of them on, and off.

OK, see?

You don't say the LEDs should be turned off in that mode.

So

  • mode 1. Turn on all LEDs

  • mode 2. Turn off all the LEDs

  • mode 3. turn on the LEDs in a random order

What makes the change in mode? Do the modes go in an order, like 1 2 3 1… or can we go from one to another at will?

Shoukd mode 3 be terminated early if we switch to mode 1?

And so forth. If you don't plan on something, you'll just get what happens. Which may not be what you expected.

a7

thank you for the advice.

the order that is put the Ifs are for functions as an counter, the counter As you press the button the counter marks the mode number until the last one that is 3 and returns to the beginning that is 1.

yes, Precisely in the last modification I made I got what I expected.

Exactly!

this the code with the solved problem of the delays.

// 2015/03/29
/*
  Demonstrates the use of an array to hold pin numbers.
  Each time the push button is pressed, next LED in the
  sequence will be turned on while the rest of LEDs are off.
*/
const int buttonPin = A1;
/*const int buttonpin2 = A2;*/ // the number of the pushbutton pin
const int ledPin1 = 8;   // the number of the LED pin
const int ledPin2 = 9;   // the number of the LED pin
const int ledPin3 = 10;   // the number of the LED pin
const int ledPin4 = 11;   // the number of the LED pin
const int ledPin5 = 12;   // the number of the LED pin
// variables will change:
int buttonState = 0;   // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 3;
int time = 2;

int Z = 0;


unsigned long newTime;
unsigned long oldTime = 0;
byte newButtonState;
byte oldButtonState = 0;
boolean ledState = false;
boolean blinkState = false;
boolean state = LOW;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}


void AA(){
  
      digitalWrite(random(8,13), HIGH);
      delay(100); // Wait for 100 millisecond(s)
      digitalWrite(random(8,13), LOW);
      delay(100);
  
}    
  
void BB(){ digitalWrite(random(8,13), LOW); delay(500); // Wait for 1000 millisecond(s) 
          for (int i=8; i<13; i++) { 
            digitalWrite(i, HIGH);
          } delay(300); // Wait for 1000 millisecond(s)
         }



void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    if (buttonPressCount % numberOfLED == 0)
      for(int i = 8; i <= 12; i++){
     digitalWrite(i, LOW); // turn all LEDs on:
      Z = 2;
    } else
      if(buttonPressCount % numberOfLED == 1)
      for(int i =8; i <= 12; i++){
      digitalWrite(i, HIGH);
      Z = 0;
    }
      else
      if(buttonPressCount % numberOfLED == 2)
      Z = 1;
      
        delay(0);
       
    buttonPressCount++;
    delay(300);
  }
  
  


  if (Z == 1) {
     // BlinkWithoutDelay
     unsigned long newTime = millis();
     if (newTime - oldTime >= 100) {
        state = !state;
        oldTime = newTime;
        AA();
     }
  } else {
    if(Z == 2){
    for(int i =8; i <= 12; i++){
      digitalWrite(i, LOW);
    
     // no Blinking : just leave redLed as is
  }
    }
  }
  Serial.println(Z);
}

still unclear what this function is intended to do
don't see a need for the delays

i was decrease the delays for solve the button respond problem.

@gcjr

this function is for the random leds mode.

the leds have 3 modes

  • mode 1. Turn on all LEDs
  • mode 2. Turn off all the LEDs
  • mode 3. turn on the LEDs in a random order

AA() turns off some LED.

This is inconsistent with "turn on the LEDs in a random order".

Not only that, AA() turns on a random LED, the turns off another at random, probably not the same LED.

So we got a ways to go on helping you describe what you have in mind.

a7

exactly

yes, many heads think more than one head :wink:

thanks for the aid.

so what's the purposed of the delays?

turn on randomly one off the leds and turn off randomly one of the leds.

the delay is for maintain of the leds speed slowly.

@sebastianriv do you understand why this is so frustrating? And why I'm going to leave you to it?

You have arrived us at a quite different mode 3. Turning on and off the LEDs at random (until the mode changes?) is not the same as turning on the LEDs in a random order.

Good luck!

a7

[quote="alto777, post:39, topic:1097817"}

Good luck!

a7
[/quote]
you too, good luck

this is my Tinker cad link with the code for understand better the purpose.