Hi forum! So I'm trying to run a servo with a bumper switch, and I like what the photocell code does with the servo it does what I want. But I want to be able to do that same thing with a bumper switch instead of running it on a photocell. Could someone help me combine those two?
//include the Servo library in your sketch
#include <Servo.h>
//create a servo object called myServo
Servo myServo;
//create calibration variable
int cal;
void setup()
{
//take a calibration value right away
cal = analogRead(A0);
//attach myServo to pin 9
myServo.attach(9);
//write 90 to myServo as a pre-set angle
}
void loop()
{
//store the sensor value on A0 to the variable val
int val = analogRead(A0);
//if val is lress than the calibration value minus 50 for sensitivity write 160 to
//myServo, else write 20 to myServo
if(val < cal -50)
{
myServo.write(160);
}
else
{
myServo.write(20);
}
}
//include the servo library in your sketch
#include <Servo.h>
//Create a servo object called myServo
Servo myServo;
//create a global boolean variable to toggle
//when button is pressed, set it to true.
int buttonState = 0;
void setup()
{
//pinModes for H-Bridge control pins
myServo.attach(9);
//set pin 6 to INPUT_PULLUP, no need for a pullup resistor
pinMode(6,INPUT_PULLUP);
}
void loop()
{
//if buttonState is 0 (less than 1) toggle the state variable to
//the opposite of the current state (!state)
if(digitalRead(6)==0)
{
buttonState++;
delay(500);
}
//if state is false move myServo to 145, else 20.
if(buttonState==1)
{
myServo.write(40);
}
else if(buttonState==2)
{
myServo.write(60);
}
else if(buttonState==3)
{
myServo.write(120);
}
else if(buttonState >= 4)
{
myServo.write(20);
buttonState=0;
}
}