Pages: [1]   Go Down
Author Topic: disable a button input unitll another pressed  (Read 427 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello every one, any suguestions, smiley

As you may see by my sketch, this has three buttons, foward, stop reverse, when press forward run till count is reached then stops, same when press reverse, one question if moves forward and stops when count reached , how can i prevent from moving forward again, and  the only direction allowed to move is reverse.


#include <Button.h>
#include <EEPROM.h>                  // 23/8/11


Button in = Button(4,PULLUP);        // keeps pin 4 high waitng for low to trip
Button halt = Button(5,PULLUP);      // keeps pin 5 high waiting for low o trip
Button out = Button(6,PULLUP);       // keeps pin 6 high waiting for low to trip

int pulsePin = 3;                  // hall effetc input pin 3
unsigned long counter = 0; 
unsigned long duration = 0; 
unsigned long timeout = 1000000; // in microseconds
unsigned long howfar = 400;       // how far to travel till stops motor
int value;

 

 void setup(){
EEPROM.write(1,howfar);    // 23/8/11 write value of howfar to eeprom loaction 1

value = EEPROM.read(1);   // read eeprom address 1 and call it value


   
pinMode (13,OUTPUT);                                                //  new 17/08/11  use led on pcb to warn that calibration about to start
pinMode(11,OUTPUT);                                                // changes direction of motor
pinMode(10,OUTPUT);                                                // turns main relay power on
pinMode(pulsePin, INPUT);       
digitalWrite(pulsePin, HIGH);                                       // enable the 20K pull-up resistor to steer the input pin to a HIGH reading.
Serial.begin(9600);                                                // open serial port
}

void loop(){

if(in.isPressed()){digitalWrite(10,HIGH);                          //   forward switch
digitalWrite(11,LOW) ;} 

if(halt.isPressed()){digitalWrite(10,LOW);}                          // stop switch

if(out.isPressed()){digitalWrite(10,HIGH);                         // reverse switch
digitalWrite(11,HIGH);}
                                             
                                               // calabration start here, eg move motor back till end stop reached, reset counter 2 , move motor till other end
                                                // stop reached, memorise total count, return motor back to start position,
                                               
duration = pulseIn(pulsePin, HIGH, timeout);
if (duration == 0) {   

Serial.println(""); 
} else {   

counter++;
Serial.print(counter);                                                         // so i can monitor count pulses etc in serial monitor
Serial.println("");


if (digitalRead(11) == HIGH  && (counter>=value)){                            // if count reached, checks where relays are
digitalWrite(11,LOW);                                                         // reverse polarty for 10ms then turns pwr relay off.
delay(10);                                                                   // need this to stop 1 pulse ove count to prevet run on
digitalWrite(10,LOW);
}
else 
{
if(digitalRead(11) == LOW  && (counter>=value)){                            // if count reached checks relays for polarity and switches
digitalWrite(11,HIGH);                                                       // them in reverse for 10ms, then turns pwr relay off.
delay(10);                                                                  // need this to stop over run from count that stops on
digitalWrite(10,LOW);};

if(counter>=value){                           //reset counter when distance reached
counter = 0;                                           

}}} }



 
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 51
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hard to control the device as presented - you mention end stops in the code - they would help you make decisions about which buttons should be usable at any given point. Without them, all you can do is make assumptions that you know where the device is. Keep a state variable telling you whether you're moving in or out and use your counter to tell you how far you've gone. When you get a button press, use counter to tell you whether you're at either end of the travelling range - if you're all the way forward, ignore additional forward pressing, same for back. This'll need your calibration routine to be implemented in setup though so you know your starting point.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you see something like this:
Code:
}}} }
in a sketch, then it is time to learn about and use the auto-format tool (ctrl-t) in the IDE.

When posting code, please use the "#" icon on the editor's toolbar/
Code:
#include <Button.h>
#include <EEPROM.h>                  // 23/8/11

Button in = Button(4,PULLUP);        // keeps pin 4 high waitng for low to trip
Button halt = Button(5,PULLUP);      // keeps pin 5 high waiting for low o trip
Button out = Button(6,PULLUP);       // keeps pin 6 high waiting for low to trip

int pulsePin = 3;                  // hall effetc input pin 3
unsigned long counter = 0;
unsigned long duration = 0;
unsigned long timeout = 1000000; // in microseconds
unsigned long howfar = 400;       // how far to travel till stops motor
int value;



void setup(){
  EEPROM.write(1,howfar);    // 23/8/11 write value of howfar to eeprom loaction 1

  value = EEPROM.read(1);   // read eeprom address 1 and call it value

  pinMode (13,OUTPUT);                                                //  new 17/08/11  use led on pcb to warn that calibration about to start
  pinMode(11,OUTPUT);                                                // changes direction of motor
  pinMode(10,OUTPUT);                                                // turns main relay power on
  pinMode(pulsePin, INPUT);       
  digitalWrite(pulsePin, HIGH);                                       // enable the 20K pull-up resistor to steer the input pin to a HIGH reading.
  Serial.begin(9600);                                                // open serial port
}

void loop(){

  if(in.isPressed()){
    digitalWrite(10,HIGH);                          //   forward switch
    digitalWrite(11,LOW) ;
  }

  if(halt.isPressed()){
    digitalWrite(10,LOW);
  }                          // stop switch

  if(out.isPressed()){
    digitalWrite(10,HIGH);                         // reverse switch
    digitalWrite(11,HIGH);
  }

  // calabration start here, eg move motor back till end stop reached, reset counter 2 , move motor till other end
  // stop reached, memorise total count, return motor back to start position,

  duration = pulseIn(pulsePin, HIGH, timeout);
  if (duration == 0) {   

    Serial.println("");
  }
  else {   

    counter++;
    Serial.print(counter);                                                         // so i can monitor count pulses etc in serial monitor
    Serial.println("");


    if (digitalRead(11) == HIGH  && (counter>=value)){                            // if count reached, checks where relays are
      digitalWrite(11,LOW);                                                         // reverse polarty for 10ms then turns pwr relay off.
      delay(10);                                                                   // need this to stop 1 pulse ove count to prevet run on
      digitalWrite(10,LOW);
    }
    else
    {
      if(digitalRead(11) == LOW  && (counter>=value)){                            // if count reached checks relays for polarity and switches
        digitalWrite(11,HIGH);                                                       // them in reverse for 10ms, then turns pwr relay off.
        delay(10);                                                                  // need this to stop over run from count that stops on
        digitalWrite(10,LOW);
      };

      if(counter>=value){                           //reset counter when distance reached
        counter = 0;                                           
      }
    }
  }
}
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  EEPROM.write(1,howfar);    // 23/8/11 write value of howfar to eeprom loaction 1

  value = EEPROM.read(1);   // read eeprom address 1 and call it value
What? You are writing 1 byte of a 4 byte variable to EEPROM, then reading that one byte and storing it in a 2 byte variable.

Why are you using EEPROM at all? Why are you switching sizes? Why are you not writing/reading properly?


Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 51
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On second thought, just use counter as a position indicator. Set it in calibration, then increment it on forward ticks of the hall sensor, decrement it when going backwards. On that point, consider using an interrupt to ensure you don't miss anything from the hall. 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1) using eeprom, i have not used before, it is the start of my calibration program, which the plan is to
 .
a) mover motor in reverse till no pulse detected.
b) reset couneter
c) move forward till no pulses detected.
d) store max pulses in memory.
e) then return to start position (where counter was reset)

 i will add a pot so if pot was at 50% the motor would only move 50%.
Logged

Pages: [1]   Go Up
Jump to: