trigger led after led at HIGH input

hey everyone,

iam a beginner in arduino and coding. i buyed a book with many informations about starting with the arduino, did some experiments and finally
doesnt come forward now.

i have 16 leds connected (via 330r) to the digital outputs (pin 30-45) of my arduino mega. a momentary switch is also connected to Digitalinput 22

the goal: plug in the arduino, the first led lights up, now i want to push the momentary switch to jump to the next LED (led2) but the first one goes out. so on until the
chain reached led 16, after led 16 it starts again to count from LED 1. I MEAN, everytime a HIGH occurs at digital pin 22 the next led will go on and the previous one go off.

i tried many examples with laststate variables, delay (hundred % unusual :slight_smile: ), millis, but that was not what i want. a high level at one input activate the next led in the array.

but damn HOW?
i know this:

for( i= o; i<16; i++)

or for(thispin=0; thispin < 45; thispin++)

i know IF, IF/else
i know int lastcount.. or something like that,
i know sensorValue but it isnt functionally with my idea.

i´ve attached my code from now .. i hope anyone can give me some suggestion in HOW TRIGGER LED AFTER LED BY AN INCOMING HIGH LEVEL

very thank you !!

int ledPin[] = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45};
int schalterPin = 8;
int schalterStatus;
int sensorPin = A1;
int taster = 22;        //doesnt know how i can start counting led after led by 
int tasterstatus = 0;   //pressing this button 
int tasterValue = 0;          //i think i need that integer
int previousTasterValue = 0;  // and this one
int sensorValue;

void setup()
{
  for(int i = 0; i < 16; i++)
  for(int i = 15; i >= 0; i--)
  pinMode(ledPin[i], OUTPUT);
  pinMode(schalterPin, INPUT);
     
   
}

/*void forthback(){   // the delay is unusual but iam glad that iam able to control the rate :)
  for (int i = 0; i < 16; i++)
    {
    sensorValue = analogRead(sensorPin);
    // turn the pin on:
    digitalWrite(ledPin[i], HIGH); 
    delay(sensorValue);                 
    // turn the pin off:
    digitalWrite(ledPin[i], LOW);   
    }

  // loop from the highest pin to the lowest:
  for (int i = 15; i >= 0; i--)
    {
    sensorValue = analogRead(sensorPin);
    // turn the pin on:
    digitalWrite(ledPin[i], HIGH);
    delay(sensorValue);
    // turn the pin off:
    digitalWrite(ledPin[i], LOW);
    }
}*/

void forth(){ // lets take this one for experimenting
  for (int i = 0; i < 16; i++)
    {
    sensorValue = analogRead(sensorPin);  // do i need to insert a IF ELSE  commandline here?
    digitalWrite(ledPin[i], HIGH);        // how can i make it count ????
    delay(sensorValue);                 
    // turn the pin off:
    digitalWrite(ledPin[i], LOW);   
    }
}
 


void loop() {  // here i can switch between leds forware or forward and then backward
  schalterStatus = digitalRead(schalterPin);
  if(schalterStatus == HIGH)
    forthback();
  else
    forth();
}

a minimal version, not tried to compile with leds on pin 3,4,5 should get you started.

int ledpin[3] = { 3,4,5 };
int ledcount = 3;
int currentPin = 0;  

void setup()
{
  for (int i=0; i< ledcount; i++) pinMode(ledpin[i], OUTPUT);

  for (int i=0; i< ledcount; i++) digitalWrite(ledpin[i], LOW);
}

void loop()
{
  if (digitalRead(buttonpin) == HIGH)  // assuming buttonpress => low -> high
  {
     digitalWrite(ledpin[currentPin], LOW);
     currentPin = (currentPin + 1) % ledcount;  // wraps ledpin around
     digitalWrite(ledpin[currentPin], HIGH);
  }
  delay(1000);
}

Using your array, try doing a for loop like this:

For(int i = 1; i<=ledPin.length;i++) {
   digitalWrite(ledPin[i-1],LOW);
   digitalWrite(ledPin[i], HIGH);
   delay(1000);
}

@xyntec: there is the delay ... that means it will automatically rotate the leds for an time of some milliseconds ... but that isnt the key,

press button = switches the next led on /the lastone off .. but just if the button is pressed .. delay is unusual here :slight_smile:

@rob: the leds rotate if i press the button, BUT the run of their own .. because the delay ... thats exactly my problem .. the use of delay or intervall isnt the right way .. mmh :fearful:

you can do it without delay but then you have to

  • debounce the button ( see tutorial section)
  • keep the state of the button in a var to detect the change
  • only switch on next led if the state changes

Tested this code, works how I believe you are looking for, if not let me know I will make changes where needed.

NOTE: This was tested with only 4 LED's, you will need to change the array and for loops according to your specifications (don't forget to change push button location!)

// Number of Button location
int buttonPin = 2;     

// Array of where leds are
int ledPinArray[] = {10,11,12,13};
// Current location in array
int pos = 0;

// read button state
int buttonState = 0;        

void setup() {
  // initialize the LEDs as an output
  for(int i = 0; i < 4; i++) { // Change 4 to length of array
    pinMode(ledPinArray[i],OUTPUT);
  }
  
  // Accept inputs from button pin
  pinMode(buttonPin, INPUT);    
  
  // Turn on the first LED
  digitalWrite(ledPinArray[0],HIGH); 
  
}

