Need help with arduino code for running motor with potentiometer and pushbutton.
I am running a motor in one direction for agricultural purposes. I am currently using some code I found to change the sporadic nature of the motor with a potentiometer and PWM. I found code which will use the potentiometer and this is successful and currently in use (built the whole 12v switch circuit using the Tip122). Now I want to be able to use a push button to retain the sporadic nature of the potentiometer when the push button is pushed once and have the motor be constant when the push button is pushed a second time.
I am stumped about how to do this, I am a beginner to the arduino code and trying to get better. I was wondering if someone might have an answer to how to program this. I've included the current code below.
int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}
Comments
9 years ago
Effectively this:
void(setup){
steadystate:= false;
.....
,.....
}
void loop () {
If PBpin==true then steadystate = !steadystate
While (PBpin==true) do {}
If steadystate==true do {
} else
{ //do not steady function
}
}
Answer 9 years ago
Once again, I appreciate your help. I had difficulty developing the code how you had it. Here is a set of example code that I have. It works perfectly. Do you see any problems with this code that may cause an issue?
const int pushButton = 2; // the number of the pushbutton pin
const int motorPin = 13; // the number of motorPin
// variables will change:
int buttonPushCounter = 0;
int buttonState = 0; // variable for reading the pushbutton status
int lastButtonState = 0;
int potPin = 3; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(motorPin, OUTPUT); // declare the ledPin as an OUTPUT
pinMode (potPin, INPUT);
pinMode (pushButton, INPUT);
Serial.begin(9600);
}
void loop(){
buttonState = digitalRead (pushButton);
if (buttonState != lastButtonState){
if (buttonState == HIGH){
buttonPushCounter++;
Serial.println ("on");
Serial.println ("number of button pushes: ");
Serial.println (buttonPushCounter);
}
else {
Serial.println ("off");
}
}
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
digitalWrite(motorPin, HIGH);
}
else
{
val = analogRead(potPin); // read the value from the sensor
digitalWrite(motorPin, HIGH); // turn the motorPin on
delay(val); // stop the program for some time
digitalWrite(motorPin, LOW); // turn the turn motorPin off
delay(val); // stop the program for some time
}}
Answer 9 years ago
If it works, it works !
Answer 9 years ago
Thank you Steve, I appreciate your quick response. I'm working this out right now.