As I learn programming i usually take smaller sketches and link them together to build larger ones.
My current project uses PWM to control a motor and and encoder to count its position.
I cannot figure out how to zero the encoder count when i use the pushbutton.
thanks john
```cpp
#include <Encoder.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Encoder myEnc(2, 3);
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
long oldPosition = -999;
void loop() {
encoder();
joystick();
zerohome();
}
void encoder() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
void joystick() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it for use with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
//delay(15); // waits for the servo to get there
}
void zerohome() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}