Pages: [1] 2 3   Go Down
Author Topic: different action based on the number of times a button is pressed  (Read 1743 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm getting close to finishing my air raid siren ( see attached photo's )  I've written some code to alter the siren speed depending the position of a rotary switch, this took me a while using the arduino cook book, Jeremy Blum vids on youtube and here searching the forums.

My setup is RC motor and controller with BEC, 7.2v 3700mah battery, arduino uno, 6 position rotary switch with voltage divider circuit to sense position, main power on / off switch, on order two led push buttons.

I've got the siren running on this set up but would like to add 2 NO push buttons in series to start the siren. this is a safety feature to ensure both hands are on the buttons when running. ( and not in the chopper )

The first three positions on the rotary switch will be low speed cycles for indoor use for me and my kid to use. the remaining three postion will be full speed simulating WWII air raid, and fire trucks but I don't want the little guy hurting his ears so I was thinking of creating a pushing pattern to enable the high speed cycles. something like 5 short presses on the push buttons.

This is where i need some help in formating the right code into my program. my thoughts are sense the button being pressed add 1 to variable each time it's pressed then i need to add this to my else if statements but I not sure of the correct formatting.

Any help would gratefully recieved, please go gently on my code as this is the 1st arduino program I've written, I'm sure it could be more efficient,  but the only other thing I've programmed is a 1970's Casio calculator for trigonometry.
 
Regards

Dale

Code:
#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
// a maximum of eight servo objects can be created

float pos = 0;    // variable to store the rotary switch input



void setup() {
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo1.write(pos);// arm speed controller
  delay(3000);
}

// the loop routine runs over and over again forever:
void loop() {
  
  start:
  int sensorValue = analogRead(A0);  // read the input on analog pin 0:
  if (sensorValue > 100 && sensorValue < 200) { goto servo1;}     // create jump to values
  else if (sensorValue > 300 && sensorValue < 400) { goto servo2;}
  else if (sensorValue > 500 && sensorValue < 599) { goto servo3;}
  else if (sensorValue > 600 && sensorValue < 699) { goto servo4;}
  else if (sensorValue > 800 && sensorValue < 899) { goto servo5;}
  else if (sensorValue > 900) { goto servo6;}
  
  

  servo1:
   {                                 // MAX SPEED 120
      myservo1.write(pos = 10);              // tell servo to go to position in variable 'pos'
   }  
  { goto start;}
  
  servo2:
   {                                
     myservo1.write(pos = 13);  
   }
  { goto start;}
  
   servo3:
    {                                
      myservo1.write(pos = 20);              // tell servo to go to position in variable 'pos'
   }  
  { goto start;}
  
  servo4:
  {                                
      myservo1.write(pos = 25);              // tell servo to go to position in variable 'pos'
   }  
  { goto start;}
  servo5:
 {                              
      myservo1.write(pos = 30);              // tell servo to go to position in variable 'pos'
   }  
  { goto start;}
  
  servo6:
  {                                
       myservo1.write(pos = 120);              // tell servo to go to position in variable 'pos'
      //delay(700);
                    
   }  
  { goto start;}

}



* 2013-01-20 15.33.28.jpg (602.85 KB, 2592x1944 - viewed 28 times.)

* 2013-01-20 15.34.46.jpg (605.05 KB, 2592x1944 - viewed 19 times.)
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Start by getting rid of the "Goto" statements and never use them again. Like ever.
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, note that if your sernsorValue is exactly 100, 200, 300, etc, it wall fall through to servo1.  Which is probably not want you want.

typically you would code more like
  else if ( 100 <= x && x < 200) ...
  else if ( 200 <= x && x < 300)

the main point being, that on one end of your range you include the "endpoint", and on the other end you exclude the endpoint.

Cheers,
John
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The value used by servo.write() is an integer which moves normal servos to the angle defined by the number or it sets the speed for continuous rotation servos.  I cannot begin to imagine what it does with (pos = 13) etc.

As an aside, a continuous rotation servo is not a servo at all as far as I am concerned, but that battle was lost some time ago.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I cannot begin to imagine what it does with (pos = 13) etc.

The return value of pos = 13 would be 13, so

Code:
myservo1.write(pos = 13);

is functionally equivalent to

Code:
pos = 13;
myservo1.write(13);

It may be bad form, but it would actually work.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the clarification, but I bet that the OP didn't mean to use that construction as that is not what the comments say.

Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the clarification, but I bet that the OP didn't mean to use that construction as that is not what the comments say.
Oh, I have no doubt the OP didn't know what he was doing with that part of the code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am happy with my motor control, it runs,  I can controller it's rise and fall with great precision. John thanks you have been most helpfull and have explained the best way to write my else if statements I did pre check the analog values before doing my else if statements 168/339/509/680/852/1023.

Kind regard

Dale
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you and I am glad it is running for you "so far".  When it comes time to add buttons you are really going to have to take your understanding "to the next level" though!

And the first step of that will be to heed the advice above about getting rid of the gotos.  If you need a better hint it would be:

Code:
void loop() {
   // NO start: LABEL
    int s=sensorValue;
    if (sensorValue > 100 && sensorValue < 200) {
      pos=10;
    }
    else if (sensorValue > 300 && sensorValue < 400) {
      pos=13;
    }
     ...
    myservo1.write(pos);
// NO goto: start
}  // Arduino does it for you, back to loop

Cheers,
John
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John,

 Yes I do need a hint, I think understand the code you posted,  I'll do a re write and post it back here.

Kind regards

Dale
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A somewhat different multi-press button example -

Code:
class push_button_t
{
    const uint8_t   _pin;
    const uint8_t   _transition_level;
    const uint8_t   _number_of_states;

    uint8_t         _press_counter;
    uint8_t         _earlier;

public:
    push_button_t(uint8_t pin, uint8_t transition_level = LOW, uint8_t number_of_states = 2)
        : _pin(pin)
        , _transition_level(transition_level)
        , _number_of_states(number_of_states)
       
        , _press_counter(0)
        , _earlier(0)
    {}

    void begin()
    {
        pinMode(_pin, INPUT);
    }

    operator uint8_t()
    {
        uint8_t now = digitalRead(_pin);
        if ( now != _earlier )
        {
            if ( now == _transition_level )
            {
                _press_counter++;
                _press_counter %= _number_of_states;
            }
        }
       
       _earlier = now;

       return _press_counter;
    }
};

class led_t
{
    const uint8_t     _pin;

public:   
    led_t(uint8_t pin) : _pin(pin)      {}

    void begin()                        { pinMode(_pin, OUTPUT); }
   
    uint8_t operator = (uint8_t rhs)    { digitalWrite(_pin, rhs); return rhs; }
    operator uint8_t ()                 { return ((LOW == digitalRead(_pin)) ? LOW : HIGH); }
};


const uint8_t   pinLED      = 33;   // chipKIT Basic I/O Shield LD1
const uint8_t   pinBUTTON   =  4;   // chipKIT Basic I/O Shield BN1

led_t           led(pinLED);
push_button_t   button(pinBUTTON, LOW, 4);

void loop()
{
    switch ( button )
    {
        case 0: led = 0;                    break;
        case 1: led = !led;  delay(125UL);  break;
        case 2: led = !led;  delay(250UL);  break;
        case 3: led = !led;  delay(500UL);  break;
    }
}

void setup()
{
    led.begin();
    button.begin();
}
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A somewhat different multi-press button example -

Code:
led_t           led(pinLED);
push_button_t   button(pinBUTTON, LOW, 4);

void loop()
{
    switch ( button )
    {
        case 0: led = 0;                    break;
        case 1: led = !led;  delay(125UL);  break;
        case 2: led = !led;  delay(250UL);  break;
        case 3: led = !led;  delay(500UL);  break;
    }
}

void setup()
{
    led.begin();
    button.begin();
}

Excellent!  That's what I love about C++ OO!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks des,

Thats a lot above my ability right now, I think i can make out it's altering the delay of the led based on the the button couter. hard for me to understand without the comment lines.

Kind regards

Dale
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The key here is in asking questions of things you don't understand now, and will never understand, until enough questions are asked, and not simply ignoring what is not currently understood.

Object oriented code, even on embedded systems, can simplify code considerably.

Thing to know learn here is classes and what conversion operators do.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was gratfully for the post but i was beening honest in my knowlege, I will do some research on the posted code after I re write my program.

Regards

Dale.
Logged

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