Contact closure pushbutton help

Hello,

I'm new to this forum and somewhat new to coding in general, but have a basic understanding of Java and C++.
I'm trying to control a 21A DC motor using a SyRen 25 driver by Dimension Engineering, which includes its own library specifically made for use with Arduino. The code I posted works in some regard, in that the motor does spin when I push and hold the different contact closure buttons, but it ramps up and down while pressing any of the given buttons. I'd like to have the motor spin at a designated speed and hold that speed when the corresponding button is pushed and held down, ramping up in the process (to avoid current spikes) and then ramping down to 0 (stop) when no buttons are pushed.

I'm also hoping to include a section where if multiple buttons are pressed, it ignores all but the lowest speed button. Can anyone help me with this? The code works but is missing something to keep it spinning at the peak speed when the button is held down.

I'm using very basic contact closure buttons that are tied to ground through a 10k resistor. If there's any other info I'm missing, let me know?

Thanks in advance for your help and guidance!

Here's a link to the SyRen25 driver:
https://www.dimensionengineering.com/products/syren25
The library can be downloaded here:
SyRen Arduino libraries

EarthquakeDoWhile.ino (2.26 KB)

Maybe it'd be helpful if I post the code directly rather than just the file:

// Earthquake Table Lo/Med/High program
// Modified from Sweep example Copyright (c) 2012 Dimension Engineering LLC


#include <SyRenSimplified.h>
 int Lpin = 3; 
 int Mpin = 4;
 int Hpin = 5; 
 
 int Lval;
 int Mval;
 int Hval;

 int ramp=0; //ramp will set the speed of the motor


SyRenSimplified SR; // We'll name the SyRen object SR.
                   // For how to configure the SyRen, see the DIP Switch Wizard for
                   //   http://www.dimensionengineering.com/datasheets/SyrenDIPWizard/start.htm
                   // Be sure to select Simplified Serial Mode for use with this library.
                   // This sample uses a baud rate of 9600.
                   //
                   // Connections to make:
                   //   Arduino TX->1  ->  SyRen S1
                   //   Arduino GND    ->  SyRen 0V
                   //   Arduino VIN    ->  SyRen 5V (OPTIONAL, if you want the SyRen to power the Arduino)
                   //
                   // If you want to use a pin other than TX->1, see the SoftwareSerial example.
                   //DIP switch setting should be [ON-OFF-ON-OFF-ON-ON] 
                                       
void setup()
{
 SyRenTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.
 pinMode(Lpin, INPUT); //low speed button
 pinMode(Mpin, INPUT); // medium speed button
 pinMode(Hpin, INPUT); // high speed button 
 
}

void loop()
{  
 Hval = digitalRead(Hpin); // buttons are connected between 5v and a 10k resistor to ground 
 Mval = digitalRead(Mpin); 
 Lval = digitalRead(Lpin);
 
do {
 delay(10);          
 ramp++; //ramp up the motor to prevent current spikes
 SR.motor(ramp); // speed up motor
} while (Hval==HIGH && ramp<127); 

do {
 delay(10);          
 ramp++;  
 SR.motor(ramp); // speed up motor
} while (Mval==HIGH && ramp<82);

do {
 delay(10);          
 ramp++;  
 SR.motor(ramp);// speed up motor
} while (Lval==HIGH && ramp<40);

// and now slow down the motor to zero when a button is released: 

do {
 delay(10);          
 ramp--;  
 SR.motor(ramp); // slow motor down to 0
} while (Hval==LOW && ramp >= 0);

do {
 delay(10);          
 ramp--;  
 SR.motor(ramp); // slow motor down to 0
} while (Mval==LOW && ramp >= 0);

do {
 delay(10);          
 ramp--;  
 SR.motor(ramp);// slow motor down to 0
} while (Lval==LOW && ramp >= 0);

}

I'm using very basic contact closure buttons that are tied to ground through a 10k resistor.

That sounds wrong. Please read http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

The do...while() structure always executes at least one time which is not desired. These should be changed to while() loops. You can compress the three deceleration blocks into one since the exit test is always the same condition while ramp is > 0.

I do have a version that uses a regular while loop with one condensed line for a ramp down to 0 (stop) but it didn't work for some reason. I'll be able to post that version and a few others on Monday when I'm back at my testing station.
More to come and sorry for breaking rules (?) not really sure what I did wrong but I didn't expect to get yelled at so severely on my first ever post.

Thanks for your understanding and guidance

but I didn't expect to get yelled at so severely on my first ever post.

You didn’t get yelled at.

not really sure what I did wrong

