Beginner here.
Equipent:
Arduino MEGA 2560
SyRen50 motor controller
5V Wall Wart for Arduino
eTopxizu 12V, 30A regulated supply for SyRen/Motor (this may be undersized (12V, 900W tarp motor) but it seems to be holding up for the moment.
Generic Incremental encoder
Generic 16character LCD
I am trying to run a dc motor with encoder feedback. Right now, my motor runs fine in both directions, ramping power up and down to minimize wear and tear. My encoder reads properly as confirmed via serialprint and printing to my lcd. The problem is that when the motor is running, the encoder stops counting, or at least printing. This will be a problem when I want to stop/start the motor based on it's current position as measured with the encoder.
I suspect what is happening is that the program is getting 'stuck' in the loop of the motor control code and won't update the encoder position until the motor loop is complete. I understand I may need to create state machines (going way back to college prgramming....) but I am unsure how to start that with my current application. Any help is greatly appreciated!
Full code here below. I have attached the atypical library file though I don't think it will be relavent.
#include <SyRenSimplified.h>
#include <LiquidCrystal.h>
// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 33, 32
LiquidCrystal lcd(12, 11, 10, 5, 4, 33, 32);
int backLight = 13;
int curPos = 0;
int tgtPos = 225;
SyRenSimplified SR;
const int BUTTON1 = 52; //ccw direction button
const int BUTTON2 = 53; //cw direction button
const int LED1 = 22; //input confirmation light for B1
const int LED2 = 23; //input confirmation light for B2
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
int power = 0;
volatile long temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
void setup()
{
pinMode(BUTTON1, INPUT); //ccw button
pinMode(BUTTON2, INPUT); //cw button
pinMode(LED1, OUTPUT); //input confirmation light for B1
pinMode(LED2, OUTPUT); //input confirmation light for B2
SyRenTXPinSerial.begin(9600);
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
lcd.begin(16, 2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
lcd.setCursor(0, 0); // set cursor to column 0, row 0 (the first row)
lcd.print("Tgt Pos:"); // set base text for screen begin
lcd.setCursor(13, 0);
lcd.print("DEG");
lcd.setCursor(0, 1);
lcd.print("Cur Pos:");
lcd.setCursor(13, 1);
lcd.print("DEG"); // set base text for screen end
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 //encoder input 1
pinMode(3, INPUT_PULLUP); // internal pullup input pin 3 //encoder input 2
//Setting up interrupt
attachInterrupt(0, ai0, RISING); //A rising pulse from encodenren activated ai0().
attachInterrupt(1, ai1, RISING); //B rising pulse from encodenren activated ai1().
}
void loop()
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
// Send the value of counter as input from encoder
if ( counter != temp ) {
temp = counter;
lcd.setCursor (9, 1);
lcd.print(counter);
}
//Run motor ccw while button is pressed, ramping to max speed and ramping down to 0 when released.
while (digitalRead(BUTTON1) == HIGH && digitalRead(BUTTON2) == LOW) //ramp up in positive direction with B1 pressed
{ //but return speed to zero if conflicting inputs
digitalWrite(LED1, HIGH);
if (power <= 100) {
power++;
}
SR.motor(power);
delay(40);
}
while (power > 0 && digitalRead(BUTTON1) == LOW || power > 0 && digitalRead(BUTTON1) == HIGH && digitalRead(BUTTON2) == HIGH)
{
power--;
SR.motor(power);
delay(40);
}
//Run motor cw while button is pressed, ramping to max negative speed and ramping up to 0 when released.
while (digitalRead(BUTTON2) == HIGH && digitalRead(BUTTON1) == LOW) //ramp up in negatvie direction with B2 pressed
{ //but return speed to zero if conflicting inputs
digitalWrite(LED2, HIGH);
if (power >= -100) {
power--;
}
SR.motor(power);
delay(40);
}
while (power < 0 && digitalRead(BUTTON2) == LOW || power < 0 && digitalRead(BUTTON1) == HIGH && digitalRead(BUTTON2) == HIGH)
{
power++;
SR.motor(power);
delay(40);
}
//display target position on LCD, will eventually be an input from another system and when target position and current position are different, will drive motor accordingly
lcd.setCursor (9, 0);
lcd.print(tgtPos);
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if (digitalRead(3) == LOW) {
counter++;
} else {
counter--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if (digitalRead(2) == LOW) {
counter--;
} else {
c
SabertoothArduinoLibraries.zip (382 KB)