Hi,
I am slowly learning and working on this code for a large servo with encoder.
I am getting the error expected initializer before if at the start of the void loop().
Thanks.
#define ENCODER0PINA 20 // this pin needs to support interrupts
#define ENCODER0PINB 18 // interuppt
#define CPR 20000 // encoder cycles per revolution
#define CLOCKWISE 1 // direction constant
#define COUNTER_CLOCKWISE 2 // direction constant
#define onInterrupt 3
// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;
// track direction: 0 = counter-clockwise; 1 = clockwise
short currentDirection = CLOCKWISE;
// track last position so we know whether it's worth printing new output
long previousPosition = 0;
int SENSOR_PIN = 0; // center pin of the potentiometer
int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
// Example 1 - Receiving single characters
char receivedChar;
boolean newData = false;
void setup()
{
Serial.begin(9600);
Serial.println("<Arduino is ready>");
pinMode(RPWM_Output, OUTPUT);
pinMode(LPWM_Output, OUTPUT);
pinMode(36, INPUT_PULLUP); //home switch
const byte interruptPin = 20;
// inputs
pinMode(ENCODER0PINA, INPUT_PULLUP);
pinMode(ENCODER0PINB, INPUT_PULLUP);
// interrupts
attachInterrupt(3, onInterrupt, RISING);
// enable diagnostic output
Serial.begin (9600);
Serial.println("\n\n\n");
Serial.println("Ready.");
//drive to home switch
analogWrite(LPWM_Output, 100);
{
if (digitalRead (36) == LOW)
{
Serial.println ("Switch closed.");
delay (1000);
}
{
analogWrite(RPWM_Output, 100);
}
void loop()
if (encoder0Position != previousPosition);
{
Serial.print(encoder0Position, DEC);
Serial.print("\t");
Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
Serial.print("\t");
Serial.println(interruptsReceived, DEC);
previousPosition = encoder0Position;
}
}
// interrupt function needs to do as little as possible
{ void onInterrupt()
// read both inputs
int a = digitalRead(ENCODER0PINA);
int b = digitalRead(ENCODER0PINB);
if (a = b )
{
// b is leading a (counter-clockwise)
encoder0Position--;
currentDirection = COUNTER_CLOCKWISE;
}
else
{
// a is leading b (clockwise)
encoder0Position++;
currentDirection = CLOCKWISE;
}
// track 0 to 20000
encoder0Position = encoder0Position % CPR;
// track the number of interrupts
interruptsReceived++;
//read the pushbutton value into a variable
{ int sensorVal = digitalRead(36);
//print out the value of the pushbutton
Serial.println(sensorVal);
{
int sensorValue = analogRead(SENSOR_PIN);
// sensor value is in the range 0 to 1023
// the lower half of it we use for reverse rotation; the upper half for forward rotation
if (sensorValue < 512)
{
// reverse rotation
int reversePWM = -(sensorValue - 511) / 2;
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);
}
else
{
// forward rotation
int forwardPWM = (sensorValue - 512) / 2;
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);
}
}
}
}