Pages: [1] 2 3   Go Down
Author Topic: Servo Control with three buttons. (resolved and working!!!)  (Read 619 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Arduino community,
       I am building a project that includes 3 buttons, lets say "buttonA", "buttonB", and "buttonC." My circuit will also include a servo.
I am attempting to write some code that says that "if buttonA is pressed, and if buttonB is pressed, than turn the servo in a specific pattern." However, "if buttonA is pressed, and if buttonC is pressed, than turn the servo in a different pattern.
       Obviously, I don't want to have to press both buttons "A and B" or "A and C" at the same time, simultaneosly. I want to be able to press one button(A) and then press either of the other buttons (B or C) a few seconds later.

How could I go about being able to press button(A) and then press either of the other buttons (B or C) a few seconds later without having to press them simultaneouly?

Here is my code...
Code:
#include <Servo.h>

Servo myservo; // creating myservo object
int buttonA = 2; // one side of buttonA attached to pin 2 and 10K resistor to ground, while other is on +5V
int buttonB = 3; // one side of buttonB attached to pin 3 and 10K resistor to ground, while other is on +5V
int buttonC = 4;  // one side of buttonC attached to pin 4 and 10K resistor to ground, while other is on +5V
int servoPin = 11;  //servo attached to pin 11
int AState = 0; // set AState
int BState = 0; // set BState
int CState = 0; // set CState

void setup()
{
myservo.attach(servoPin);
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(buttonC, INPUT);
}


void loop()
{AState = digitalRead(buttonA); // read and save to the variable "AState" the actual state of button
{BState = digitalRead(buttonB); // read and save to the variable "BState" the actual state of button
{CState = digitalRead(buttonC); // read and save to the variable "CState" the actual state of button
if (AState == HIGH){
  if (BState == HIGH){
    myservo.write(50);
    delay(1000);
    myservo.write(111);
    delay(1000);
}
  }
else
myservo.write(90);

if (AState == HIGH){
  if(CState == HIGH){
    myservo.write(130);
    delay(1000);
    myservo.write(70);
    delay(1000);
}
}
else
myservo.write(90);

}
}
}


* dispenser.png (285.39 KB, 1366x768 - viewed 7 times.)
« Last Edit: February 16, 2014, 12:12:39 pm by icecats » Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 98
Posts: 4389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Here is my code...

..... but where is your question?
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

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

Make some changes to your code. Put every { on a new line. Put NOTHING else after the {. Use Tools +Auto Format. Then, explain WHY your code looks like this:
Code:
void loop()
{
  AState = digitalRead(buttonA); // read and save to the variable "AState" the actual state of button
  {
    BState = digitalRead(buttonB); // read and save to the variable "BState" the actual state of button
    {
      CState = digitalRead(buttonC); // read and save to the variable "CState" the actual state of button
      if (AState == HIGH)
      {
        if (BState == HIGH)
        {
          myservo.write(50);
          delay(1000);
          myservo.write(111);
          delay(1000);
        }
      }
      else
        myservo.write(90);
Why does moving myservo depend on the state of both switches?
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the auto formatted code...
Code:
#include <Servo.h>

Servo myservo; // creating myservo object
int buttonA = 2; // one side of buttonA attached to pin 2 and 10K resistor to ground, while other is on +5V
int buttonB = 3; // one side of buttonB attached to pin 3 and 10K resistor to ground, while other is on +5V
int buttonC = 4;  // one side of buttonC attached to pin 4 and 10K resistor to ground, while other is on +5V
int servoPin = 11;  //servo attached to pin 11
int AState = 0; // set AState
int BState = 0; // set BState
int CState = 0; // set CState

void setup()
{
  myservo.attach(servoPin);
  pinMode(buttonA, INPUT);
  pinMode(buttonB, INPUT);
  pinMode(buttonC, INPUT);
}


void loop()
{
  AState = digitalRead(buttonA); // read and save to the variable "AState" the actual state of button
  {
    BState = digitalRead(buttonB); // read and save to the variable "BState" the actual state of button
    {
      CState = digitalRead(buttonC); // read and save to the variable "CState" the actual state of button
      if (AState == HIGH){
        if (BState == HIGH){
          myservo.write(50);
          delay(1000);
          myservo.write(111);
          delay(1000);
        }
      }
      else
        myservo.write(90);

      if (AState == HIGH){
        if(CState == HIGH){
          myservo.write(130);
          delay(1000);
          myservo.write(70);
          delay(1000);
        }
      }
      else
        myservo.write(90);

    }
  }
}
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 98
Posts: 4389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Here is the auto formatted code...

.... but you still haven't posed a question. So what do you expect members to do?
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 1
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the question... I have also edited my original post. Thank you for pointing out this error!

How could I go about being able to press button(A) and then press either of the other buttons (B or C) a few seconds later without having to press them simultaneouly? Currently, with the code that I am running, this is what I have to do.
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 98
Posts: 4389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you're on the right track with the state idea, but you have a lot of extra {} pairs in there and I'm not personally sure what effect that's having. See <<<<<< below.

Code:
void loop()
{    // this one is needed since it starts lopp() <<<<<<<<
  AState = digitalRead(buttonA); // read and save to the variable "AState" the actual state of button
  {     // this and its closing partner are a bit weird <<<<<<<<<<<
    BState = digitalRead(buttonB); // read and save to the variable "BState" the actual state of button
    {    // this and its closing partner are a bit weird <<<<<<<<<<<
      CState = digitalRe

As I said, I'm not sure what effect those have but I'm pretty sure you can lose them.

You might add Serial.print() in strategic places with a message to show where the logic is taking you.

You also need to reset the states to low once the moving is finished.
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

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

You seem to have a different interpretation of EVERY than the rest of us.

Quote
How could I go about being able to press button(A) and then press either of the other buttons (B or C) a few seconds later without having to press them simultaneouly? Currently, with the code that I am running, this is what I have to do.
You should separate detecting that the switches have been pressed from the code that takes action based on which switches are pressed.

You need to note when each switch is pressed. Make your decisions based on timing.

Of course, the real problem seems to be your difficulty in defining exactly what you want to do. "A few seconds later" is not something that you can write a program to deal with. If switch B or switch C is pressed within 5000 milliseconds of switch A being pressed, do..." is something that CAN be translated into a program.

Another thing to consider is that switches can be pressed and released. Do you want to take one action if switch 2 is pressed while switch 1 is pressed? Or, do you want to take action is switch 2 is pressed and released after switch 1 is pressed and released?
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 98
Posts: 4389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS... what's the effect of those extra {....} pairs?
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

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

@PaulS... what's the effect of those extra {....} pairs?
It depends on what is inside of them. By themselves, extra { and } cause no problems.
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 98
Posts: 4389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks... was just wondering they somehow might bundle some lines together. (In a way similar to the {} after an if, where without them, only the first xyz; gets recognised.)

Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 1
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In order to clear up what I am trying to do...
Just to make it clear what I am trying to do.
In the end I am going to build a coin operated salt and pepper dispenser. When a coin is put in the slot, it will trigger a momentary button (buttonA). After that, I want the customer to have the option to choose between salt and pepper (buttonB and buttonC). The problem is that the current code states that if the coin button (buttonA) is pressed, then the user needs to select their seasoning (buttonB or buttonC) simultaneously in order for the servo to turn. I would like to allow the user to insert a coin, and then take their own sweet time to choose from salt or pepper, similar to modern day pop machines. How could I do this?
Hopefully this clears it up. Thanks for your help.
« Last Edit: February 16, 2014, 11:14:39 am by icecats » Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 98
Posts: 4389
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make the B and C buttons interrupts maybe?

(I can't help but wonder why we're going to have to pay for salt and pepper when most places have those annoying, but free, little packets  smiley-cool )
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 1
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would I implement interrupts? At our Junior High cafeteria, they give us utterly tasteless food, and don't supply any seasoning. I was hoping to make some cash off my peers by selling a dash of salt or pepper for a quarter. I am also building this project just to have some fun.
Logged

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

Quote
Thanks... was just wondering they somehow might bundle some lines together. (In a way similar to the {} after an if, where without them, only the first xyz; gets recognised.)
They can do that. That's why I wanted OP to fix his code, to see where that might be happening.
Logged

Pages: [1] 2 3   Go Up
Jump to: