Good morning,
I'm a total newb and this is my first post here.
I have Arduino Uno that I'm trying to program it to use an analog sensor to control bi-color LED's. I built my sketch and got it to verify without any errors. I loaded it up and checked the logger and I'm seeing signal Tx on the board.
Here's the problem:
I have int POWER = 8; and pinMode(POWER, OUTPUT); and under the void signalred(int valA1) area I have digitalWrite(POWER, HIGH);
When I load my sketch I get no 5v out of pin 8. But if I load a different sketch using the same output power pin 8 it works and I get like 4.75v.
I've compared the two sketches over and over and can't for the life of me figure out why I don't have 5v on pin 8 with my sketch.
int sensePin1 = A2;
int GREEN = 4;
int RED = 7;
int POWER = 8;
void setup() {
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(POWER, OUTPUT);
}
enum SIGNALSTATES
{
ST_GREEN,
ST_RED,
};
SIGNALSTATES signalState = ST_GREEN;
void loop() {
int valA1 = analogRead(sensePin1);
Serial.println(valA1);
delay(200);
switch (signalState)
{ case ST_RED:
signalred(valA1);
break;
}
}
void signalred(int valA1) {
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(POWER, HIGH);
if (valA1 < 500) {
signalState = ST_RED;
}
else if (valA1 > 500) {
signalState = ST_GREEN;
}
}
void signalgreen(int valA1) {
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(POWER, HIGH);
if (valA1 < 500) {
signalState = ST_GREEN;
}
else if (valA1 > 500) {
signalState = ST_RED;
}
}
What is the sketch that works? (please post using code tags, see how to use the forum item #7)
When the program first starts, what do you think the value of signalState is?
Here is the one that works.
Signal state should be ST_GREEN
int sensePin1 = A0;
int sensePin2 = A1;
int RED = 6;
int YELLOW = 5;
int GREEN = 3;
int POWER = 8;
void setup() {
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(POWER, OUTPUT);
}
enum SIGNALSTATES
{
ST_GREEN,
ST_RED1,
ST_RED2,
ST_YELLOW,
};
SIGNALSTATES signalState = ST_GREEN;
void loop() {
int valA1 = analogRead(sensePin1);
int valA2 = analogRead(sensePin2);
Serial.println(valA1);
Serial.println(valA2);
delay(200);
switch (signalState)
{
case ST_GREEN:
signalgreen(valA1, valA2);
break;
case ST_RED1:
signalred1(valA1, valA2);
break;
case ST_RED2:
signalred2(valA1, valA2);
break;
case ST_YELLOW:
signalyellow(valA1, valA2);
break;
}
}
void signalgreen(int valA1, int valA2) {
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, HIGH);
digitalWrite(POWER, HIGH);
if (valA1 < 500 && valA2 > 500) {
signalState = ST_RED1;
}
else if (valA1 > 500 && valA2 < 500) {
signalState = ST_RED2;
}
}
void signalred1(int valA1, int valA2) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(POWER, HIGH);
if (valA1 > 500 && valA2 < 500) {
signalState = ST_YELLOW;
}
}
void signalred2(int valA1, int valA2) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(POWER, HIGH);
if (valA1 < 500 && valA2 > 500) {
signalState = ST_YELLOW;
}
}
void signalyellow(int valA1, int valA2) {
digitalWrite(GREEN, HIGH);
digitalWrite(RED, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(POWER, HIGH);
delay(5000);
if (valA1 > 500 && valA2 > 500) {
signalState = ST_GREEN;
}
}
Correct. ST_GREEN.
I see a big noticeable difference between the programs.
And in the one that doesn't work in the original post, which case is executed? (did we forget to program something?)
Yes, they are different. I got the first one to work, it was from a youtube video, it controls a set of signals for a railroad, when a train comes on the block the signal switches from green to red. When the train leaves the block from either end it goes from red to yellow for a few seconds then back to green.
The first code that doesn't work only controls one signal with two bi-color LED's for a single ended block of track.
My assumption was that if my code verified with no errors that it was working properly. Is there something more in the non-working sketch that would nullify the power to it?
AJ
The code that doesn't work, controls only 1/2 of one signal.
And in the one that doesn't work in the original post, which case is executed on the first pass? (did we forget to program something? hint, hint)
I really appreciate the help. I'm not a programmer and this is my first swing at it. I don't know what you mean by which case? :o
I think I'll take a step back and do more, much more, reading on programming the Uno. 
You don't need to take a step back. It's not an UNO. Look at the code in the first post and trace what happens based on the values of the variables.
'Case' as in the cases of a switch statement.
What happens in the switch statement when signalState = ST_RED? What happens when signalState = ST_GREEN?
Thanks. I will do that. I'll let you know what I find out.
Okay! I found what you were talking about and fixed it. The system now powers on. But I've inadvertently wrote a sketch that would be great if I were making a railroad crossing signal. 
Now when I power the Uno on the two LED's flash back and forth. I know it has something to do with the IF ELSE statements at the bottom of the sketch. What I'm looking for is to have the Green LED on at boot. When the sensor is activated the green goes off and the red comes on. Then when the sensor is tripped again it switches back to green.
Post your new code and explain what you want it to do.
What is the sensor connected to? What value do you see for valA1 in the serial monitor? I'd bet > 500.
int sensePin1 = A2;
int GREEN = 4;
int RED = 7;
int POWER = 8;
void setup() {
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(POWER, OUTPUT);
}
enum SIGNALSTATES
{
ST_GREEN,
ST_RED,
};
SIGNALSTATES signalState = ST_GREEN;
void loop() {
int valA1 = analogRead(sensePin1);
Serial.println(valA1);
delay(500);
switch (signalState)
{ case ST_RED:
signalred(valA1);
break;
case ST_GREEN:
signalgreen(valA1);
break;
}
}
void signalred(int valA1) {
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(POWER, HIGH);
if (valA1 < 500) {
signalState = ST_RED;
}
else if (valA1 > 500) {
signalState = ST_GREEN;
}
}
void signalgreen(int valA1) {
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(POWER, HIGH);
if (valA1 > 500) {
signalState = ST_GREEN;
}
else if (valA1 < 500) {
signalState = ST_RED;
}
}
Serial Monitor reads in the 900's with nothing at the sensor. When I put my finger over the sensor it drops to the 60's. When I remove my finger it goes back to green. It's a great block detection circuit but I need one more tweak.
Scenerio:
A short train enters a closed ended section of track, a spur line. The train may not be long enough to keep the sensor active and after the short train passes the sensor the Signal Light will switch back to GREEN indicating that the track is clear when it is not.
What I want this to do is start GREEN, which it does. Trip sensor the first time and go to RED, which it does. Then I want it to stay on RED until the sensor is tripped a second time, then go back to GREEN. Indicating that the train has left the spur and the line is now safe to enter.
You have it working now? I see the change in to the if statement.
It works, but I want it to stay in the RED state until the sensor is tripped a second time. So basically on/off/on.
The is what I read from your last two posts
- Green On
- Train enters (sensor tripped first time)
- Turns Red
- Train Exits - stays red
- Train enters (sensor tripped second time)
- Turns Green
I'm sure this can't be correct. There is no difference if the sensor is tripped by the train for your finger. the program will do the same thing. If that operation is incorrect, then your design basis is incorrect and you will need to return to step 0.
P.S. You can simplify both if statements. They don't need to be if else. You can't run signalred unless the state is ST_RED, it doesn't need to be set to ST_RED if it is already ST_RED. Just delete from 'if' through 'else' leaving the second if statement.
Sorry for the confusion. Here's the operation in your format with correction.
- Green On Yes, Default when starting the program
- Train enters (sensor tripped first time) Correct, trips the sensor
- Turns Red Correct, spur is occupied
- Train Exits - stays red Turns back to green after second time passed the sensor on it's way out.
- Train enters (sensor tripped second time) First Train has left, Green should be on. No new train.
- Turns Green Yes, back to start default Green stays on.
I could probably do this with a second sensor or more but I"m trying to keep it to one proximity sensor.
I really don't understand your statements about "the second time'. The train and your finger work the same.
The sensor has only two states, blocked/tripped and not blocked/tripped.
When the sensor is clear, the value is 900, when blocked 60 (your numbers from a few posts back).
- Green On = sensor value 900
- Train enters = sensor value 60
- Turns Red
- Train Exits - sensor value 900
- Turns Green
There is no "second time" in that sequence.
There are only two transitions (900 to 60, and 60 to 900). You have to work with that or make a change to your design basis (how you want it to work or what you have to work with).
I was thinking that too, that there are only two states. And there no way to tell it to be in both states at the same time, or detect the point at where it goes from high to low.
Thanks for all the help. I'm going to rethink my design or maybe where I was going to put this signal on the layout.
The point is you helped me get past my original power problem. Now I'm off to rule the world. 
AJ
psuajs15:
Thanks for all the help. I'm going to rethink my design or maybe where I was going to put this signal on the layout.
Nope. Same problem only in reverse.
You need two sensors less than a small train apart.
If sensorBefore or sensorAfter is blocked then red
If sensorBefore and sensorAfter are clear then green
Or use a timer to hold it red after the sensor goes off, but I hate timers as there is never a guarantee on what the right value is.