Go Down

Topic: Randomized Increasing/Decreasing Light Sequence (Read 513 times) previous topic - next topic

ThisGuy1840

So in this I'm trying to get four lights to randomly go up or down in a lighting sequence. I'm fairly new to Arduino so when it comes to troubleshooting the issue I'm pretty lost. At this point it just turns on the first light and from that point onward nothing else happens. If someone could take a look and tell me what I'm doing wrong please let me know.

kenwood120s

Unless your code is really huge, it seems preferred here to just stick it in the post rather than attach. As a favour to mankind ;) I have done that here for ya. Note it's in tags (the </> icon) to appear as code:

Code: [Select]
//randomized light sequence



int ledBot= 13;
int ledBMid= 12;
int ledTMid= 11;
int ledTop= 10;
int t= 100;
int r;
boolean stateledBot;
boolean stateledBMid;
boolean stateledTMid;
boolean stateledTop;

void setup() {
  // put your setup code here, to run once:
pinMode (ledBot,OUTPUT);
pinMode (ledBMid, OUTPUT);
pinMode (ledTMid, OUTPUT);
pinMode (ledTop, OUTPUT);
boolean stateledBot=false;
boolean stateledBMid=false;
boolean stateledTMid=false;
boolean stateledTop=false;

}

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


//randomization for lights
if (stateledBot==false){
  lightu1();
}
if (stateledBot==true and stateledBMid==false){
  int r=random(1,10);
}
if (stateledBMid==true and stateledTMid==false){
  int r=random(11,20);
}
if (stateledTMid==true and stateledTop==false){
  int r=random(21,30);
}
if (stateledTop==true){
  int r=random(31,40);
}

//programs when bot light is on

if (r==1){
  lightd1();
}
if (r==2 or r==3){
  lightdelay();
}

if (r>3 and r<11){
  lightu2();
}

//programs when lower middle light is on

if (r>10 and r<14){
  lightd2();
}
if (r==14){
  lightdelay();
}
if (r==15){
  lightdelay();
}
if (r>15 and r<21){
  lightu3();
}

//programs when upper middle light is on

if (r>20 and r<25){
  lightd3();
}
if (r==25){
  lightdelay();
}
if (r==26){
  lightdelay();
}
if (r>26 and r<31){
  lightu4();
}

//programs when top light is on

if (r>30 and r<38){
  lightd4();
}
if (r>37){
  lightdelay();
}
}


//SUBROUTINES FOR THE LIGHTS

void lightu1() {
  digitalWrite (ledBot, HIGH);
  delay (t);
  boolean stateledBot=true;
}

void lightu2() {
  digitalWrite (ledBMid, HIGH);
  delay (t);
  boolean stateledBMid=true;

}

void lightu3() {
  digitalWrite (ledTMid, HIGH);
  delay (t);
  boolean stateledTMid=true;
}

void lightu4() {
  digitalWrite (ledTop, HIGH);
  delay (t);
  boolean stateledTop=true;
}

void lightd1() {
  digitalWrite (ledBot, LOW);
  delay (t);
  boolean stateledBot=false;
}

void lightd2() {
  digitalWrite (ledBMid, LOW);
  delay (t);
  boolean stateledBMid=false;
}

void lightd3() {
  digitalWrite (ledTMid, LOW);
  delay (t);
  boolean stateledTMid=false;
}

void lightd4() {
  digitalWrite (ledTop, LOW);
  delay (t);
  boolean stateledTop=false;
}

void lightdelay(){
  delay (t);
}

[gumby]I've got my head stuck in the cupboard[/gumby]

PaulRB

#2
Sep 25, 2017, 08:49 pm Last Edit: Sep 25, 2017, 08:53 pm by PaulRB
First mistake:
Code: [Select]

void setup() {
  // put your setup code here, to run once:
...
boolean stateledBot=false;
boolean stateledBMid=false;
boolean stateledTMid=false;
boolean stateledTop=false;

}


When you put the name of a type, such as boolean or int, before an assignment, you are actually creating new variables, not referring to the ones you previously declared. These new variables only exist until the next } and are then destroyed. So if you want to update an existing variable, don't put a type in front of it.

Another example:
Code: [Select]

if (stateledBot==true and stateledBMid==false){
  int r=random(1,10);
}

Go Up