Project guidance: how move a bar with a button

Hello everyone, I am new in this beautiful world and i doing an experiment for school. I have a bar that moves horizontally. This bar is operated by a button which has two limit once the press of the button bar goes far to the right. I press the button again and moves back to the left limit. I was able to control the motor with the button but I can not make it stop when you operate the limit. In conclusion I was 4: 1) limit right pull down action no limit left no 2) limit right pull down pull down action left no limit 3) limit right no action pull down limit left no 4) right limit no limit no action left pull down. Looking on the internet I felt that probably I have to enter a variable motion but I have no idea how to do.

Please help me.

Thanks to all.

G_W_H

P.S.: I can’t upload a jpg and i post the circuit on this link http://imagebin.ca/v/1lzUXDJcs18B

button_motor.ino (1.84 KB)

I find your post raises more questions than answers.

For the sake of others that may want to follow this post I’m attaching the picture that you link to.

KenF:
I find your post raises more questions than answers.

For the sake of others that may want to follow this post I’m attaching the picture that you link to.

How can I be any clearer? Tell me what information do you need?

PS: thanks for the photo post

How can I be any clearer?

You could explain what this gibberish means, and then explain what you actually want to do:

In conclusion I was 4: 1) limit right pull down action no limit left no 2) limit right pull down pull down action left no limit 3) limit right no action pull down limit left no 4) right limit no limit no action left pull down

I was able to control the motor with the button but I can not make it stop when you operate the limit.

A motor reversing limit switch setup.

motor-toggle.jpg

jremington:
You could explain what this gibberish means, and then explain what you actually want to do:

There are four states and three actions (two are limit (right and left) and the button to move the motor connected to the bar (action). The actions: 1) button limit switch right down (so the bar is still on the right) button motion is not pressed, limit switch button left is not pressed 3) the bar moves to the left so there are not buttons pressed 4) the bar moves to the end and press the left limit switch and stops. After I press the button for controlling the motor and bar again returns to the right limit switch.

I hope I explained better :smiley:
if I did not try to explain even better

zoomkat:
A motor reversing limit switch setup.

Can you explain? I do not see Arduino. I could do without Arduino? Anyway, I'm forced to use a button so I can not use a control on-off-on

I hope I explained better :smiley:

No, unfortunately you didn't. It might be more understandable if you stopped peppering your dialog with "action" and "state" where they don't belong.

Just try to describe in plain English what you want to achieve. Use phrases that your grandma would understand. Don't try to make things sound technical for the sake of it.

How do you operate the button? Hold it down for movement while held down, stop movement when released? Or is just a quick momentary push to start movement and movement continues after button is released?

It might be clearer if you POST YOUR CODE.

Henry_Best:
It might be clearer if you POST YOUR CODE.

Actually, he did (as an attachment in the first post). I’ve never looked at it as I didn’t understand what the heck he’s on about.

But, here it is. Slightly more understandable than his posts (but not a lot :slight_smile: )

#define supplyVolts 7.4 
#define motorVolts 6 
#define BUTTONF 8 //left limit button
#define BUTTONI 2 //right limit button



int pwmSinPin = 3;  
int pwmDesPin = 11;  
int dirSinPin = 12;  
int dirDesPin = 13;  

int iniziocorsa = 0;         // limit right
int azionamento = 7;     // action
int finecorsa = 0;           // limit left


int stato = HIGH;        //actual state
int lettura;             //reading
int precedente = LOW;    //previous state

unsigned long tempo = 0;            //time
unsigned long rimbalzamento = 200;   // debounce

void setup()
{
  pinMode(pwmSinPin, OUTPUT);
  pinMode(pwmDesPin, OUTPUT);
  pinMode(dirSinPin, OUTPUT);
  pinMode(dirDesPin, OUTPUT);
  setMotors(0, 0);
 
      pinMode(azionamento, INPUT);
      pinMode(BUTTONI, INPUT);
      pinMode(BUTTONF, INPUT);
}

void loop()
{
      finecorsa = digitalRead(BUTTONF);
      iniziocorsa = digitalRead(BUTTONI);

   if (finecorsa == HIGH)                //--- limit right   
      {setMotors (0,0);
      }
 //  if (iniziocorsa == HIGH)                //--- limit left     
   //   {setMotors (0,0);
     // }
    lettura = digitalRead(azionamento);
  if (lettura == LOW && precedente == HIGH && millis() - tempo > rimbalzamento) {
    if (stato == HIGH){
         stato = LOW;
               delay(50);
               setMotors (0, -255); 
    }

    else{
     stato = HIGH;
      delay(50);
      setMotors (0, 255);
    }
    tempo = millis();    
  }
  precedente = lettura;
}


void setMotors(int Sin, int Des)
{
   int vSin = abs(Sin) * motorVolts / supplyVolts;
   int vDes = abs(Des) * motorVolts / supplyVolts;
   int dSin = (Sin > 0);
   int dDes = (Des > 0);
   if (vSin < 50)
   {
     vSin = 0; 
   }
   if (vDes < 50)
   {
     vDes = 0; 
   }
   analogWrite(pwmSinPin, vSin);
   analogWrite(pwmDesPin, vDes);
   digitalWrite(dirSinPin, dSin);
   digitalWrite(dirDesPin, dDes);
}

KenF:
No, unfortunately you didn’t. It might be more understandable if you stopped peppering your dialog with “action” and “state” where they don’t belong.

