Hello every one, any suguestions, ![]()
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;
}}} }