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
/* The analogWrite() function uses PWM, so if you want to change the pin you're using, be sure to use another PWM capable pin. On most Arduino, the PWM pins are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11. */ int led = 9; // the PWM pin the photo flash LED is attached to int led1 = 3; // PWM pin number for welding flash. // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); pinMode(led1, OUTPUT); } // the loop routine runs over and over again forever: void loop() { for (int i = 0; i < 100000; i++) { if (i % 240 == 0) { //For welding random flash analogWrite(led1, 0); delay(30); } if (i % 375 == 0) { //For welding random flash analogWrite(led1, 255); delay(20); analogWrite(led1, 0); delay(30); analogWrite(led1, 80); delay(5); analogWrite(led1, 0); delay(5); } if (i % 7500 == 0) { //For camera twin flash once in a while analogWrite(led, 255); delay(20); analogWrite(led, 0); delay(300); analogWrite(led, 255); delay(30); analogWrite(led, 0); delay(5000); } } }
int ledPolice1 = 10; // PWM pin number for police light1 int ledPolice2 = 11; // PWM pin number for police light2 int hazardLight = 9; // PWM pin number for vehicle hazard light int railwayCrossing1 =6; int railwayCrossing2 = 5; int m=1; void setup() { // declare pin 9 to be an output: pinMode(hazardLight, OUTPUT); pinMode(ledPolice1, OUTPUT); pinMode(ledPolice2, OUTPUT); pinMode(railwayCrossing1, OUTPUT); pinMode(railwayCrossing2, OUTPUT); } void loop() { analogWrite(railwayCrossing1,0); analogWrite(railwayCrossing2,0); analogWrite(hazardLight, 0); analogWrite(ledPolice1, 100); analogWrite(ledPolice2, 0); delay(200); analogWrite(ledPolice2, 100); analogWrite(ledPolice1, 0); delay(200); for (int i = 0; i < 25; i++) { analogWrite(hazardLight, i*4); delay(5); } railwayCrossingBlink(); } void railwayCrossingBlink() { if (m==1) { analogWrite(railwayCrossing2,0); delay(5); m=2; } else { analogWrite(railwayCrossing1,0); delay(5); m=1; } for (int i = 0; i < 100; i++) { analogWrite(m==1?railwayCrossing1:railwayCrossing2, i*2); delay(1); } }