marklin-users.net community | Forum
Wish to join the discussion?! Login to your marklin-users.net community | Forum accountor Register a new forum account. Or Connect via Facebook
Edited by user 24 May 2016 21:22:09(UTC) | Reason: Not specified
const int motorPin = 9; int motorSpeed; void setup() { pinMode(motorPin, OUTPUT); } void loop() { analogWrite (motorPin, 14) ; //Play around with this number for slow rotation delay(23); //Some delay for motor to digest and process the command given analogWrite (motorPin, 7); //Just to avoid jerky motor movments delay(30); }
#include<Servo.h> /** * This sketch activates a servo by detecting an obstacle. The primary purpose of this program is to rotate a water crane's * arm when a loco arrives. Read the inline comments for more details * Infrared Sensor connected to A1 * A light bulb (not an LED) connected to OUTPUT 12 * A servo connected to PIN 9 * Author: Madhu Gubby * Date: 2020 */ Servo servo; int lastReading = 0; const int SENSOR1 = A1; const int SERVO = 9; const int LIGHT = 12; int sensor1Value = 0; boolean spoutOpen = false; void setup() { pinMode(LIGHT, OUTPUT); servo.attach(SERVO); servo.write(0); } void loop() { sensor1Value = analogRead(SENSOR1); delay(15); if (sensor1Value < 200) //When the train arrives and blocks the sensor { delay(4000); if (analogRead(SENSOR1) == lastReading) { // If it is a moving loco, the sensor might identify an obstacle but no need to activate the spout. //Here it is assumed that a loco takes 4 seconds to go past the sensor. However this needs to readjusted after fixing on the layout activateArm(); } } else { lastReading = analogRead(SENSOR1); } } void activateArm() { digitalWrite (LIGHT, HIGH); //Switch on the spout's light for (int i = 0; i <= 80; i++) { servo.write(i); delay(40); } delay(6000); //The spout's arm will be open for 6 seconds lastReading = analogRead(SENSOR1); deactivatArm(); // Rotate the arm back to the position that it was in before } void deactivatArm() { for (int i = 0; i <= 80; i++) { servo.write(80 - i); delay(40); } lastReading = 0; //set it to zero to mark the completion of a water filling cycle delay(1000); //wait for a second before switching off the light digitalWrite (LIGHT, LOW); //Switch off the spout's light }
Edited by user 03 October 2020 19:17:07(UTC) | Reason: typo