#include <Servo.h>
Servo myservo;
int passFlag = 0;
int pos =0;
int const buttonfw =2;
int const buttonbw =3;
int maxservo = 0;
int minservo = 0;
int laser = 6;
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long time = 0; // the last time the output pin was toggled
unsigned long debounce = 200UL; // the debounce time, increase if the output flickers
void setup() { // put your setup code here, to run once:
pinMode(buttonfw, INPUT_PULLUP);
pinMode(buttonbw, INPUT_PULLUP);
myservo.attach(5);
pinMode(laser, OUTPUT);
minservo = 20;
maxservo = 45;
}
void loop() { // put your main code here, to run repeatedly:
if (passFlag == 0) {
for(pos = maxservo; pos > minservo; pos-=1){
myservo.write(pos);
delay(30);}
for (pos = minservo; pos < maxservo; pos+=1){
myservo.write(pos);
delay(30);}
passFlag++;
}
if (digitalRead(buttonfw) == LOW) {
myservo.write(pos++);
delay(50);
}
if (digitalRead(buttonbw) == LOW) {
myservo.write(pos--);
delay(50);
}
//if (digitalRead(buttonbw == HIGH && buttonfw == HIGH )) --this seems to work--
else {}
if (pos >= maxservo) {
pos = maxservo;
}
else if (pos <= minservo) {
pos = minservo;
}
reading = digitalRead(buttonbw && buttonfw);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce)
{
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
analogWrite(laser, state*70); //dunno why but over 70 turns High
previous = reading;
}