Hey guys,
I just wrote this code and it seems it doesn't work as expected.
Problem is the sequence-wise controlling (don't know how to explain it better).
The problem seems to be in the function Update().
I put in some Serial.print to get some variables.
Here a quick output from this stuff:
currentMillis=2
maxPin=53
minPin=47
sequence=0
i=47
i=48
currentMillis=2
maxPin=43
minPin=40
sequence=2
i=40
i=41
currentMillis=5002
maxPin=53
minPin=47
sequence=0
i=47
i=48
currentMillis=5002
maxPin=43
minPin=40
sequence=2
i=40
i=41
currentMillis=10002
maxPin=53
minPin=47
sequence=0
i=47
i=48
previousMillis=10002
currentMillis=10002
maxPin=43
minPin=40
sequence=2
i=40
i=41
previousMillis=10002
Do you guys have any idea what the problem could be?
The programm should do the following:
I have 32 LEDs which are seperated in 7 segments. Each segment runs from one LED to the next, turning them on and off. It should be like magma flowing (from top to bottom).
The other segments should start randomly. As soon as one of the random segments is finished (last LED is turned off) the next one starts.
Only segment 0 will run all the time, so it sets i to 0 after completion.
That's the theory. But as you can see in the output above, it only switches between the first two values.
Any idea why?
Beneath is the code of my programm.
Please feel free to ask, if there are any questions.
class Flasher
{
// Class Member Variables
// These are initialized at startup
int minPin; // minimum pin
int maxPin; // maximum pin
int sequence; // sequence number
long onOffTime=10000;
int status=0;
// These maintain the current state
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables
public:
Flasher(int seq, int min, int max)
{
sequence=seq; //which sequence?
minPin=min; //lowest pin
maxPin=max; //highest pin
previousMillis=millis();
}
int Update(void)
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
Serial.print("currentMillis=");
Serial.println(currentMillis);
Serial.print("maxPin=");
Serial.println(maxPin);
Serial.print("minPin=");
Serial.println(minPin);
Serial.print("sequence=");
Serial.println(sequence);
for (int i=minPin; i<=maxPin+1;i++)
{
Serial.print("i=");
Serial.println(i);
if (i==minPin)
{//if first run, turn first LED on
digitalWrite(i,HIGH);
}
else
{
if (currentMillis-previousMillis>=onOffTime)
{
previousMillis=currentMillis;
Serial.print("previousMillis=");
Serial.println(previousMillis);
if (i<=maxPin)
{
digitalWrite(i,HIGH); //turn current LED on
digitalWrite(i-1,LOW); //turn last LED off
}
else if (i>maxPin)
{//if last pin, turn off last LED
digitalWrite(i-1,LOW);
}
}
if ((sequence==0)&&(i==maxPin+1)) //if sequence 0 and loop is done, reset i.
{
i=minPin;
}
if (i==maxPin+1)
{ //if loop is done set status to 1
status=1;
}
return status; //return current status
}
}
}
};
Flasher seq0(0, 47, 53);
Flasher seq1(1, 44, 46);
Flasher seq2(2, 40, 43);
Flasher seq3(3, 33, 39);
Flasher seq4(4, 29, 32);
Flasher seq5(5, 26, 28);
Flasher seq6(6, 22, 25);
int seqNum;
int statusSeq=1;
int status0=1;
//PINs für den Schalter
int inputPin=14;
int led2=1;
int outputPinPlus=15;
int outputPinMasse=16;
int statusVorher=2;
int statusAktuell;
void setup()
{
Serial.begin (115200);
for (int i=22; i<54 ;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,LOW);
}
pinMode(inputPin,INPUT);
pinMode(outputPinPlus,OUTPUT);
pinMode(outputPinMasse,OUTPUT);
digitalWrite(outputPinPlus,HIGH);
digitalWrite(outputPinMasse,LOW);
}
void loop()
{
statusAktuell=digitalRead(inputPin);
if (statusAktuell==HIGH) //this part works just fine, randomly turning on and off LEDs.
{
if (statusVorher!=statusAktuell)
{
for (int i=22; i<54; i++)
{
digitalWrite(i,LOW);
}
}
int led1=random(22,54);
Serial.println(led1);
digitalWrite(led2,LOW);
digitalWrite(led1,HIGH);
led2=led1;
statusVorher=statusAktuell;
delay(300);
}
else //this part doesn't really work, but I think it's the Update() function
{
if (statusVorher!=statusAktuell)
{
for (int i=22; i<54; i++)
{
digitalWrite(i,LOW);
}
}
status0=seq0.Update();
if (statusSeq==1)
{
seqNum=random(1,7);
statusSeq==0;
}
switch (seqNum)
{
case 1:
statusSeq=seq1.Update();
break;
case 2:
statusSeq=seq2.Update();
break;
case 3:
statusSeq=seq2.Update();
break;
case 4:
statusSeq=seq2.Update();
break;
case 5:
statusSeq=seq2.Update();
break;
case 6:
statusSeq=seq2.Update();
break;
}
statusVorher=statusAktuell;
}
delay(5000);
}