/* FallerCar turn around unit
* Input detecs a car and is blinded until sequence is finish
* FallerCar drives onto turntable.
* Turntable turns 180 degress and the car starts again
* Second servo moves a magnet close to the stop magnet in the table to neutralize it.
* After one second the turntable goes back to initial position and "unblinds" the sensor.
*
* LCD shows status of the sequence.
* Serial port sends status of the sequence.
*
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
*
*
*/
#include <Servo.h> // include the library code for servo:
Servo Turnservo; // create servo object to control a servo
Servo Startservo; // create servo object to control a servo
#include <LiquidCrystal.h> // include the library code for LCD:
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // initialize the library by associating any needed LCD interface pin
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // with the arduino pin number it is connected to
// Variabels for the first servo, that controls the turntable
int pos = 0; // variable to store the servo position
int MinPos = 10; // minimum servo angle in degrees
int MaxPos = 180; // maximum servo angle in degrees
int ServoSpeedLoad = 25; // delay in servospeed under load
int ServoSpeedEmpty = 25; // delay in servospeed empty
// Variables for the second servo, that controls the start magnet.
int SSpos = 0; // variable to store the stopservo position
int SSMinPos = 20; // minimum servo angle in degrees
int SSMaxPos = 90; // maximum servo angle in degrees
int SSServoSpeed = 25; // delay in servospeed
// I/O pins:
const int inputPin = 8; // the number of the input pin
int InputState = 0; // variable for reading the input status
const int ledPin1 = A0; // the number of the LED1 pin (sequence running)
const int ledPin2 = A1; // the number of the LED2 pin (table turning)
const int ledPin3 = A2; // the number of the LED3 pin (pause)
int FCInPause = 2000; // Pause for Faller car entering turntable
int FCOutPause = 1000; // Pause for Faller car leaving turntable
void setup() {
pinMode(inputPin, INPUT_PULLUP); // initialize the input pin as an input with pullup:
pinMode(ledPin1, OUTPUT); // initialize the LED1 pin as an output:
pinMode(ledPin2, OUTPUT); // initialize the LED2 pin as an output:
pinMode(ledPin3, OUTPUT); // initialize the LED3 pin as an output:
Serial.begin(9600); // start serial port
Serial.println("Startup"); // Send status to serial port
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.setCursor(0, 0); // Goto pos 0 line 0
lcd.print(" Faller Car "); // Print start up message to the LCD.
lcd.setCursor(0, 1); // Goto pos 0 line 2
lcd.print(" Turntable "); // Print start up message to the LCD.
Turnservo.attach(9); // attaches the servo on pin 9 to the servo object
Turnservo.write(MaxPos); // tell servo to go to reference position
delay (1500); // wait 1,5 second
Turnservo.detach(); // detach servo to supress stutter
Startservo.attach(10); // attaches the servo on pin 10 to the servo object
Startservo.write(SSMaxPos); // tell servo to go to reference position
delay (1500); // wait 1,5 second
Startservo.detach(); // detach servo to supress stutter
digitalWrite(ledPin1, HIGH); // turn off LED1 (sequence running)
digitalWrite(ledPin2, HIGH); // turn off LED2 (table turning)
digitalWrite(ledPin3, HIGH); // turn off LED3 (pause)
lcd.clear(); // clear LCD
}
void loop() {
InputState = digitalRead(inputPin); // read the state of the pushbutton
if (InputState == LOW) { // check if the pushbutton is pressed. If it is, the InputState is LOW:
digitalWrite(ledPin1, LOW); // turn on LED1 (sequence running)
Serial.println("Car detected - Sequence start"); // Send status to serial port
digitalWrite(ledPin3, LOW); // turn on LED3 (pause)
Serial.println("Car entering turntable"); // Send status to serial port
lcd.setCursor(0, 0); // Goto pos 0 line 0
lcd.print("Car entering "); // Send status message to the LCD.
lcd.setCursor(0, 1); // Goto pos 0 line 1
lcd.print("turntable "); // Send status message to the LCD.
delay(FCInPause); // wait for FallerCar to enter the turntable
digitalWrite(ledPin3, HIGH); // turn off LED3 (pause)
Turnservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.println("Table turning"); // Send status to serial port
lcd.setCursor(0, 0); // Goto pos 0 line 0
lcd.print("Table turning "); // Send status message to the LCD.
lcd.setCursor(0, 1); // Goto pos 0 line 1
lcd.print("Pos: "); // Send status message to the LCD.
{
digitalWrite(ledPin2, LOW); // turn on LED2 (table turning)
for (pos = MaxPos; pos >= MinPos; pos -= 1) { // goes from maximum servo angle to minimum servo angle in 1 degree steps
Turnservo.write(pos); // tell servo to go to position in variable 'pos'
delay(ServoSpeedLoad); // waits XXms for the servo to reach the position
Serial.print("Pos = "); // Send status to serial port
Serial.print(pos); // Send position to serial port
Serial.println(" deg."); // Send status to serial port
lcd.setCursor(9, 1); // Goto pos 6 line 1
lcd.print(" "); // Clear field for position
lcd.setCursor(9, 1); // Goto pos 6 line 1
lcd.print(pos); // Send actual position to LCD
}
digitalWrite(ledPin2, HIGH); // turn off LED2 (table turning)
Turnservo.detach(); // detach servo to supress stutter
}
Startservo.attach(10); // attaches the servo on pin 10 to the servo object
Serial.println("Table turning"); // Send status to serial port
lcd.setCursor(0, 0); // Goto pos 0 line 0
lcd.print("Start servo turn"); // Send status message to the LCD.
lcd.setCursor(0, 1); // Goto pos 0 line 1
lcd.print("Pos: "); // Send status message to the LCD.
{
for (SSpos = SSMaxPos; SSpos >= SSMinPos; SSpos -= 1) { // goes from maximum servo angle to minimum servo angle in 1 degree steps
Startservo.write(SSpos); // tell servo to go to position in variable 'SSpos'
delay(SSServoSpeed); // waits XXms for the servo to reach the position
Serial.print("Pos = "); // Send status to serial port
Serial.print(pos); // Send position to serial port
Serial.println(" deg."); // Send status to serial port
lcd.setCursor(9, 1); // Goto pos 6 line 1
lcd.print(" "); // Clear field for position
lcd.setCursor(9, 1); // Goto pos 6 line 1
lcd.print(SSpos); // Send actual position to LCD.
}
Startservo.detach(); // detach servo to supress stutter
}
digitalWrite(ledPin3, LOW); // turn on LED3 (pause)
Serial.println("Car leaving turntable"); // Send status to serial port
lcd.setCursor(0, 0); // Goto pos 0 line 0
lcd.print("Car leaving "); // Send status message to the LCD.
lcd.setCursor(0, 1); // Goto pos 0 line
lcd.print("turntable "); // Send status message to the LCD.
delay(FCOutPause); // wait for FallerCar to leave the turntable
digitalWrite(ledPin3, HIGH); // turn off LED3 (pause)
Startservo.attach(10); // attaches the servo on pin 10 to the servo object
Serial.println("Start servo turn"); // Send status to serial port
lcd.setCursor(0, 0); // Goto pos 0 line 0
lcd.print("Start servo turn"); // Send status message to the LCD.
lcd.setCursor(0, 1); // Goto pos 0 line 1
lcd.print("Pos: "); // Send status message to the LCD.
{
for (SSpos = SSMinPos; SSpos <= SSMaxPos; SSpos += 1) { // goes from minimum servo angle to maximum servo angle in 1 degree steps
Startservo.write(SSpos); // tell servo to go to position in variable 'SSpos'
delay(SSServoSpeed); // waits XX ms for the servo to reach the position
Serial.print("Pos = "); // Send status to serial port
Serial.print(SSpos); // Send position to serial port
Serial.println(" deg."); // Send status to serial port
lcd.setCursor(9, 1); // Goto pos 6 line 1
lcd.print(" "); // Clear field for position
lcd.setCursor(9, 1); // Goto pos 6 line 1
lcd.print(SSpos); // Send actual position to LCD.
}
Startservo.detach(); // detach servo to supress stutter
Turnservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.println("Table turning"); // Send status to serial port
lcd.setCursor(0, 0); // Goto pos 0 line 0
lcd.print("Table turning "); // Print first line
lcd.setCursor(0, 1); // Goto pos 0 line 1
lcd.print("Pos: "); // Print second line
}
{
digitalWrite(ledPin2, LOW); // turn on LED2 (table turning)
for (pos = MinPos; pos <= MaxPos; pos += 1) { // goes from minimum servo angle to maximum servo angle in 1 degree steps
Turnservo.write(pos); // tell servo to go to position in variable 'pos'
delay(ServoSpeedEmpty); // waits XX ms for the servo to reach the position
Serial.print("Pos = "); // Send status to serial port
Serial.print(pos); // Send position to serial port
Serial.println(" deg."); // Send status to serial port
lcd.setCursor(9, 1); // Goto pos 6 line 1
lcd.print(" "); // Clear field for position
lcd.setCursor(9, 1); // Goto pos 6 line 1
lcd.print(pos); // Send actual position to LCD.
}
Turnservo.detach(); // detach servo to supress stutter
digitalWrite(ledPin2, HIGH); // turn off LED2 (table turning)
}
digitalWrite(ledPin1, HIGH); // turn off LED1 (sequence running)
Serial.println("Sequence end"); // Send status to serial port
lcd.clear(); // clear LCD
}
else {
lcd.setCursor(0, 0); // Goto pos 0 line 0
lcd.print("Waiting for next"); // Send status message to the LCD.
lcd.setCursor(0, 1); // Goto pos 0 line 2
lcd.print("car to turn "); // Send status message to the LCD.
}
}