hello
First i am sorry for any grammar mistakes but English is not my native languish
and if this is not the right sub please re-direct.
i am brand new too arduino and c+ but i do now some vba (didn't help )
so i need some help.
The project is too drive a model train between two gates.
So i build 2 gates with a LED and a LDR.
I choose this option cause i had the LDR and LED and i want too be able to move the gates
without damaging the track.
i have a arduino mega and arduino motorshield and connect them this way.
looks confusing but its just two pairs of a LED and a LDR.
and i started with the code;
/*
Train control
Drive the train between two gates made of a LED and a LDR.
*/
int ledL = 32;
int ledR = 34;
int sensL = A10;
int sensR = A11;
int train = 12;
int brake = 9;
int sensLreading = analogRead;
int sensRreading = analogRead;
String state = String("Stands");
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(ledL, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(train, OUTPUT);
pinMode(brake, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensLreading = analogRead(sensL);
sensRreading = analogRead(sensR);
delay(50);
sensLreading = analogRead(sensL);
sensRreading = analogRead(sensR);
Serial.println(sensLreading);
Serial.println(sensRreading);
Serial.println(state);
if (sensLreading > 200 && sensRreading > 200) {
digitalWrite(ledL, HIGH); //Turn left LED on
digitalWrite(train, HIGH); //Establishes left direction of train
digitalWrite(brake, LOW); //Disengage the Brake
analogWrite(3, 200); //Sets the speed of the train (255 is ful speed)
state = String("goLeft"); //Sets "state"
}
sensLreading = analogRead(sensL);
sensRreading = analogRead(sensR);
delay(50);
sensLreading = analogRead(sensL);
sensRreading = analogRead(sensR);
Serial.println(sensLreading);
Serial.println(sensRreading);
Serial.println(state);
if (sensLreading > 200 && state == "goLeft") {
digitalWrite(ledL, LOW); //Turn left LED off
digitalWrite(ledR, HIGH); //Turn right LED on
digitalWrite(train, LOW); //Establishes backward direction of train
digitalWrite(9, LOW); //Disengage the Brake
analogWrite(3, 200); //Sets the speed of the train (255 is ful speed)
state = String("goRight"); //Sets "state"
}
sensLreading = analogRead(sensL);
sensRreading = analogRead(sensR);
delay(50);
sensLreading = analogRead(sensL);
sensRreading = analogRead(sensR);
Serial.println(sensLreading);
Serial.println(sensRreading);
Serial.println(state);
if (sensRreading > 200 && state == "goRight") {
digitalWrite(ledR, LOW); //Turn right LED off
digitalWrite(ledL, HIGH); //Turn left LED on
digitalWrite(train, HIGH); //Establishes forward direction of train
digitalWrite(9, LOW); //Disengage the Brake
analogWrite(3, 200); //Sets the speed of the train (255 is ful speed)
state = String("goLeft"); //Sets "state"
}
}
as you can see the code for running the train comes later.
first i wanted too switch the gates.
at the start both LED`s are off so the first if statement turns on the left gate and (later)
send the train to the left gate and set "state" to "goLeft"
so when the train comes thru the gate, the value of the left LDR rise.
that should trigger the second if statement. so it turns off the left turns on the right LED
reverse the train and set state to goRight.
the Serial.println is to check what is happening.
problem 1
the led switch to right while the value of "sensL" didn`t rise above 400 or wathever value i try.
this is what serial monitor whrite;
878
886
Stands
878
886
goLeft
83 <<< (not > 400)
447
goRight
problem 2
not a problem just a question. is there a way to combine a string and the value in println
so it puts out "left 876" strings work very different than VBA and i cant find the right syntax
for this.
well that it for now
Al help is appreciated