Components and supplies
Resistor 10k ohm
Resistor 330 ohm
Motor Shield R3
Arduino UNO
Photo resistor
LED white
Project description
Code
Train Control
c_cpp
Please note that the pin numbers of LED and LDR has chanced. The pin numbers of motor control are fixed.
1/* 2 Train control 3 This code is developed to "test" drive 4 a model train between two gates made of a LED and LDR. 5 6 Parts required: 7 1 - arduino uno/mega or compatible 8 1 - arduino motor shield R3 9 2 - Led brite white (5mm) 10 2 - Ldr (A 9013 photo resistor 5mm) 11 2 - 10K resistor 12 2 - 220 Ohm resistor 13 1 - model train 14 15 Created 4 October 2016 16 by Kitemasters 17 18 This code is part of the public domain 19*/ 20// --------CONSTANTS (won't change)--------------- 21 22int sens_L_Pin = A4; // the pin number for ldr L 23int sens_R_Pin = A5; // the pin number for ldr R 24int led_L_Pin = 4; // the pin number for the L LED 25int led_R_Pin = 5; // the pin number for the R LED 26int motor_Pin = 3; // the pin number for the motor speed 27int brake_Pin = 9; // the pin number for the motor brake 28int direction_Pin = 12; // the pin number for the motor direction 29int current_sens_Pin = A0; // the pin number for the current sensor 30 31int read_sens_Interval = 200; // millisecs between reading sensors 32int motor_Acc_Interval = 100; // millisecs between acceleration steps 33int motor_Dec_Interval = 10; // millisecs between deceleration steps 34 35//------------ VARIABLES (will change)--------------------- 36 37unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop() 38unsigned long previous_sens_Millis = 0; // will store the last time sensors are read 39unsigned long previous_Acc_Millis = 0; // will store time of last acceleration step 40unsigned long previous_Dec_Millis = 0; // will store time of last deceleration step 41 42int sensLreading = 0; // declare variable and set value for left sensor reading 43int sensRreading = 0; // declare variable and set value for right sensor reading 44int max_Speed = 200; // declare variable and set value for maximum Speed (0 to 255) 45int Speed = 0; // declare variable and set value for current Speed (0 to 255) 46boolean direct = HIGH; // declare variable and set value for direction to HIGH (HIGH = left) 47 48#define STATE_START 1 // declare value 0 to be STATE_START 49#define STATE_RUN 2 // declare value 1 to be STATE_RUN 50#define STATE_DECELERATE 3 // declare value 2 to be STATE_DECELERATE 51#define STATE_TURN 4 // declare value 3 to be STATE_TURN 52#define STATE_ACCELERATE 5 // declare value 4 to be STATE_ACCELERATE 53 54int state = STATE_START; // declare variable "state" and set value to STATE_START 55 56//======================================== 57 58void setup() { 59 60 Serial.begin(9600); 61 62 pinMode(led_L_Pin, OUTPUT); // set led_L_Pin as output 63 pinMode(led_R_Pin, OUTPUT); // set led_R_Pin as output 64 pinMode(motor_Pin, OUTPUT); // set motor_Pin as output 65 pinMode(brake_Pin, OUTPUT); // set brake_Pin as output 66 pinMode(direction_Pin, OUTPUT); // set direction_Pin as output 67 pinMode(sens_L_Pin, INPUT); // set sens_L_Pin as input 68 pinMode(sens_R_Pin, INPUT); // set sens_R_Pin as input 69 pinMode(current_sens_Pin, INPUT); // set current_sens_Pin as input 70 71} 72//======================================== 73 74void loop() { 75 76 currentMillis = millis(); // store the latest value of millis() 77 read_sens(); // read the sensors 78 79 switch (state) // state switch 80 { 81 case STATE_START: 82 Start(); 83 break; 84 85 case STATE_ACCELERATE: 86 Accelerate(); 87 break; 88 89 case STATE_DECELERATE: 90 Decelerate(); 91 break; 92 93 case STATE_TURN: 94 Turn(); 95 break; 96 97 case STATE_RUN: 98 break; 99 } 100} 101//======================================== 102 103void read_sens() { 104 105 if (currentMillis - previous_sens_Millis >= read_sens_Interval) { // time is up, so read sensors 106 previous_sens_Millis = currentMillis; // because shooter and Koepel told me so.// save the time we last read sensors 107 sensLreading = analogRead(sens_L_Pin); // read left ldr (high value means the light is off or blockt) 108 sensRreading = analogRead(sens_R_Pin); // read right ldr (high value means the light is off or blockt) 109 110 if (sensLreading > 200 && direct == HIGH) { // if conditions are throu, the train reached left gate*** 111 digitalWrite(led_L_Pin, LOW); // turn left LED off 112 digitalWrite(led_R_Pin, HIGH); // turn right LED on 113 state = STATE_DECELERATE; // set state to "decelerate" 114 previous_Dec_Millis = currentMillis; // set previous_Dec_Millis to current time 115 } 116 117 if (sensRreading > 200 && direct == LOW) { // if conditions are throu, the train reached right gate*** 118 digitalWrite(led_R_Pin, LOW); // turn right LED off 119 digitalWrite(led_L_Pin, HIGH); // turn left LED on 120 state = STATE_DECELERATE; // set state to "decelerate" 121 previous_Dec_Millis = currentMillis; // set previous_Dec_Millis to current time 122 } 123 } 124} 125//======================================== 126 127void Start() { 128 129 digitalWrite(led_L_Pin, HIGH); // turn left led on 130 digitalWrite(brake_Pin, LOW); // Disengage the Brake 131 digitalWrite(direction_Pin, direct); // Establishes direction of the train 132 state = STATE_ACCELERATE; // set state to "accelerate" 133 previous_Acc_Millis = currentMillis; // set previous_Acc_Millis to current time 134} 135//======================================== 136 137void Accelerate() { 138 139 if (currentMillis - previous_Acc_Millis >= motor_Acc_Interval) { // check interval time 140 previous_Acc_Millis = currentMillis; // because shooter and Koepel told me so.//last time of acceleration step 141 Speed = Speed + 1; // add 1 to speed 142 analogWrite(motor_Pin, Speed); // send Speed to motor_Pin 143 if (Speed == max_Speed) { // if speed reach max speed 144 state = STATE_RUN; // set state to "run" 145 } 146 } 147} 148//======================================== 149 150void Decelerate() { 151 152 if (currentMillis - previous_Dec_Millis >= motor_Dec_Interval) { // check interval time 153 previous_Dec_Millis = currentMillis; // because shooter and Koepel told me so.//last time of acceleration step 154 Speed = Speed - 1; // subtract 1 of speed 155 analogWrite(motor_Pin, Speed); // send Speed to motor_Pin 156 if (Speed == 0) { // if speed reach 0 157 state = STATE_TURN; // set state to "turn" 158 } 159 } 160} 161//======================================== 162 163void Turn() { 164 165 if (direct == HIGH) { // flip direction 166 direct = LOW; 167 digitalWrite(direction_Pin, direct); // establishes right direction of train 168 } 169 else { 170 direct = HIGH; 171 digitalWrite(direction_Pin, direct); // establishes left direction of train 172 } 173 state = STATE_ACCELERATE; // set state to "accelerate" 174 previous_Acc_Millis = currentMillis; // set previous_Acc_Millis to current time 175} 176//========================================END 177
Downloadable files
scematic
scematic
scematic
scematic
Comments
Only logged in users can leave comments
Masters
0 Followers
•0 Projects
+2
Work attribution
0