Go Down

Topic: MEGA2560 with SyRen50; Motor Only Rotates one Direction (Read 76 times) previous topic - next topic

fyrfytr310

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.

The libraries attached (SyRenSimplified.h) are supposed to ensure that Tx1 is used to send a signal to an input pin on the SyRen50 to regulate it's output and the motor speed/direction.  The first issue I had was random motor movements, either direction at any speed, but I think I've resolved that through experimentation.  I was leaving the MEGA plugged into my laptop while trying to operate the motor.  Once I disconnected the USB, the randomness ended.

I am trying to write a very basic code to energize a motor in one direction or another based on push buttons associated with each direction.  I also have LED's associated on outputs for each pushbutton just to ensure that the input is being read. 

My issue:  The motor only rotates when pressing button 2.  The associated LED for button 1 does illuminate when button 1 is pressed but the motor does not rotate.  I can force rotation in the opposite direction if I change the code for button 2 itself (remove the '-' sign in the power variable) but, obviously, that is less than ideal.

You should know that my experience with Arduinos is a half notch above beginner and even that experience is dated.  This is my first time working with serial.  This is also my first post here so let me know if I need to provide any more detail or present anything in another format.

Code in its entirety:

Code: [Select]
#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;
  BUTTONstate1 = digitalRead(BUTTON1);
  BUTTONstate2 = digitalRead(BUTTON2);
  if (BUTTONstate1 == HIGH)
  {
    digitalWrite(LED1, HIGH);
    power = 127;
  }
  if (BUTTONstate2 == HIGH)
  {
    digitalWrite(LED2, HIGH);
    power = -127;
  }
  else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    power = 0;
  }
  SR.motor(power);
}



groundFungus

Please post a schematic of your wiring.

How are the buttons wired?  Are there pulldown resistors on the button switch inputs?

Do you want the motor to only run while the button is pushed?

fyrfytr310

Please post a schematic of your wiring.

How are the buttons wired?  Are there pulldown resistors on the button switch inputs?

Do you want the motor to only run while the button is pushed?
Yes on both questions.

MarkT

Your code always sets the power to 0 if button2 is not pressed.  That's not what you meant.

Try this:
Code: [Select]

void loop()
{
  int power = 0;
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  if (digitalRead(BUTTON1))
  {
    digitalWrite(LED1, HIGH);
    power = 127;
  }
  if (digitalRead(BUTTON2))
  {
    digitalWrite(LED2, HIGH);
    power = -127;
  }
  SR.motor(power);
}

Note the default to off is global, not attached to just one button's logic...
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

fyrfytr310

Oh my....  you nailed it.

Thank you so much!!!!

groundFungus

#5
Feb 23, 2021, 08:05 pm Last Edit: Feb 23, 2021, 08:09 pm by groundFungus
I do not have the right library so I use a serial print to follow and print the value of power.

I changed the code a bit and now the value of power is 127 with button 1 pressed and -127 with button 2 pressed.  All I did was put else if instead of if for the button2 test.
Code: [Select]
void loop()

{
   int power;
   BUTTONstate1 = digitalRead(BUTTON1);
   BUTTONstate2 = digitalRead(BUTTON2);
   if (BUTTONstate1 == HIGH)
   {
      digitalWrite(LED1, HIGH);
      power = 127;
   }
   else if (BUTTONstate2 == HIGH)
   {
      digitalWrite(LED2, HIGH);
      power = -127;
   }
   else
   {
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      power = 0;
   }
   Serial.println(power);
   delay(500);  // so serial monitor won't flood
   //SR.motor(power);
}


The way that it was, the else was only attached to the second if so if the button2 was not pressed the value of power was set to 0 no matter the state of button1.

fyrfytr310

Well my next round of experimentation failed nicely...

The next step is to ramp the speed of the motor.  Start at value 0 (fully stopped) for either button, then add power in whichever direction (button 1 - ccw, button 2 - cw) incrementally over the next 10 seconds or so by holding the button.  Let go of the button, the motor stops.  Ideally it would automatically stop accelerating (adding value to 'power') at 127 or -127 (max speed) and then decelerate when the button is released.  

All I was trying to do with this round is make the motor ramp up upon button press and hold, then stop on button release.  What happens is the LEDs function as expected but the motor starts running as soon as 12V is applied.  Then it acts like it is getting conflicting instructions from the Arduino by jerking randomly when I press either button.

I tried using for loops as well ex; for (power = 0; power <=127; power ++)  but that didn't get me very far either.

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!!!

Full code here:

Code: [Select]
#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);
  if (BUTTONstate1 == HIGH)
  {
    digitalWrite(LED1, HIGH);
    power = power++;

    delay(20);
  }

  else if (BUTTONstate2 == HIGH)
  {
    digitalWrite(LED2, HIGH);
    power = power--;

    delay(20);


  }
  Serial.println(power);
  delay(100);
  SR.motor(power);
}

Go Up