This is embarrassing. I know it was pseudo code, but I'm not sure how to declare "returning = false" (i.e., how it needs to be phrased and exactly where it should go.).
I'm not sure how to declare "returning = false" (i.e., how it needs to be phrased and exactly where it should go.).
Declare it as a global boolean variable
boolean returning = false;
Making progress, the servo rotates when button 1 is pressed but button 2 has no affect. I suspect it has to do with how I coded the positioning?
#include <Servo.h>
Servo myservo;
int position = 180;
const int button1Pin = 2;
const int button2Pin = 3;
int button1PushCounter = 0;
int button1State = 0;
int button2State = 0;
int lastButtonState = 0;
void setup() {
myservo.attach(9);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
}
void loop(){
boolean returning = false;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == HIGH){
myservo.write(180);
returning = false;
}
if (button2State == HIGH){
returning = true;
position = 180;
}
if (returning == true){
myservo.write(position);
delay (1000);
position-=1;
}
if (position == 0);
{
returning = false;
}
}
I really appreciate your help.
You have code that slowly decreases the position variable and writes it to the servo.
But you set position to 180 every loop if the button2State is high. I suspect it never has a chance to get below 179.
How bout you set the position to 180 if button1State is high and not if button2State is high?
Also, what do you want to happen if the user releases button2 before the servo gets to zero? stop moving where it is or keep moving to zero?
Thank you, I changed the criteria for button 2 to "position = 0;". It doesn't shake and button 1 works as it should, making a full rotation once the button is pressed and released. However, button 2 also operates in the same manner, rotating back once the button is pressed and released. I would like button 2 to only rotate as long as the button is being pressed (similar to a sketch I previously posted).
You are almost there.
If button2state is high, the only thing it should change is the retuning flag. But to stop when button 2 is released, you need an else portion that sets returning to false.
Edit: Oh, set position to 180 when button1State is high.
And you can experiment with different delay values.
And post code when you come back with changes. That way we are not guessing.
I'm trying, but this late I'm on fumes.
I tried adding an else statement below (highlighted with stars) but things aren't working out.
#include <Servo.h>
Servo myservo;
int position = 180;
const int button1Pin = 2;
const int button2Pin = 3;
int button1PushCounter = 0;
int button1State = 0;
int button2State = 0;
int lastButtonState = 0;
void setup() {
myservo.attach(9);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
}
void loop(){
boolean returning = false;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == HIGH){
myservo.write(180);
returning = false;
}
if (button2State == HIGH){
returning = true;
position = 0;
}
if (returning == true){
myservo.write(position);
delay (1000);
position-=1;
}
if (position == 0){
returning = false;
}
//***************************************
else(button2State == LOW){
returning = false;
}
//***************************************
*/
}
If this works out, I'll pull for the Cavs for the remainder of the playoffs.
button 2 also operates in the same manner, rotating back once the button is pressed and released. I would like button 2 to only rotate as long as the button is being pressed (similar to a sketch I previously posted).
That is the fault of how my pseudo program was written.
Try this logic instead
returning = false
position = 180
start of loop()
if button1 has become pressed
position = 180
servo.write(position)
returning = true
end if
if button2 is pressed and returning == true
servo.write(position)
delay a while
decrement position
if position == 0
returning = false
end if
end if
end of loop()
UKHeliBob's pseudo code looks great. Run with that.
Then if you want to try for extra credit, rather than using a delay, look at using micros like blink without delay.
I'm still at loss concerning the "else" portion of when button 2 is not being pressed. I'm not even sure where I currently have the statement is correct.
He and I were leading to a similar solution but from 2 different paths.
Use his algorithm. It is very concise and should do what you have described.
I have been using the pseudo code (if that is the algorithm you are referring to) but have no clue how to handle button 2.
Are you suggesting I omit the "else" portion? Even so, I'm not sure if that would affect the second condition (button 2).
The algorithm/pseudocode is in post 27.
UKHeliBob put into general statements all the logic to make this work.
It is fairly close to where you were.
Take that, turn it into actual code and try it. Or if it is not clear, ask.
I'm sorry to be so clueless, but as the code stands now the servo continues to make a full rotation once button 2 is pressed and released rather stop once the button is released.
#include <Servo.h>
Servo myservo;
int position = 180;
const int button1Pin = 2;
const int button2Pin = 3;
int button1PushCounter = 0;
int button1State = 0;
int button2State = 0;
int lastButtonState = 0;
void setup() {
myservo.attach(9);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
}
void loop(){
boolean returning = false;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == HIGH){
myservo.write(180);
returning = false;
}
if (button2State == HIGH){
returning = true;
position = 0;
}
if (returning == true){
myservo.write(position);
delay (1000);
position-=1;
}
if (position == 0){
returning = false;
}
}
Again, you are so close!
We are forcing you to drag yourself to success. Do not give up!
Go look again at UKHeliBob's pseudocode in post 27.
In the button1 if block, he does not just servo.write 180
He sets the position to 180 then servo.writes position.
this is important, as the position variable now contains the starting value for when button 2 starts to decrement it.
And in your button2 if block, you set position to zero. Take that out. You adjust the position variable with the -=1. If you have already set it to zero, it will have nowhere to go down from.
try those two changes and let us know what you see. UKHeliBob's logic combined several of your blocks into one big block for button 2 but I think that yours can work too.
And I think that the delay(1000) is too long which will make the return to zero very slow (3 minutes). You might try a smaller delay.
Thanks, I appreciate your patience.
I took tried your suggestions and combined some of the blocks but now button 2 does not perform any action when pressed. I also had to take out the delay in button 2 because it was for some reason adversely affecting the rotation of button 1. Instead of an IF statement for button 2, should it be a FOR or WHILE (although I have tried them without success)? Here is the current sketch:
#include <Servo.h>
Servo myservo;
int position = 180;
const int button1Pin = 2;
const int button2Pin = 3;
int button1PushCounter = 0;
int button1State = 0;
int button2State = 0;
int lastButtonState = 0;
void setup() {
myservo.attach(9);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
}
void loop(){
boolean returning = false;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == HIGH){
myservo.write(position);
returning = false;
}
if (button2State == HIGH){
returning = true;
// delay (10);
position-=1;
myservo.write(position);
}
if (position == 0){
returning = false;
}
}
wickedhurricane:
Thanks, I appreciate your patience.
you want something like this:
#include <Servo.h>
Servo myServo;
const byte servoPin = 9;
const byte buttonPinA = 2;
const byte buttonPinB = 3;
void setup()
{
Serial.begin(9600);
pinMode(buttonPinA, INPUT_PULLUP);
pinMode(buttonPinB, INPUT_PULLUP);
myServo.attach(servoPin);
myServo.write(0);
}
void loop()
{
if(buttonPressA())
{
Serial.println("moving to 180");
myServo.write(180);
}
else if(buttonPressB())
{
Serial.println("moving to 0");
myServo.write(0);
}
}
bool buttonPressA()
{
static unsigned long lastMillis = 0;
static byte lastState = HIGH;
const byte newState = digitalRead(buttonPinA);
if(newState != lastState)
{
if(millis() - lastMillis > 250)
{
if(newState == LOW) // pressed
{
lastMillis = millis(); // edited here
return true;
}
}
}
return false;
}
bool buttonPressB()
{
static unsigned long lastMillis = 0;
static byte lastState = HIGH;
const byte newState = digitalRead(buttonPinB);
if(newState != lastState)
{
if(millis() - lastMillis > 250)
{
if(newState == LOW) // pressed
{
lastMillis = millis();
return true;
}
}
}
return false;
}
You changed things outside of my 2 suggested changes.
Go back to the source in post 33.
In the button1 if block, you had
myservo.write(180);
I suggested using UKHeliBob's logic that set position and writes that position. This is what I had in mind.
position = 180;
myservo.write(position);
In button2 if block, you had
position = 0;
I suggested taking that out. This is what I had in mind.
//position = 0;
BulldogLowell also raises a good point in his code about debouncing. But you may not need it.
Thank you both for your suggestions, I'll try to incorporate.
vinceherman:
But you may not need it.
no. like fire insurance, you kinda want to have it.