I am new to C programming and asking for help with this little project I have using 5 LEDs, 5 relays, a 5 position rotary switch and an Arduino UNO board.
I want the LEDs to randomly blink for 3 seconds, then pause for x number of seconds depending on the rotary switch position and then repeat the process. Here are the seconds it should pause when the switch is in the following positions:
1 = 3 seconds pause and turn LED on
2 = 6 seconds pause and turn LED on
3 = 10 seconds pause and turn LED on
4 = step through all LEDs, one at a time, turn LED on and wait for 2 second, turn it off and go on to the next LED and repeat the process for all 5 LEDs, then do this all over and never step until the switch position is changed
5 = Pause indefinitely and turn LED on
The relays are connected to pins 8, 9, 10, 11 and 12 and one LED is connected to each relay. The positions on the rotary switch are connected to pins 2, 3, 4, 5 and 6. 3.5v are supplied to the common terminal of the rotary switch. So, I am checking for “HIGH” and “LOW” on a pin to determine how long the software should pause/delay execution.
When I run the software everything seems to work OK, but when pin 4 is “HIGH” it seems to get ignored. It does pick up the “pauseTime = 2000;” and go into blink/pause loop with a 2 seconds delay. It should step through all 5 LEDs, 2seocnds on and 2 seconds off and do that forever, but instead it goes into a random LED selection mode. I hope my description makes sense.
As a side note, there are other components connected to the relays, but I wanted to keep the description simple.
The code is listed below. I would appreciate it very much if someone could look at it and help me out.
// Multi targets V2 4.17.2015
// 5 targets
// random selection energizes LEDs & sensors
// 5 options:
// 1. switch on 1 = 3 secs pause
// 2. switch on 2 = 6 seconds pause
// 3. switch on 3 = 10 seconds pause
// 4. switch on 4 = step through all sensors & LEDs 2 secs on, 2 secs off
// 5. switch on 5 = random LED/sensor always on
int ranNum = 3; // default is LED 3
int blinkTime = 40; // default
int pauseTime = 3000; // default
int ledPin1 = 8; // LED connected to digital pin 8
int ledPin2 = 9; // LED connected to digital pin 9
int ledPin3 = 10; // LED connected to digital pin 10
int ledPin4 = 11; // LED connected to digital pin 11
int ledPin5 = 12; // LED connected to digital pin 12
int switchPin1 = 2; // 3 seconds delay
int switchPin2 = 3; // 6 seconds delay
int switchPin3 = 4; // 10 seconds delay
int switchPin4 = 5; // step-by-step LEDs 1 through 5
int switchPin5 = 6; // endless loop delay
// array to define 5 output ports
#define PIN_COUNT 5
int ledPin[ PIN_COUNT ] = {
8, 10, 12, 9, 11
};
int i;
void setup () {
// Check which pin is set
if (digitalRead(2) == HIGH)
pauseTime = 3000; // 3 secs
if (digitalRead(3) == HIGH)
pauseTime = 6000; // 6 secs
if (digitalRead(4) == HIGH)
pauseTime = 10000; // 10 secs
if (digitalRead(6) == HIGH)
pauseTime = 999999999; // stay on as long as possible
// don't worry about remaining code
// ********** this is where I have problems with the code
// Do this as long as switchPin4 is HIGH
if (digitalRead(switchPin4) == HIGH){
pauseTime = 2000; // ~2 secs
while(switchPin4 == HIGH )
{
delay(pauseTime);
digitalWrite(ledPin[2], HIGH);
delay(pauseTime);
digitalWrite(ledPin[2], LOW);
delay(pauseTime);
digitalWrite(ledPin[3], HIGH);
delay(pauseTime);
digitalWrite(ledPin[3], LOW);
delay(pauseTime);
digitalWrite(ledPin[4], HIGH);
delay(pauseTime);
digitalWrite(ledPin[4], LOW);
delay(pauseTime);
digitalWrite(ledPin[5], HIGH);
delay(pauseTime);
digitalWrite(ledPin[5], LOW);
}
}
// looks like switchPin4 went to LOW if it comes to this point
// create random output pin
randomSeed( analogRead( 0 ) );
for ( int i=0 ; i < PIN_COUNT ; i++ ) {
pinMode( ledPin[ i ], OUTPUT );
}
}
void loop () {
for ( int i=75 ; i ; i-- ) { // 75 = ~3 secs
ranNum = random( PIN_COUNT );
for ( int pin=0 ; pin < PIN_COUNT ; pin++ ) {
if ( pin == ranNum ) {
digitalWrite( ledPin[ pin ], LOW );
} else {
digitalWrite( ledPin[ pin ], HIGH );
}
}
delay( 20 + random( blinkTime ) );
}
delay( pauseTime );
}