void loop(){
  
  // read the state of the pushbutton
  buttonState = digitalRead(buttonPin);

   // if the button is preseed
  if (buttonState == HIGH) {     

    // increment location in Array
    pos++;
    
    // if the location is higher than the length of the array, set it back to 0
    if(pos >= 4) {
      pos = 0;
    }
    
    // turn on next LED
    digitalWrite(ledPinArray[pos], HIGH);  
    
    // Turns off last LED
    if(pos!=0) { // If the current possition isnt the first in the row
      digitalWrite(ledPinArray[pos-1], LOW);
    } else { // If it is, turn off the last one
      digitalWrite(ledPinArray[3],LOW);
    }
    
    // without this, it will change LED's faster than you can let go of the button
    delay(100);
    
  }

}

@xynteq: the problem is that the leds rotate if the state of the button is HIGH .. but only ONE led should be flash. if the pressed again THEN
the next led should be ON.

@rob: i tried some experiments with the debounce function. but i didnt know how to implement that in the code. i´ve read so many sequencer codes but doesnt really understand
HOW they synchronize to a common clock.

the main idea is: TAKE AN EXTERNAL ANALOG CLOCK (p2p = gnd -> 5volts) and step the 16 LEDS .. the button is to simulate a transistor which closes for the time a HIGH level comes in

@ xynteg: i tried a very short gigh level peak .. e voila .. it works. so i need to remember: YOUR CODE WORKS, i need to tweak a pulse wave to short peak (compare GATE and TRIG)

wow

serpath:
@ xynteg: i tried a very short gigh level peak .. e voila .. it works. so i need to remember: YOUR CODE WORKS, i need to tweak a pulse wave to short peak (compare GATE and TRIG)

wow

'

I'm sorry, but this response does not make any sense to me, so it works or does not work for what you're looking for?

look, if a switch is closed it have just one state. a analog clock is a pulse wave with a pwm of 50% ... that means ON_ OFF_ON ... an so on .. if i use THAT type of state the leds run for the time the state is HIGH, if i use some buffering and rc filtering i get a TRIG impuls out of the gate pulsewave for every time a rising edge occurs. thats a very very short high state and that lets flash led AFTER led .. in that time the short TRIG occurs. .. without this filtering its a pure on/off pulsewave .. while the on state .. more leds rotate, with trig just once at a time

check the two lower periods

hey,

here is my modified code for selecting forth or back. it works fine. the next question is .. how iam able to let it count random?
thank you

// Number of Button location
int buttonPin = 22;     

// Array of where leds are
int ledPinArray[] = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45};
// Current location in array
int pos = 0;

// read button state
int buttonState = 0;  
int mode1 = 8;
int mode2 = 9;
int mode1status;
int mode2status;


void setup() {
  // initialize the LEDs as an output
  for(int i = 0; i < 16; i++)
  //for(int i = 15; i > 16; i--) 
  { // Change 4 to length of array
    pinMode(ledPinArray[i],OUTPUT);
  }
  
  // Accept inputs from button pin
  pinMode(buttonPin, INPUT);    
  
  // Turn on the first LED
  digitalWrite(ledPinArray[0],HIGH); 
  //digitalWrite(30,HIGH);
  
}

void vor()
{ 
  // read the state of the pushbutton
  buttonState = digitalRead(buttonPin);

   // if the button is preseed
  if (buttonState == HIGH) {     

    // increment location in Array
    pos++;
    
    // if the location is higher than the length of the array, set it back to 0
    if(pos >= 16) {
      pos = 0;
    }
    
    // turn on next LED
    digitalWrite(ledPinArray[pos], HIGH);  
    
    // Turns off last LED
    if(pos!=0) { // If the current possition isnt the first in the row
      digitalWrite(ledPinArray[pos-1], LOW);
    } else { // If it is, turn off the last one
      digitalWrite(ledPinArray[15],LOW);
    }
    
    // without this, it will change LED's faster than you can let go of the button
    delay(10);
    
  }
}
  
void back(){
   // read the state of the pushbutton
  buttonState = digitalRead(buttonPin);

   // if the button is preseed
  if (buttonState == HIGH) {     

    // increment location in Array
    pos--;
    
    // if the location is higher than the length of the array, set it back to 0
    if(pos <= -1) {
      pos = 15;
    }
    
    // turn on next LED
    digitalWrite(ledPinArray[pos], HIGH);  
    
    // Turns off last LED
    if(pos!=15) { // If the current possition isnt the first in the row
      digitalWrite(ledPinArray[pos+1], LOW);
    } else { // If it is, turn off the first one
      digitalWrite(ledPinArray[0],LOW);
    }
    
    // without this, it will change LED's faster than you can let go of the button
    delay(10);
    
  }
}
  
void zufall();



void loop(){
  mode1status = digitalRead(mode1);
  mode2status = digitalRead(mode2);
    {if(mode1status == LOW)
      vor();
    else
      back();}
    
    {if(mode2status == HIGH)
      zufall();
    else
      vor();
    }
  
  
 

}

how iam able to let it count random?