Hi
I know my code is a mess, but I'm a newbie with this.. I'm trying to make a train go to one station, wait for 5 sek and then go the other direction and stop for 5 sek. This will go on until I stop it. I cant seem to make the motor start again when it reaches one of the stations.
I'm using a ultrasonic distance sensor to tell me where the train is.
Any tips?
const int LEDGREEN = 5;
const int LEDYELLOW = 4;
const int LEDBLUE = 3;
const int LEDRED = 2;
const int POTMETER = A0;
const int START = 8;
const int TRIGGERPIN = 7;
const int ECOPIN = 6;
const int controlPin1 = 13;
const int controlPin2 = 9;
const int enablePin = 10;
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;
int STARTSwitchState = 0;
int previousSTARTSwitchState = 0;
int distanceThreshold = 0;
int cm = 0;
int inches = 0;
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
long readUltrasonicDistance(int TRIGGERPIN, int ECOPIN) {
pinMode(TRIGGERPIN, OUTPUT); // Clear the trigger
digitalWrite(TRIGGERPIN, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(TRIGGERPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGERPIN, LOW);
pinMode(ECOPIN, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(ECOPIN, HIGH);
}
void setup() {
pinMode(START, INPUT);
pinMode(POTMETER, INPUT);
pinMode(LEDYELLOW, OUTPUT);
pinMode(LEDGREEN, OUTPUT);
pinMode(LEDBLUE, OUTPUT);
pinMode(LEDRED, OUTPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(START);
STARTSwitchState = digitalRead(START);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
{
motorSpeed = analogRead(POTMETER) / 4;
if (STARTSwitchState != previousSTARTSwitchState) {
if (STARTSwitchState == HIGH) {
motorEnabled = !motorEnabled;
}
}
}
if (motorDirection = !motorDirection) {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
} else {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
}
if (motorEnabled == 1) {
analogWrite(enablePin, motorSpeed);
}
previousSTARTSwitchState = STARTSwitchState;
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
{
// set threshold distance to activate LEDs
distanceThreshold = 300;
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 6);
// convert to inches by dividing by 2.54
if (cm > distanceThreshold) {
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
}
if (cm <= distanceThreshold - 10 && cm > distanceThreshold - 50) {
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
}
if (cm <= distanceThreshold - 150 && cm > distanceThreshold - 200) {
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
}
if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 290) {
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
}
{
if (cm <= distanceThreshold - 20 && cm > distanceThreshold - 40) {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, LOW);
delay(5000);
digitalWrite(controlPin2, HIGH);
digitalWrite(controlPin1, LOW);
}
}
{
if (cm <= distanceThreshold - 260 && cm > distanceThreshold - 280) {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, LOW);
delay(5000);
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
}
}
}
}


