I have been stumped for hours now, not sure if my Arduino Uno is broken or something. For the photoresistor, when I put my hand over the resistor, the value barely changes, I have tried many different valued resistors. I am also using a line-follower to detect if there is an object in the way, which also barely changes values. Basically, I'm really really confused at this point and would like any help. I have uploaded the code and a wiring diagram.
#include <Servo.h>
Servo myservo;
const byte Button = 3;
const byte Sensor = 5;
const byte Traffic_R = 9;
const byte Traffic_G = 10;
const byte Traffic_B = 11;
const byte Follower = A5;
const byte ServoPin = A4;
enum vtlStates {TrafficState1, TrafficState2, TrafficState3, TrafficState4, TrafficState5, TrafficState6, TrafficState7};
enum vpStates {pState1, pState2, pState3, pState4};
// enum assigns values to each state (starting from value 0)
vtlStates tlState; //create a variable tlState
vpStates pState; //create a variable pState
unsigned long SetupLight, Brightness, SetupFollower, CountStart, CountElapsed, tl4Start, tl4Elapsed, Wait6Start, Wait6Elapsed, tl6Start, tl6Elapsed, tl7Start, tl7Elapsed ;
int ButtonState = 0;
int LoopState = 0;
int LoopLight = 0;
int LoopFollower = 0;
int CountFollower = 0;
int BoomGateState = 0;
int Count = 0;
int CountDelay = 100;
int tl4Delay = 2000;
int Wait6Delay = 1000;
int Wait6Count = 0;
int tl6Delay = 3000;
int tl7Delay = 1000;
void setup() {
Serial.begin(9600);
myservo.attach(ServoPin);
pinMode(Button, INPUT);
pinMode(Sensor, INPUT);
pinMode(Traffic_R, OUTPUT);
pinMode(Traffic_G, OUTPUT);
pinMode(Traffic_B, OUTPUT);
pinMode(Follower, INPUT);
pinMode(ServoPin, OUTPUT);
tlState = TrafficState1;
SetupLight = analogRead(Sensor);
SetupFollower = analogRead(Follower);
myservo.write(0);
}
void loop() {
bool ButtonStateOpp = digitalRead(Button);
LoopLight = analogRead(Sensor);
LoopFollower = analogRead(Follower);
if (ButtonStateOpp == LOW) {
ButtonState = -~ButtonStateOpp;
}
if (LoopLight < SetupLight - 50) {
LoopState = 1;
Brightness = 128; // Night
}
else {
LoopState = 0;
Brightness = 0; // Day (255 is off)
}
if (Wait6Count == 0) {
Wait6Start = millis();
}
if (ButtonState == 1 && tlState == TrafficState1) {
analogWrite(Traffic_R, Brightness);
if (Wait6Count == 0) {
Wait6Count++;
}
Wait6Elapsed = millis() - Wait6Start;
if (Wait6Elapsed > Wait6Delay) {
tl6Start = millis();
Wait6Count = 0;
tlState = TrafficState6;
}
}
// Serial.print("A5 - ");
// Serial.println(analogRead(A5));
// Serial.print("A4 - ");
// Serial.println(analogRead(A4));
// Serial.print("A3 - ");
// Serial.println(analogRead(A3));
// Serial.print("A2 - ");
// Serial.println(analogRead(A2));
// Serial.print("A1 - ");
// Serial.println(analogRead(A1));
// Serial.print("A0 - ");
// Serial.println(analogRead(A0));
Serial.print("SetupL - ");
Serial.println(SetupLight);
Serial.print("LoopL - ");
Serial.println(LoopLight);
// Serial.print("milli - ");
// Serial.println(millis());
// Serial.print("Start - ");
// Serial.println(Wait6Start);
// Serial.print("Wait6 - ");
// Serial.println(Wait6Count);
// Serial.print("SetupF - ");
// Serial.println(SetupFollower);
// Serial.print("LoopF - ");
// Serial.println(LoopFollower);
// Serial.print("Gate - ");
// Serial.println(BoomGateState);
// Serial.print("Servo - ");
// Serial.println(myservo.read());
// Serial.print("State - ");
// Serial.println(tlState + 1);
// Serial.print("Button - ");
// Serial.println(ButtonState);
Serial.println(" ");
switch (tlState) {
case TrafficState1: {
myservo.write(0);
analogWrite(Traffic_R, Brightness);
analogWrite(Traffic_G, 255);
analogWrite(Traffic_B, 255);
if ((myservo.read() == 0 && ButtonState == 0) && (SetupFollower + 10 < LoopFollower || LoopFollower < SetupFollower - 10)) {
tlState = TrafficState2;
}
}
break;
case TrafficState2: {
if (Count % 2) {
analogWrite(Traffic_R, Brightness);
CountElapsed = millis() - CountStart;
if (CountElapsed > CountDelay) {
CountStart = millis();
Count++;
}
}
else {
analogWrite(Traffic_R, 255);
CountElapsed = millis() - CountStart;
if (CountElapsed > CountDelay) {
CountStart = millis();
Count++;
}
}
if (myservo.read() != 90) {
myservo.write(myservo.read() + 1);
}
if (myservo.read() == 90) {
BoomGateState = 1;
tlState = TrafficState3;
}
}
break;
case TrafficState3: {
analogWrite(Traffic_R, 255);
analogWrite(Traffic_G, Brightness);
analogWrite(Traffic_B, 255);
if (BoomGateState = 1 && (SetupFollower + 5 < LoopFollower || LoopFollower < SetupFollower - 5)) {
tl4Start = millis();
tlState = TrafficState4;
}
}
break;
case TrafficState4: {
if (Count % 2) {
analogWrite(Traffic_G, Brightness);
CountElapsed = millis() - CountStart;
if (CountElapsed > CountDelay) {
CountStart = millis();
Count++;
}
}
else {
analogWrite(Traffic_G, 255);
CountElapsed = millis() - CountStart;
if (CountElapsed > CountDelay) {
CountStart = millis();
Count++;
}
}
tl4Elapsed = millis() - tl4Start;
if (tl4Elapsed > tl4Delay) {
tl4Start = millis();
tlState = TrafficState5;
}
}
break;
case TrafficState5: {
analogWrite(Traffic_G, 255);
if (Count % 2) {
analogWrite(Traffic_R, Brightness);
CountElapsed = millis() - CountStart;
if (CountElapsed > CountDelay) {
CountStart = millis();
Count++;
}
}
else {
analogWrite(Traffic_R, 255);
CountElapsed = millis() - CountStart;
if (CountElapsed > CountDelay) {
CountStart = millis();
Count++;
}
}
if (myservo.read() != 0) {
myservo.write(myservo.read() - 1);
}
if (myservo.read() == 0 && ButtonState == 1) {
BoomGateState = 0;
if (Wait6Count == 0) {
Wait6Count++;
}
Wait6Elapsed = millis() - Wait6Start;
if (Wait6Elapsed > Wait6Delay) {
tl6Start = millis();
Wait6Count = 0;
tlState = TrafficState6;
}
}
else if (myservo.read() == 0 && ButtonState == 0) {
BoomGateState = 0;
SetupFollower = analogRead(Follower);
ButtonState = 0;
tlState = TrafficState1;
}
}
break;
case TrafficState6: {
analogWrite(Traffic_R, 255);
analogWrite(Traffic_B, Brightness);
tl6Elapsed = millis() - tl6Start;
if (tl6Elapsed > tl6Delay) {
tl7Start = millis();
tlState = TrafficState7;
}
}
break;
case TrafficState7: {
if (Count % 2) {
analogWrite(Traffic_B, Brightness);
CountElapsed = millis() - CountStart;
if (CountElapsed > CountDelay) {
CountStart = millis();
Count++;
}
}
else {
analogWrite(Traffic_B, 255);
CountElapsed = millis() - CountStart;
if (CountElapsed > CountDelay) {
CountStart = millis();
Count++;
}
}
tl7Elapsed = millis() - tl7Start;
if (tl7Elapsed > tl7Delay) {
SetupFollower = analogRead(Follower);
ButtonState = 0;
tlState = TrafficState1;
}
break;
}
}
}