MEGA2560 with SyRen50 Phase 2

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.

Arrangement: Arduino has 2 PBs with pulldown resistors and 2 LEDs with series resistors. TX1 is connected to a serial terminal on the SyRen motor controller. The motor controller interfaces the power supply and the motor.

Problem: I want the motor to ramp up to a max speed (63 in this case) when button 1 is held but immediately ramp down when button 1 is released. Ideally, the motor would stop ramping up immediately upon button 1 release regardless of where it is in the ramp and start ramping down towards 0. If button 1 is pressed again before reaching 0, the motor would pick up ramping from its current speed back to it’s maximum provided button 1 is held sufficiently long. (Think of those silly helicopter games where the longer you hold the space bar, the higher it flies until it crashes into something; release the space bar and it starts to fall)

The next step, once this is figured out, would be for button 2 to do the same as button 1, just in the opposite direction. Then some equipment safety code to prevent a change of direction when the motor speed is != 0.

With the code I have below, the motor ramps to full speed regardless of how long I press button 1. If I press button 2 (again, I don’t want to use button 2 for this purpose but it’s the only thing I’ve been able to figure out) nothing happens unless the motor is at full speed, at which point it will ramp to 0.

If curious there is some additional detail at this link but I believe everything pertinent is here.
Previous Related Topic

Current Code In Its Entirety:

#include <SyRenSimplified.h>

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;


void setup()
{
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  SyRenTXPinSerial.begin(9600);

}

void loop()

{
  int power = 0;
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);

  BUTTONstate1 = digitalRead(BUTTON1);
  BUTTONstate2 = digitalRead(BUTTON2);


  for (power = 0; power <= 63 && BUTTONstate1 == HIGH; power ++)
  {
    SR.motor(power);
    delay(20);
  }

  for (power = 63; power >= 0 && BUTTONstate2 == HIGH; power --)
  {
    SR.motor(power);
    delay(20);
  }

}

If further curious, here is the endgame: I don’t know if it helps any but, at the end of what will probably be a very long exercise for me, I will be using this hardware to drive an observatory dome. As such, it will need to be able to go either direction by knowing the domes position (I plan to use an encoder) and how far it is from the desired position. It would then energize the motor in the appropriate direction, stopping at the right spot within some TBD margin of error. The ramping feature is intended to minimize mechanical wear and tear. I’m not looking for help on the whole shebang at this point just on these baby steps. I sincerely appreciate any further input!!!

SabertoothArduinoLibraries.zip (382 KB)

I’ve gone round and round now with multiple iterations of for, do, dowhile, and if loops. No luck. This is my latest code that isn’t working. As written, the motor automatically cycles from low to high. I need to get to bed but if someone could take a look, I would be forever grateful.

#include <SyRenSimplified.h>

SyRenSimplified SR;

const int BUTTON1 = 52; //ccw direction button
const int LED1 = 22;    //input confirmation light for B1
int BUTTONstate1 = 0;

void setup()
{
  pinMode(BUTTON1, INPUT);
  pinMode(LED1, OUTPUT);
  SyRenTXPinSerial.begin(9600);
}

void loop()

{
  int power = 0;
  digitalWrite(LED1, LOW);

  BUTTONstate1 = digitalRead(BUTTON1);

  for (power = 0; power <= 63 && digitalRead(BUTTON1) == HIGH; power ++)
  {
    digitalWrite(LED1, HIGH);
    SR.motor(power);
    delay(20);
  }

  for (power = 63; power >= 0 && digitalRead(BUTTON1) == LOW; power --)
  {
    SR.motor(power);
    delay(20);
  }

}

Just in case google brings anyone else here, I did resolve it (well 95% resolved).

#include <SyRenSimplified.h>

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;

void setup()
{
  pinMode(BUTTON1, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  SyRenTXPinSerial.begin(9600);
}

void loop()

{
  int power = 0;
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);

  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)
  {
    power--;
    SR.motor(power);
    delay(40);
  }

  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)
  {
    power++;
    SR.motor(power);
    delay(40);
  }
}

The only thing this code doesn’t let me do is re-engage the up ramp once I’ve removed the input until it has completely come to 0. Not a big deal really for this application.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.