Hi
I'm new to the forum and relatively new to Arduino. I'm attempting to get a sketch running on the SparkFun Redboard, but probably trying to run before I can walk. I have the basics working but have been disabling parts of the script to diagnose problems. Rather than go into the sketch as a whole right now, I'm posting something which I'm stumbling on, that to me should be pretty basic, so I think I'm stumbling on some of the bare fundamentals, despite hours of surfing for solutions online in forums and tutorials.
My sketch ultimately is to run a model railway signal, so is based on fading LEDs in and out based on digital inputs being HIGH (built in pull up) or LOW and also depending on whether the value from a light sensor on the track falls below a value selected manually by an analog pot.
I've included all the setup code below but only left in a tiny bit of code plus the functions it uses at the bottom.
It's the while command I have a problem with at a very basic level. It will enter the loop, but not exit when the value from the photoresistor rises. I've tried monitoring the values from the LIGHTSENSE and LIGHTPOT values on the serial monitor. They look fine until the while loops executes, but then the values freeze and so does the sketch. Could this be the source of my problems?
Many thanks for any help in advance!
const int R = 9;
const int Y = 10;
const int G = 11;
const int RI = 6;
const int Rs = 3;
const int Gs = 5;
const int REDTRIG = 2;
const int RITRIG = 4;
const int RIPRIOR = 12;
const int STARTERTRIG = 7;
unsigned long start_time;
unsigned long current_time;
unsigned long elapsed_time;
void setup()
{
Serial.begin(9600);
pinMode (R, OUTPUT);
pinMode (Y, OUTPUT);
pinMode (G, OUTPUT);
pinMode (RI, OUTPUT);
pinMode (Rs, OUTPUT);
pinMode (Gs, OUTPUT);
pinMode( REDTRIG,INPUT);
pinMode( RITRIG, INPUT);
pinMode( RIPRIOR, INPUT);
pinMode( STARTERTRIG, INPUT);
digitalWrite( REDTRIG,HIGH);
digitalWrite(RITRIG,HIGH);
digitalWrite(RIPRIOR, HIGH);
digitalWrite(STARTERTRIG,HIGH);
digitalWrite(R,HIGH);
digitalWrite(Rs,HIGH);
delay(2000);
}
void loop()
{
int LIGHTSENSE;
LIGHTSENSE = analogRead(A0);
int LIGHTPOT;
LIGHTPOT = analogRead(A1);
int TIMEPOT ;
TIMEPOT = analogRead(A2);
LIGHTSENSE = map(LIGHTSENSE,0,1023,0,255);
LIGHTPOT = map(LIGHTPOT,0,1023,1,255);
TIMEPOT = map(TIMEPOT,0,1023,5000,30000);
Serial.println(TIMEPOT);
delay(50);
while(LIGHTSENSE < LIGHTPOT)
{
RED();
}
GREEN();
void RED()
{
if ((digitalRead(Y) == HIGH) && (digitalRead(RI) == LOW))
{
int index;
for (index = 255; index >=0 ; index--)
{
analogWrite(Y, index);
delay(1);
}
for(index = 0; index <= 255; index++)
{
analogWrite(R,index);
delay(1);
}
}
if (digitalRead(G) == HIGH)
{
int index;
for (index = 255; index >=0 ; index--)
{
analogWrite(G, index);
delay(1);
}
for(index = 0; index <= 255; index++)
{
analogWrite(R,index);
delay(1);
}
}
if (digitalRead(RI) == HIGH)
{
int index;
for (index = 255; index >=0 ; index--)
{
analogWrite(RI, index);
analogWrite(Y, index);
delay(1);
}
for(index = 0; index <= 255; index++)
{
analogWrite(R,index);
delay(1);
}
///digitalWrite(R,HIGH);
///digitalWrite (Y,LOW);
///digitalWrite(RI,LOW);
}
}
void GREEN()
{
if (digitalRead(R) == HIGH)
{
int index;
for (index = 255; index >=0 ; index--)
{
analogWrite(R, index);
delay(1);
}
for(index = 0; index <= 255; index++)
{
analogWrite(G,index);
delay(1);
}
}
if ((digitalRead(Y) == HIGH) && (digitalRead(RI) == LOW))
{
int index;
for (index = 255; index >=0 ; index--)
{
analogWrite(Y, index);
delay(1);
}
for(index = 0; index <= 255; index++)
{
analogWrite(G,index);
delay(1);
}
}
if (digitalRead(RI) == HIGH)
{
int index;
for (index = 255; index >=0 ; index--)
{
analogWrite(RI, index);
analogWrite(Y, index);
delay(1);
}
for(index = 0; index <= 255; index++)
{
analogWrite(G,index);
delay(1);
}
}
}