Can somebody please help me with this code. I am hoping to run leds in sequence over and over, two leds on from 0 seconds to 3 seconds then off, and then a different two turn on from 3-6 seconds then off, and then another two from 6-9 seconds and then off. After 9 seconds the time resets, and starts the sequence again.
When compiling program there are no errors.
The timer works fine and resets after 9 secs.
At the moment just one led turns on, but it is lit dim. But nothing changes. Any help on this would be appreciated. Cheers
const long Interval = 10;
unsigned long PreviousMillis = 0;
unsigned long Time = 0;
const long TimeReset = 9000;
const long StartTime = 0;
int LedPin[6] = {
2, 3, 4, 5, 6, 7
};
void setup() {
Serial.begin(9600);
for (int pins = 2; pins < 8; pins ++) {
pinMode(pins, OUTPUT);
}
}
void loop() {
long unsigned int CurrentMillis = millis();
if (CurrentMillis - PreviousMillis >= Interval) {
PreviousMillis = CurrentMillis;
Time = (Time + Interval);
}
if (Time >= TimeReset) {
Time = StartTime;
}
if (Time >= 0) {
digitalWrite(LedPin[2,5], HIGH);
digitalWrite(LedPin[3,4,6,7], HIGH);
}
if (Time >= 3000) {
digitalWrite(LedPin[3,6], LOW);
digitalWrite(LedPin[2,4,5,7], HIGH);
}
if (Time >= 6000) {
digitalWrite(LedPin[4,7], LOW);
digitalWrite(LedPin[2,3,5,6], HIGH);
}
Serial.println(Time);
}
Timer.ino (867 Bytes)
Thanks for reply spycatcher2k, i have just tried changing the interval from 10ms to 1000ms and i have the same problem.
Can u please let me no what you would set the interval too, or if you can see anything else wrong with the code.
I have just noticed that the HIGH and LOW commands are in the wrong order, i have put this correct but i still have the same problem.
It seems strange that the led that is lit is pin 7, i forgot to say this before so thought i would mention.
digitalWrite(LedPin[4,7], LOW);
digitalWrite(LedPin[2,3,5,6], HIGH);
WARNING: Arrays don't work this way. You can't select multiple indexes.
WARNING: Even if arrays worked this way you can't pass multiple pin numbers to digitalWrite();
This is closer to what you seem to intend:
digitalWrite(LedPin[4], LOW);
digitalWrite(LedPin[7], LOW);
digitalWrite(LedPin[2], HIGH);
digitalWrite(LedPin[3], HIGH);
digitalWrite(LedPin[5], HIGH);
digitalWrite(LedPin[6], HIGH);
WARNING: An array of length 6 does not have an element 6 or element 7. Indexes start at ZERO.
Closer to what you really intended:
digitalWrite(LedPin[0], HIGH);
digitalWrite(LedPin[1], HIGH);
digitalWrite(LedPin[2], LOW);
digitalWrite(LedPin[3], HIGH);
digitalWrite(LedPin[4], HIGH);
digitalWrite(LedPin[5], LOW);
Thank you for the reply johnwasser, i will try writing the code the correct way.
Can u please tell me is there a way to digitalWrite to multiple pins.
You could do it with an array in a loop:
for (byte x = 0; x<6; x=x+1){
digitalWrite (LEDpin[x], dataArray[x]);
}
with
byte LEDpin[] = {2,3,4,5,6,7,};
byte dataArray[] = {1,1,0,1,1,0,};
Or perhaps easier (and faster execution)
PORTD = PORTD & 0b00000011; // clear bits 7-2, leave 1,0 alone
PORT = PORTD | (newData & 0b11111100); // mix in new 6 upper bits, leave 1,0 alone
Use bit math to manipulate newData as needed, example:
newData = newData & 0b00111111; // clear bits 7 & 6
newData = newData | 0b10000000; // set bit 7
Thankyou will give it ago.
Is this your code? I am sure I have seen it before with exactly the same request. This was a few weeks ago. So have you posted this under a different name? Or is this homework where you are supposed to find out what is going on?
The errors are identical to last time, down to that odd way of addressing pin outputs.
NO this is the first time i have posted this problem.
Im trying to learn programming on my own as hobby at the moment.
OK so why the very odd way of talking to an output pin?
Have you had experiences in other languages?
The other big problem is that suppose the time is 6001, note that all the if statements will be executed on its way to the one greater than 6000. You need a proper state machine.
See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0