Just try to describe in plain English what you want to achieve. Use phrases that your grandma would understand. Don’t try to make things sound technical for the sake of it.

That code does this video. As I would say to my grandmother(the number of buttons is in the video) :slight_smile: I press the button 2 and the motor moves. I press the button 1 and stops. I press again the button 2 and the engine goes in the opposite direction. I press the button 3 and the motor stops. If the engine goes in one direction (after pressing the button 2) pressing it the 2 button that changes the direction of movement. The last part I was able to do it but the first not.

zoomkat:
How do you operate the button? Hold it down for movement while held down, stop movement when released? Or is just a quick momentary push to start movement and movement continues after button is released?

Just a quick momentary push to start movement and movement continues after button is released.
If you see the video I was able to do it.

I translated the code in English. Maybe you understand something more. :smiley: The code does exactly what you see in the video

pulsante_motore_eng.ino (1.68 KB)

I did this maybe can help.

Thankyou

anyone can help me?

anyone can help me?

#7 below explains how to post code in a code box, which is best if you want people to look at it (if the code is too large for a code box, write code that just demonstrates the issue).

http://forum.arduino.cc/index.php/topic,148850.0.html

I posted the ino file. Anyway here’s the code:

#define supplyVolts 7.4 
#define motorVolts 6 
#define BUTTON1 8 //left limit button
#define BUTTON3 2 //right limit button



int pwmLeftPin = 3;  
int pwmRightPin = 11;  
int dirLeftPin = 12;  
int dirRightPin = 13;  

int limitSwitchRight = 0;         // limit right
int action = 7;     // action
int limitSwitchLeft = 0;           // limit left


int state = HIGH;        //actual state
int reading;             //reading
int previous = LOW;    //previous state

unsigned long time = 0;            //time
unsigned long debounce = 200;   // debounce

void setup()
{
  pinMode(pwmLeftPin, OUTPUT);
  pinMode(pwmRightPin, OUTPUT);
  pinMode(dirLeftPin, OUTPUT);
  pinMode(dirRightPin, OUTPUT);
  setMotors(0, 0);
 
      pinMode(action, INPUT);
      pinMode(BUTTON1, INPUT);
      pinMode(BUTTON3, INPUT);
}

void loop()
{
      limitSwitchRight = digitalRead(BUTTON1);
      limitSwitchLeft = digitalRead(BUTTON3);

    reading = digitalRead(action);
  if (reading == LOW && previous == HIGH && millis() - time > debounce) {
    if (state == HIGH){
         state = LOW;
               
               setMotors (0, -255); 
    }

    else{
     state = HIGH;
     
      setMotors (0, 255);
    }
    time = millis();    
  }
  previous = reading;
}



void setMotors(int Left, int Right) //This is an easy way to control the motor
{
   int vLeft = abs(Left) * motorVolts / supplyVolts;
   int vRight = abs(Right) * motorVolts / supplyVolts;
   int dLeft = (Left > 0);
   int dRight = (Right > 0);
   if (vLeft < 50)
   {
     vLeft = 0; 
   }
   if (vRight < 50)
   {
     vRight = 0; 
   }
   analogWrite(pwmLeftPin, vLeft);
   analogWrite(pwmRightPin, vRight);
   digitalWrite(dirLeftPin, dLeft);
   digitalWrite(dirRightPin, dRight);
}

the code has no errors

the code has no errors

Looking at the wiring arrangement you posted, it appears you are trying to power the motor from the arduino. I don't see an external power supply for the motor.

The power supply is external as the engine is 6V. In the program I used to make the design was not the correct power so to avoid confusion I have not inserted. The problem initially was what the Arduino was blocked. Then gave false commands and I solved by making a hard-reset as shown by Banzi with on post on the forum.

the problem for me is not the hardware but in software. If I press the button 1 and 2 or the motor does not move or moves slowly until one is released. So if I press either button 1 must be ignored. I'm trying to impose it by software with poor results. Do you have suggestions?

the code:

    void setup() {  
       
      
        pinMode(pwmLeftPin, OUTPUT);
  pinMode(pwmRightPin, OUTPUT);
  pinMode(directionLeftPin, OUTPUT);
  pinMode(directionRightPin, OUTPUT); 
  setMotors(0, 0);
  
      pinMode(BUTTONI, INPUT);    
      
      pinMode(BUTTONF, INPUT);     
      pinMode(BUTTONA, INPUT);    
    }  
      
    void loop() {  
      valI = digitalRead(BUTTONI);  
      valF = digitalRead(BUTTONF);
      valA = digitalRead(BUTTONA);
  
      if (valI == HIGH && valA == LOW && valF == LOW) {  
      setMotors(0,0); // off the engine

      } 
     
     if (valI== HIGH && valA == HIGH && valF == LOW) {  
       
delay(50);
valI = 0;  //I try to set valI = 0

  setMotors(0,128); //turn on the motor 
      }
      
     if ( valI == LOW && valA == HIGH && valF == LOW) { 
     
setMotors(0,128); //turn on the motor
 

} 
  
    if (valI == LOW && valA == HIGH && valF == HIGH) {  
setMotors(0,0);//off the engine 
      }
  
  if (valI == LOW && valA == LOW && valF == HIGH) {  
setMotors(0,0);//off the engine  
      }  
     
      else {  
        setMotors(0,0);   //off the engine
}  
     
    }

you may notice that I deleted the code in which we momentarily quick momentary push to start movement and movement after a continue button is released. I intend to put it back after.