Well its been almost 3 weeks since I started this little project and I have almost got it down pat but there is one part I need a little help with.
#include <Servo.h>
Servo myservo;
const int R1 = 9; // 1st Red LED
const int R2 = 10; // 2nd Red LED
const int G = 11; // Green Active LED
const int R = 12; // Red inactive LED
const int buttonPin = 2; // pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int running = 0;
int long interval = 500;
long previousMillis = 0;
int R1State = HIGH;
int pos = 0;
void setup() {
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(G, OUTPUT);
pinMode(R, OUTPUT);
pinMode(buttonPin, INPUT);
myservo.attach(7);
digitalWrite(buttonPin, HIGH);
}
// the loop routine runs and stops after last command
void loop() {
int buttonState = digitalRead(buttonPin);
// the switch has been activated and the program can run
if (buttonState == HIGH && running == 0) {
int running = 1;
tone(8, 550, 750); // play SOUND
digitalWrite(G, HIGH); // turn on GREEN LED
myservo.write(45);
delay(1500);
digitalWrite(G, LOW); // turn off GREEN LED
digitalWrite(R2, LOW); // turn the LED off
digitalWrite(R1, LOW); // turn the LED off
delay(250);
digitalWrite(R1, HIGH); // turn the LED on
digitalWrite(R2, HIGH); // turn the LED on
delay(2500);
buttonState = digitalRead(buttonPin);
while(buttonState == LOW && running == 1) {
// Timer used instead of delay to allow input to be read at the same time
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (R1State == HIGH){ // Switches lights over
digitalWrite(R1, LOW);
digitalWrite(R2, HIGH);
R1State = LOW;
}
else {
digitalWrite(R1, HIGH); // Switches lights over
digitalWrite(R2, LOW);
R1State = HIGH;
}
}
else {
// Terminates program and switches off lights if the trigger input is detected
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
digitalWrite(10, LOW); // turn the LED off
digitalWrite(9, LOW); // turn the LED off
tone(8, 550, 1000); // play SOUND
digitalWrite(12, HIGH); // turn on RED LED
delay(1500);
myservo.write(0);
digitalWrite(12, LOW); // turn off RED LED
int R1State = LOW;
delay(5000);
int running = 0;
break;
}
}
}
}
}
My problem is that I have tried to incorporate servo code into the sketch to make the boom gates on the level crossing lower and raise slowly as with real ones but I can only get them to go up to 45' and down to 0' in one hit instead of in increments.
I have tried to insert the servo code where I need it but it won't work how can I fix it?