Success!
Only error code now is the attach interrupt. I have searched and tried a few things but not found a solution.
I do appreciate all the help and advice given. Its a big learning curve.
#define ENCODER0PINA 20 // this pin needs to support interrupts
#define ENCODER0PINB 18 // interuppt
#define CPR 400 // encoder cycles per revolution
#define CLOCKWISE 1 // direction constant
#define COUNTER_CLOCKWISE 2 // direction constant
volatile long encoder0Position = 0; // variables modified by interrupt handler must be declared as volatile
volatile long interruptsReceived = 0;
short currentDirection = CLOCKWISE;// track direction: 0 = counter-clockwise; 1 = clockwise
long previousPosition = 0;// track last position to see if worth printing new output
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)
void setup()
{
Serial.begin(9600);
Serial.println("<Arduino is ready>");
pinMode(RPWM_Output, OUTPUT);
pinMode(LPWM_Output, OUTPUT);
pinMode(36, INPUT_PULLUP); //home switch
// inputs
pinMode(ENCODER0PINA, INPUT_PULLUP);
pinMode(ENCODER0PINB, INPUT_PULLUP);
attachInterrupt(20, RISING); // interrupts
Serial.begin (9600); // enable diagnostic output
Serial.println("\n\n\n");
Serial.println("Ready.");
analogWrite(LPWM_Output, 100); //move to home switch
digitalRead (36);
if (LOW);
Serial.println ("Switch closed."); //set encoder to zero, to be coded
delay (1000);
analogWrite(RPWM_Output, 100); //move to new start position, to be coded
}
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;
void onInterrupt(); // interrupt function needs to do as little as possible
int a = digitalRead(ENCODER0PINA);
int b = digitalRead(ENCODER0PINB); // read both inputs
if (a == b) // b is leading a (counter-clockwise)
{
encoder0Position--;
currentDirection = COUNTER_CLOCKWISE;
}
else
{
encoder0Position++; // a is leading b (clockwise)
currentDirection = CLOCKWISE;
encoder0Position = encoder0Position % CPR; // track 0 to 20000
interruptsReceived++; // track the number of interrupts
int sensorVal = digitalRead(36); //read the pushbutton value into a variable
Serial.println(sensorVal); //print out the value of the pushbutton
}
{
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)
{
int reversePWM = -(sensorValue - 511) / 2; // reverse rotation
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);
}
else
{
int forwardPWM = (sensorValue - 512) / 2; // forward rotation
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);
}
}
}
Error messages,
Arduino: 1.8.7 (Windows 8.1), TD: 1.44, Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
C:\Users\HP\Documents\Arduino\formatted\formatted.ino: In function 'void setup()':
C:\Users\HP\Documents\Arduino\formatted\formatted.ino:30:29: warning: invalid conversion from 'int' to 'void (*)()' [-fpermissive]
attachInterrupt(20, RISING); // interrupts
^
formatted:30:29: error: too few arguments to function 'void attachInterrupt(uint8_t, void (*)(), int)'
In file included from sketch\formatted.ino.cpp:1:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:150:6: note: declared here
void attachInterrupt(uint8_t, void (*)(void), int mode);
^
exit status 1
too few arguments to function 'void attachInterrupt(uint8_t, void (*)(), int)'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