You didn’t read the rules about how to use this forum. You should post your code like it says in the rules we “hide” at the top of every forum section. You still haven’t corrected your mistake by editing the offending post. The forum software can mangle code and introduce errors.

Lol arduino forum can feel hostile but it is noy toooo bad afterall. To post your code you'll have to use the code tags like this

[code] here goes your code[ /code]

Bringamosa:
Lol arduino forum can feel hostile but it is noy toooo bad afterall. To post your code you'll have to use the code tags like this

Bringamosa: while your hack kinda sorta works, the grammaticaly correct way to imbed anything that confuses the code parser is to surround them with the tags:

[nobbc]any literal text here[/nobbc]

Will generate the desired:

[code]code here[/code] text without creating the actual code block.

Thank you for that. Explaining about the img tags using the code tags works fine. Using code tags to explain enjoy code tags does not work as intended indeed

Grumpy_Mike:
That sounds wrong. Please read http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

I've modified my posts to better suit the forum. Please be aware of your tone, Grumpy_Mike, as being corrected in a harsh manner like this does not foster a positive learning environment.

WattsThat:
The do...while() structure always executes at least one time which is not desired. These should be changed to while() loops. You can compress the three deceleration blocks into one since the exit test is always the same condition while ramp is > 0.

Here's another version of the code with a simplified statement for the ramp down function using a new variable called "logic". The motor driver still seemed to ignore the LOW values, as it would spin even when all buttons were unpressed.
I may need to get new buttons to be able to clearly identify what amount of resistance to tie to ground as pulldowns, as it might not be receiving the correct HIGH/LOW values. I might have myself a floating input situation even though I'm using 10k pulldowns. I'm trying to also write a function that tests and prints HIGH/LOW values for each button using Serial.print but the documentation for Serial.print isn't very clear and I've always struggled with it.

I have more versions that I've tried, will post more soon just need to take accurate notes of the motor behavior for each one.

Here's the link to the libraries again:
SyRen Arduino libraries

// Earthquake Table Lo/Med/High program


#include <SyRenSimplified.h>
  int Lpin = 3; 
  int Mpin = 4;
  int Hpin = 5; 
 
  int Lval;
  int Mval;
  int Hval;

  int ramp = 0;
  int logic = 0; 

SyRenSimplified SR; // We'll name the SyRen object SR.
                    // For how to configure the SyRen, see the DIP Switch Wizard for
                    //   http://www.dimensionengineering.com/datasheets/SyrenDIPWizard/start.htm
                    // Be sure to select Simplified Serial Mode for use with this library.
                    // This sample uses a baud rate of 9600.
                    //
                    // Connections to make:
                    //   Arduino TX->1  ->  SyRen S1
                    //   Arduino GND    ->  SyRen 0V
                    //   Arduino VIN    ->  SyRen 5V (OPTIONAL, if you want the SyRen to power the Arduino)
                    //
                    // If you want to use a pin other than TX->1, see the SoftwareSerial example.
                    //DIP switch setting should be [ON-OFF-ON-OFF-ON-ON] 
                                        
void setup()
{
  SyRenTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.
  pinMode(Lpin, INPUT); //low speed button
  pinMode(Mpin, INPUT); // medium speed button
  pinMode(Hpin, INPUT); // high speed button 
  
SR.motor(ramp);
}


void loop()
{  
  Hval = digitalRead(Hpin);
  Mval = digitalRead(Mpin);
  Lval = digitalRead(Lpin);

  if(Hval==LOW && Mval==LOW && Lval==LOW){
    logic=0;
  }
  else(Hval==HIGH || Mval==HIGH || Lval==HIGH){
    logic=1;
  }
  
  while(logic==1 && Hval==HIGH && ramp<127) {
  ramp++;
  SR.motor(ramp);  // Ramp up to full speed 127
  delay(12); 
  }

  while(logic==1 && Mval==HIGH && ramp<80) {
  ramp++;
  SR.motor(ramp);  // Ramp up to medium speed 80
  delay(12); 
  }

  while(logic==1 && Lval==HIGH && ramp<45) {
  ramp++;
  SR.motor(ramp);  // Ramp up to low speed 45
  delay(12); 
  }

  while(logic==0 && ramp>0){
    ramp--;
  }

  Serial.print("logic= "); 
  Serial.println(logic);
  }

0hm_Mic_0dd:
Please be aware of your tone, Grumpy_Mike, as being corrected in a harsh manner like this does not foster a positive learning environment.

How old are you? You are not one of these snowflakes people are you?
Did your school have the ethos of you never being wrong only "differently correct" and there were no wrong answers. Well this is the real world where there are real wrong answers.

Look at this code:-

