Arduino sketch used for testing the encoders on each motor
posted 1 year, 9 months ago by Alex JV

#include <elapsedMillis.h>

elapsedMillis timeElapsed;
int val=0;
int sensorPin = A0;
int interval = 1000;
int count=0;
int ledState=0;
int dVal=0;
int pdVal=0;

void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);


}

// the loop function runs over and over again forever
void loop() {
// digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
// delay(100); // wait for a second
// digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
// delay(100); // wait for a second
val = analogRead(sensorPin);
if (val > 500)
{
dVal=1;
}
else
{
dVal=0;
}
if (dVal!=pdVal)
{
count++;
pdVal=dVal;
}
if (timeElapsed > interval)
{
ledState = !ledState; // toggle the state from HIGH to LOW to HIGH to LOW ...
digitalWrite(13, ledState);
timeElapsed = 0;
Serial.println(count);
count=0; // reset the counter to 0 so the counting starts over...

}
}

Comments