How to change outcome if previous statement occurs

hello
i am just looking for help with what i need to look in to for my project i dont learn if i am just given the answer.
my issue is with led's
i have 4 led's.
i never want more then 2 to be off at a time.
so when i get down to 1 being left on i want it to turn another.
how do i explain this 0=led off 1=led on
if
0001 occurs
do 1001 this is ok
but if 1001 occurs and i change it to 0001 i don't want 1001 to reoccur i am looking to change it to 0101 or 0011

basically what do i need to research in order to prevent repeat of previous code?

i have tried :
random num
random seed
switch ... case
else
else if

if you know a good tutorial i would greatly appreciate the link

Your topic was MOVED to its current forum category as it is more suitable than the original as it has nothing to do with installation and troubleshooting of the IDE

1 Like

What about cases when more then two leds are ON?
If cases 1011 or 0111 are valid? Or number of leds switched ON should be exactly two?

it starts with all 4 on and only runs first if statement when 3 leds are off. i can drop code if that helps.

How do you select which led will switched off next? random?

int p1=A0;
int p2=A1;
int p3=A2;
int p4=A3;
//case vals
int ld1v=HIGH;
int ld2v=HIGH;
int ld3v=HIGH;
int ld4v=HIGH;
//calculated value
int RV1;
int RV2;
int RV3;
int RV4;

//led state
int ld1s = HIGH;
int ld2s = HIGH;
int ld3s = HIGH;
int ld4s = HIGH;
//led case state
int plds1=HIGH;
int plds2=HIGH;
int plds3=HIGH;
int plds4=HIGH;

//piezo output
float pZ1;
float pZ2;
float pZ3;
float pZ4;

//LED's
int ld1=2;
int ld2=3;
int ld3=4;
int ld4=5;

//delaytime
int dt1=1;
int dt2=3000;
int dt3=30;
//analog data read

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ld1, OUTPUT);
pinMode(ld2, OUTPUT);
pinMode(ld3, OUTPUT);
pinMode(ld4, OUTPUT);

digitalWrite(ld1,HIGH);
digitalWrite(ld2,HIGH);
digitalWrite(ld3,HIGH);
digitalWrite(ld4,HIGH);

pinMode(pZ1, INPUT);
pinMode(pZ2, INPUT);
pinMode(pZ3, INPUT);
pinMode(pZ4, INPUT);

delay (dt2);

}
void loop(){
// put your main code here, to run repeatedly:

RV1=analogRead(p1);
pZ1=RV1;
Serial.print("piezo 1 val ");
Serial.println(pZ1);
if(pZ1>=1000)
{
digitalWrite(ld1,LOW);
}
delay(dt1);

RV2=analogRead(p2);
pZ2=RV2;
Serial.print("piezo 2 val ");
Serial.println(pZ2);
if(pZ2>=1000)
{
digitalWrite(ld2,LOW);
}
delay(dt1);

RV3=analogRead(p3);
pZ3=RV3;
Serial.print("piezo 3 val ");
Serial.println(pZ3);
if(pZ3>=1000)
{
digitalWrite(ld3,LOW);
}
delay(dt1);

RV4=analogRead(p4);
pZ4=RV4;
Serial.print("piezo 4 val ");
Serial.println(pZ4);
if(pZ4>=1000)
{
digitalWrite(ld4,LOW);
}
delay(dt1);

if (digitalRead(ld1) == LOW and digitalRead(ld2) == LOW and digitalRead(ld3) == LOW and digitalRead(ld4) ==HIGH)
{
digitalWrite(ld1,HIGH);

// everything below here seems to not work or run in a loop to previous statement i.e. led flashing

if (digitalRead(ld1) == LOW and digitalRead(ld2) == LOW and digitalRead(ld3) == LOW and digitalRead(ld4) ==HIGH)
{
digitalWrite(ld2,HIGH);
}
}

}

i understand that the my last (if) will never work that was just the last thing i have tried...

no through piezo input on analog read

Very hard to edit code. Never write code by copying the same parts.

In your code, you need to create three arrays - 1) for the digital pins of the diodes, 2)for the analog pins of the signals, and 3)for the values HIGH/LOW. Write everything in a loop, the code will shortened by 4 times.

After switching diode off, check the sum of the values of all diodes. If it is less than two, turn on the nearest off LED AFTER the one that just turned off

1 Like

thank you

what structure / statement would you use after that if (digitalRead(led1) == LOW and digitalRead... statement to check what previous diodes are on or off? and after verification of which diodes are on/off which structure / statement would you use to set nearest diode to on?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You don't need digitalRead() for check diode states because is you who switching it on and off and always should know which on or not
You goes from one to another in a loop - 1, 2, 3, 4, 1, 2... Say, if the 3rd diode is just switched off and the sum of levels goes to 1. Then you check the diode 4 - if it OFF - you will turn it On. if this diode ON, you go to the next...

1 Like

sorry for the inconvenience earlier now that we are here. how do i set parameters for checking (sum levels). so that i dont have a reoccurring event. and would you like me to rewrite the code so that we can start from where we left off?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.