if(Hval==LOW && Mval==LOW && Lval==LOW){
    logic=0;
  }
  else(Hval==HIGH || Mval==HIGH || Lval==HIGH){
    logic=1;
  }

Now if the first logic statement is not true then the second statement has to be true. So you can use just:-

if(Hval==LOW && Mval==LOW && Lval==LOW){
    logic=0;
  }
  else{
    logic=1;
  }

This code:-

while(logic==0 && ramp>0){
    ramp--;
  }

can be replaced by:-

if(logic==0) ramp = 0;

Serial.print but the documentation for Serial.print isn't very clear and I've always struggled with it.

In the setup function have a :-

Serial.begin(9600);

Then when you want to print something, say in the loop function after you read the switches, use

Serial.print("switch Hval ");
Serial.print( Hval); 
Serial.print(" - switch Mval ");
Serial.print( Mval);
Serial.print(" - switch Lval ");
Serial.println( Lval);

This will print 0 if it is LOW and 1 if it is HIGH.

I'm using 10k pulldowns.

That is fine.

Grumpy_Mike:
This code:-

while(logic==0 && ramp>0){

ramp--;
  }



can be replaced by:-


if(logic==0) ramp = 0;

No it cannot be replaced with that, it needs to ramp down to 0 to prevent current spikes. That is the whole reason I am using ramp --; and ramp ++; as ramp directly controls the speed of the motor itself.

0hm_Mic_0dd:
No it cannot be replaced with that, it needs to ramp down to 0 to prevent current spikes. That is the whole reason I am using ramp --; and ramp ++; as ramp directly controls the speed of the motor itself.

How does that work?
You're not using "ramp" in that loop.

0hm_Mic_0dd:
No it cannot be replaced with that,

I think it can; all that happens in the while() is that ramp decrements but you don't do anything with each new value. So you might as just let it be 0.

elvon_blunden:
I think it can; all that happens in the while() is that ramp decrements but you don't do anything with each new value. So you might as just let it be 0.

Ah I had forgotten to include SR.motor(ramp); within the chunk in that version! Fixing it now, will check for functionality...

That was the problem all along! I had a feeling it was something very simple I was missing...
It works yay!
Thanks everyone

Here is the final version, still planning to add a function that accounts for multiple buttons pressed at once:

// Earthquake Table Lo/Med/High program
// Modified from Sweep example Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.

#include <SyRenSimplified.h>
  int Lpin = 3; 
  int Mpin = 4;
  int Hpin = 5; 
 
  int Lval;
  int Mval;
  int Hval;

  int ramp = 0;
  int logic = 0; 

SyRenSimplified SR; // We'll name the SyRen object SR.
                    // For how to configure the SyRen, see the DIP Switch Wizard for
                    //   http://www.dimensionengineering.com/datasheets/SyrenDIPWizard/start.htm
                    // Be sure to select Simplified Serial Mode for use with this library.
                    // This sample uses a baud rate of 9600.
                    //
                    // Connections to make:
                    //   Arduino TX->1  ->  SyRen S1
                    //   Arduino GND    ->  SyRen 0V
                    //   Arduino VIN    ->  SyRen 5V (OPTIONAL, if you want the SyRen to power the Arduino)
                    //
                    // If you want to use a pin other than TX->1, see the SoftwareSerial example.
                    //DIP switch setting should be [ON-OFF-ON-OFF-ON-ON] 
                                        
void setup()
{
  SyRenTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.
  pinMode(Lpin, INPUT); //low speed button
  pinMode(Mpin, INPUT); // medium speed button
  pinMode(Hpin, INPUT); // high speed button 
  
SR.motor(ramp);
}


void loop()
{  
  Hval = digitalRead(Hpin);
  Mval = digitalRead(Mpin);
  Lval = digitalRead(Lpin);

  if(Hval==LOW && Mval==LOW && Lval==LOW){
    logic=0;
  }
  else{
    logic=1;
  }
  
  while(logic==1 && Hval==HIGH && ramp<127) {
  ramp++;
  SR.motor(ramp);  // Ramp up to full speed 127
  delay(12); 
  }

  while(logic==1 && Mval==HIGH && ramp<85) {
  ramp++;
  SR.motor(ramp);  // Ramp up to medium speed 80
  delay(12); 
  }

  while(logic==1 && Lval==HIGH && ramp<60) {
  ramp++;
  SR.motor(ramp);  // Ramp up to low speed 45
  delay(12); 
  }

  while(logic==0 && ramp>0){
    ramp--;
    delay(5);
    SR.motor(ramp); 
  }

   

  }

Maybe plan on having fewer globals and more constants.
And better formatting - the IDE's auto-format tool will help there.