Author Topic: Arduino boolean question  (Read 3354 times)

0 Members and 1 Guest are viewing this topic.

Offline IvoSTopic starter

  • Frequent Contributor
  • **
  • Posts: 310
  • Country: us
Arduino boolean question
« on: January 29, 2015, 11:12:18 pm »
I have a book from Jaremy Blum to learn Arduino programming. I am doing some basic stuff right now like button debounce for example.
I set up Arduino Uno as ISP programmer to program ATTiny85 because that's all I need for my future application....
All works OK. Now, my goal was to program 2 separate buttons and 2 LEDs, each button toggling ON&OFF one LED.
I breadboard it and made that (somehow)happen. Buttons work as desired but I am not sure if I did it the right way because I just basically copy/paste and rename some lines from the book. I don't know if that is the right approach because my goal was to have just one general "Boolean debouce" definition but I was unable for some reason to do so. I wanted to implement both buttons (BUTTON1 & BUTTON2) into the debounce statement something like this:
boolean debounce (boolean last)
{
  boolean current = digitalRead (BUTTON1), (BUTTON2);

or maybe
boolean current = digitalRead (BUTTON1 && BUTTON2);  ??

What would be the right way to do it more simple.
Thanks.
Current code:
Code: [Select]
const int BUTTON1 = 3; // IO pin #2
const int LED1 = 4; // IO pin #3
const int BUTTON2 = 1; // IO pin #6
const int LED2 = 2; // IO pin #7

boolean lastButton = LOW;     
boolean currentButton = LOW;
boolean ledOn = false;         
boolean lastButton1 = LOW;
boolean currentButton1 = LOW;
boolean ledOn1 = false;

void setup ()
{
  pinMode (LED1, OUTPUT);
  pinMode (BUTTON1, INPUT);
  pinMode (LED2, OUTPUT);
  pinMode (BUTTON2, INPUT);
}

/* 
* Debouncing function
* Pass it to previous button state,
* and get back the current debounced button state.
*/
boolean debounce (boolean last)
{
  boolean current = digitalRead (BUTTON1); 
  if (last != current)                       
  {
    delay (5);                             
    current = digitalRead (BUTTON1);       
    return current;                         
  }
}
boolean debounce1 (boolean last1)
{
  boolean current1 = digitalRead (BUTTON2);   
  if (last1 != current1)                     
  {
    delay (5);                               
    current1 = digitalRead (BUTTON2);         
    return current1;                         
  }
}
 


void loop ()
{
  currentButton = debounce (lastButton);           
  if (lastButton == LOW && currentButton == HIGH)   
  {
    ledOn = ! ledOn;
  }                                                 
    lastButton = currentButton;                     
    digitalWrite (LED1, ledOn);                     




  currentButton1 = debounce1 (lastButton1);           
  if (lastButton1 == LOW && currentButton1 == HIGH)   
  {
    ledOn1 = ! ledOn1;
  }                                                   
    lastButton1 = currentButton1;                     
    digitalWrite (LED2, ledOn1);                     

}

 

 
   
   

 

Offline bobcat

  • Regular Contributor
  • *
  • Posts: 94
  • Country: us
Re: Arduino boolean question
« Reply #1 on: January 30, 2015, 12:34:37 am »
digitalRead() takes an Arduino pin number.
Using a boolean operator will only return true or false. (what value  that resolves to depends on how arduino defines it)
You have to take inputs one at a time.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Arduino boolean question
« Reply #2 on: January 30, 2015, 12:40:00 am »
1. The two debounce routines are highly alike (almost identical). You can probably combine them;
2. The routine needs to return a value under all conditions - it doesn't currently.
================================
https://dannyelectronics.wordpress.com/
 

Offline leo_r

  • Newbie
  • Posts: 7
Re: Arduino boolean question
« Reply #3 on: January 30, 2015, 01:24:00 pm »
Have you done much programming before? Your question is slightly unclear - I think you could interpret it two ways.

  • (I don't think this is what you want:) You want your debounce function to return true only when both buttons are pressed. I don't think the debounce function would be the place to do this, but you would, somewhere, have the code (button1 && button2).
  • You want to have a generic function which you can call for debouncing either switch.

Assuming it's the second one, you need to have a function which takes 2 arguments.

Argument 1 is the boolean "last"
Argument 2 is the pin of the button you wish to read.

The function would then look like this:

Code: [Select]
boolean debounce (boolean last, int buttonPin)
{
  boolean current = digitalRead (buttonPin);   
  if (last != current )                     
  {
    delay (5);                               
    current = digitalRead (buttonPin);                                 
  }
    // Putting this outside the "if" statement means the code will always return a value
    return current ; 

}

and then to call this function, you'd do the following:

Code: [Select]
currentButton1 = debounce (lastButton1, BUTTON1);

If you're new to programming in general then I'd recommend learning the basics on your PC rather than trying to do embedded stuff. The Arduino IDE is terrible for telling you where you've made mistakes and the embedded nature of it means you can't step through it line by line and see what the values each of your variables have at each execution step. If you're still trying to understand the basics building blocks of a program then having the added complexity of an Arduino and the Arduino IDE won't help.

A fairly basic introduction to C would be a good start, or perhaps something like Python or Ruby would be good to introduce the basic building blocks of programs - functions, loops, conditional statements and the like.
« Last Edit: January 30, 2015, 01:26:48 pm by leo_r »
 

Offline IvoSTopic starter

  • Frequent Contributor
  • **
  • Posts: 310
  • Country: us
Re: Arduino boolean question
« Reply #4 on: January 30, 2015, 08:07:42 pm »
Have you done much programming before? Your question is slightly unclear - I think you could interpret it two ways.

  • (I don't think this is what you want:) You want your debounce function to return true only when both buttons are pressed. I don't think the debounce function would be the place to do this, but you would, somewhere, have the code (button1 && button2).
  • You want to have a generic function which you can call for debouncing either switch.

Thank you. Yes, that's what I meant, to have a generic function which can call for debouncing either switch.
I am not new to electronics, I designed and built lot of stuff but I am new to Arduino......therefore the lack of knowledge.



Assuming it's the second one, you need to have a function which takes 2 arguments.

Argument 1 is the boolean "last"
Argument 2 is the pin of the button you wish to read.

The function would then look like this:

Code: [Select]
boolean debounce (boolean last, int buttonPin)
{
  boolean current = digitalRead (buttonPin);   
  if (last != current )                     
  {
    delay (5);                               
    current = digitalRead (buttonPin);                                 
  }
    // Putting this outside the "if" statement means the code will always return a value
    return current ; 

}

and then to call this function, you'd do the following:

Code: [Select]
currentButton1 = debounce (lastButton1, BUTTON1);

If you're new to programming in general then I'd recommend learning the basics on your PC rather than trying to do embedded stuff. The Arduino IDE is terrible for telling you where you've made mistakes and the embedded nature of it means you can't step through it line by line and see what the values each of your variables have at each execution step. If you're still trying to understand the basics building blocks of a program then having the added complexity of an Arduino and the Arduino IDE won't help.

A fairly basic introduction to C would be a good start, or perhaps something like Python or Ruby would be good to introduce the basic building blocks of programs - functions, loops, conditional statements and the like.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf