Hi, sorry yet another newbee in trouble.
This program is an attempt at getting two gondolas (one each side) to go up a mountain and stop when being detected by a sensor. There is then supposed to be a delay before going back down again until the second gondola is detected.
The gondolas are physically arranged on opposite sides so that when one has reached the top, the other one is at the bottom.
The motor is being powered via a L293D shield to protect the Arduino Uno R3.
The motor isn't changing direction, so I guess the if loops aren't functioning and also it isn't stopping when I operate the end of travel by links to check function before fitting the actual sensors.
Realising that the sensors will lose their signal as the gondola moves away from them I have attempted to use lastState to remember until the sensor at the end of travel tells it to stop.
Any help would be much appreciated but please remember that I'm fairly new at this being a model train guy trying to make things more interesting for my grandchildren, so treat an old man gently please.
// 2 gondola model mountain cable car with 2 end travel sensors
// By Ken Nippard 31/9/19 try 2
// Allocate pin usage
int pwm = 3; // motor speed output signal
int Pd1 = 4; // cw limit of travel sensor
int Pd2 = 7; // ccw limit of travel sensor
int cw = 8; // cw motor direction signal
int ccw = 12; // ccw motor direction signal
// variable to save last sensor position
int lastPd1State;
int lastPd2State;
void setup()
{
pinMode (pwm, OUTPUT); // motor speed 0 - 255
pinMode (Pd1, INPUT_PULLUP));
pinMode (Pd2, INPUT_PULLUP);
pinMode (cw, OUTPUT); // motor direction
pinMode (ccw, OUTPUT); // motor direction
digitalWrite(Pd1, HIGH); // sensors go low on detect
digitalWrite(Pd2, HIGH);
} // end of setup
void loop()
{
lastPd1State == digitalRead (Pd1); // Check current sensor reading
lastPd2State == digitalRead (Pd2);
delay (2000);
if (lastPd1State == LOW) { // need to move in anticlockwise direction
digitalWrite (cw, LOW);
digitalWrite (ccw, HIGH);
analogWrite (pwm, 160);
while (digitalRead (lastPd2State) == HIGH); // not yet at end of travel
digitalWrite (ccw, LOW); //stop motor
delay (120000);
}
else if (lastPd2State == LOW) { // need to move in clockwise direction
digitalWrite (cw, HIGH);
digitalWrite (ccw, LOW);
analogWrite (pwm, 160);
while (digitalRead (lastPd1State) == HIGH);
digitalWrite (cw, LOW); //stop motor
delay (120000);
}
else { // starting with gondolas in mid position
digitalWrite (cw, HIGH);
digitalWrite (ccw, LOW);
analogWrite (pwm,160);
while (digitalRead (lastPd2State) == HIGH); // not yet at end of travel
digitalWrite (cw, LOW); // stop motor
delay (120000);
}
}