Please see code below.
I am reading 2 push buttons using no resistors.
One button makes the servo go one way and the other button makes it go the other way.
I was thinking I can use this to electronically change gears on my bike??
Ideally I will want to learn these angles and store and read them from memory, but I need to get the servo to feedback when its in the wrong position and drive back to it.. I tried to use the .read command but it didnt work?
#include <SimpleServo.h>
SimpleServo servoLeft; // Define left servo
#define ACTIVATED LOW
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin1 = 3; // the number of the pushbutton pin 1
const int ledPin = 1;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState1 = 0; // variable for reading the pushbutton status
int a = 90; //rotation angle //rotation angle
int pos = 0;
int astep = 15;
void setup() {
pinMode(ledPin, OUTPUT);
servoLeft.attach(0); // Set left servo to digital pin 10
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin,HIGH);
pinMode(buttonPin1, INPUT);
digitalWrite(buttonPin1,HIGH);
servoLeft.write(a);
}
void loop() { // Loop through motion tests
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
//servoLeft.write(a);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == ACTIVATED) {
// turn LED on:
digitalWrite(ledPin, HIGH);
forward(); // Example: move forward
delay(100); // Wait 2000 milliseconds (2 seconds)
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
if (buttonState1 == ACTIVATED) {
// turn LED on:
digitalWrite(ledPin, HIGH);
reverse(); // Example: move forward
delay(100); // Wait 2000 milliseconds (2 seconds)
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
// Motion routines for forward, reverse, turns, and stop
void forward() {
for(pos = a; pos <= a+astep; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servoLeft.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 milliseconds for the servo to reach the position
// SoftwareServo::refresh(); // required by SoftwareServo Library to sweep correctly
}
a +=astep;
}
void reverse() {
for(pos = a; pos >= a-astep; pos -= 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servoLeft.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 milliseconds for the servo to reach the position
}
a -=astep;
}
Adding this to the top of the loop works but its really noisy
void loop() { // Loop through motion tests
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
servoLeft.write(a-1);
servoLeft.write(a);
delay(15);