Hello all,
I have been working on a project that requires code to count the number of turns a drum of rope makes as its pulled out, then when a switch is made by the user to select 'reel in' i need number of pulses which are counted to be minused from the those counted as the rope was pulled out.
when the count is 0 (ie the rope pulled out is equal to that pulled in) the led pin (output pin to go low) shutting off the drum winch in motor and stopping drive.
basically any amount of rope can be pulled off, with the code counting the number of turns of the drum as its pulled out then when the user sets the reel in switch and the motor pulls the rope back in, the code counts down once the number of pulses is the same as the pulses counted out the motor shuts off.
the motor drive is the same as LED pin for code, I have used the serial mointer just for de bugging.
my issue is the code is very jumpy and i have tried varoius ways of trying to get it to work as i want even using the 'goto' command (sorry!!)
The pulses are produced from a proximity switch on the shaft of the drum, this is switching and pulsing cleanly.
My code has become rather messy and i am sorry for my lack of skills and having to trouble you all, No doubt someone will help out and i shall learn!!
Thank you
Radjit
//Constants/// :
const int ProxSensor = 2; // The i/p from shaft prox sensor 1 pulse per rev
const int reelPin = 8; // the pin the reel in enable switich is connected to
const int ledPin = 13; // the pin that the LED is attached to will be motor drive
const int rstPin = 7; // the reset indicator LED pin ready to go
// Variables ///:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int reelin = 0;
int windback = 0;
void setup() {
// initialize the button pin as a input:
pinMode(ProxSensor, INPUT);
pinMode(reelPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(19200);
}
void loop() {
// read the pushbutton input pin:
reelin = digitalRead(reelPin);
if (reelin == HIGH){
goto windback;
}
else {
buttonState = digitalRead(ProxSensor);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
// Serial.println("on");
// Serial.print("n pulsos: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
// Serial.println("off");
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
// *reel in minus code between here
windback:
digitalWrite(ledPin, HIGH);
buttonState = digitalRead(ProxSensor);
}
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, deincrement the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter--;
// Serial.println("on");
// Serial.print("n pulsos: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
// Serial.println("off");
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
if (buttonPushCounter == 0) {
digitalWrite(rstPin, LOW);
digitalWrite(ledPin, LOW);
delay (1000);
}
}