Button and counter

Hello everyone.

This might seem very easy compared to the rest of the projects on this forum, but I've been asked to do something similar to that example: http://blog.saintlondon.co.uk/2011/04/01/good-daybad-day/

I know nothing about processing or arduino and bought some starters kit and books, but I'm realising I'll never be able to do the above in the time I have on my own (I have a week to build, code and finalise), so I'm a bit desperate!

Basically I just need two big buttons, one 'yes' one 'no'.

I'll need to put it out there, so it won't be connected to a computer, but it needs to be able to count how many times it's been pressed and when they were pressed. I need to be able to export that data back into my computer when the project is finished, for analysis.

I was also thinking of adding some small speakers behing each buttons as to have a little yeah or noooo when they are pressed.

I've got the hang of playing with small buttons and having them do a small action, but it's taken me the best part of two days to do it... haha, yes I'm probably a little bit crap at this.

So any help or pointers you could give me will be immensely appreciated.

Should I use 2 separate arduinos, one for each button? OR one controller connecting to the two buttons. And some help with the code as I don't think I'll ever be able to create it myself in time...

Many thanks!

One Arduino with two buttons and the tiny speakers if you want are enough for this.

You’ll want to keep your good mood, bad mood counts in the internal EEPROM.

Something like this would be nice to have hooked up on the internet sending data to a remote server. With Pachube you could actually create a trend of bad mood coming up and at which time of the day it would appear too.

//pins...
#define GOOD    3
#define BAD       4
#define RESET_COUNTS    5
#define TUNE     6 

unsigned long BadMood;
unsigned long GoodMood; 

playTune()    {
unsigned int i = 0; 

digitalWrite(TUNE, HIGH);
while (i < 30000)    { i ++}
digitalWrite(TUNE, LOW); 

}

}


setup()  {

pinMode(INPUT, RESET_COUNTS);
pinMode(INPUT, GOOD);
pinMode(INPUT, BAD);
pinMode(OUTPUT, TUNE);

Serial.begin(19200); 

getValuesFromEEPROM(); 
Serial.print(BadMood);
Serial.print(GoodMood);

if (RESET_COUNTS == LOW) {
    GoodMood = 0; 
    BadMood = 0; 
    storeValuesToEEPROM();
}



}

loop ()      { 

if (GOOD == LOW)    {
    while (GOOD == LOW); //we don't want to keep on counting, do we???
    GoodMood++;
    playTune();
    }

if (BAD == LOW)    {
    while (BAD == LOW); //we don't want to keep on counting, do we???
    BadMood++;
    playTune();
    }
 
}

So, this should work… if you want to reset counts, you need to reset the microcontroller with the button defined as RESET set to LOW. Button pushed if you install one.

All you need, and this is quite an important part, is to create a function to store the variables in EEPROM so you can retrieve them at the beginning to send through the Serial port.
Also, you need to perform a reset to get the values to the computer.

haha, I knew this would be complicated.

First thank you so much for taking the time to respond. So helpful.

But as I said just starting with it and... a few things I don't get :* :

I've copied and pasted the code in my arduino and when I verify it, it gives me that thing:

ISO C++ forbids declaration of 'playTune' with no type

AnalogInput:9: error: ISO C++ forbids declaration of 'playTune' with no type AnalogInput.cpp: In function 'int playTune()': AnalogInput:13: error: expected `;' before '}' token AnalogInput.cpp: At global scope: AnalogInput:18: error: expected declaration before '}' token

Not sure what to do with it...

void playTune()    {

There’s a whole lot of stuff in there “left as an exercise for the reader”.
You’ll figure it out.

So, this should work…

No, not really.
3 is not going to equal zero.

that helped thanks!

Now I’m getting this

expected ‘;’ before ‘}’ token
its highlights this line: while (i < 30000) { i ++}

AnalogInput.cpp: In function ‘void playTune()’:
AnalogInput:13: error: expected `;’ before ‘}’ token
AnalogInput.cpp: At global scope:
AnalogInput:18: error: expected declaration before ‘}’ token

I so wish I could fix it myself.

Yes, that while loop caused me a few concerns too.

I'd skip the tune playing and the EEPROM read/writing, and just use serial prints for now to read the pins defined (Hint - this wasn't implemented by bubulindo) and just print the "Good" or "bad" button presses to the serial monitor.

You'll need to look at the reference section over at the main Arduino site to see how a pinMode should be written, and how to use "digitalRead" to read the state of an input pin.

Thanks again.

Do you mean deleting all of the following?

loop () {

if (GOOD == LOW) {
while (GOOD == LOW); //we don’t want to keep on counting, do we???
GoodMood++;
playTune();
}

if (BAD == LOW) {
while (BAD == LOW); //we don’t want to keep on counting, do we???
BadMood++;
playTune();
}

}

and what do you mean he didn’t implement it? Its already been done by someone?

actually no that doesn't work.

How do i skip the tune playing?

Thanks for the tip, I'll have a look at these sections.

At least, if I can get the code right, I'll be a good place to start.

How do i skip the tune playing?

Pretty simple. Delete playTune(), and don't call it.

I did not compile it... and did not test it. Hell, I didn't even open the Arduino IDE or a text editor. But it should give you some pointers on how to implement it, sorry for not stating that in my reply.

This

void loop ()      { 

if (digitalRead(GOOD) == LOW)    {
    while (digitalRead(GOOD) == LOW); //we don't want to keep on counting, do we???
    GoodMood++;
    //playTune();
    }

if (digitalRead(BAD) == LOW)    {
    while (digitalRead(BAD) == LOW); //we don't want to keep on counting, do we???
    BadMood++;
    //playTune();
    }

}

is what counts the number of times that the buttons have been pushed.

I did not compile it... and did not test it. Hell, I didn't even open the Arduino IDE or a text editor.

One of the problems with C for beginners is that it will allow them to copy and compile constructs like pinMode(INPUT, RESET_COUNTS);, and in a few cases, such typos may even give the appearance of working.

It will even compile if (RESET_COUNTS == LOW) even though everyone else can see that five is not going to be equal to zero anytime soon.

A little caution, before posting?

Ha, no need to apologize!

I wish I copuld be a little less useless. I’ve literally started yesterday, so get confused easily. And being on a deadline doesn’t help.

So so far, I have the following code:

//pins…
#define GOOD 3
#define BAD 4
#define RESET_COUNTS 5
#define TUNE 6

unsigned long BadMood;
unsigned long GoodMood;

void playTune() {
unsigned int i = 0;

digitalWrite(TUNE, HIGH);
while (i < 30000) { i ++}
digitalWrite(TUNE, LOW);

}

}

setup() {

pinMode(INPUT, RESET_COUNTS);
pinMode(INPUT, GOOD);
pinMode(INPUT, BAD);
pinMode(OUTPUT, TUNE);

Serial.begin(19200);

getValuesFromEEPROM();
Serial.print(BadMood);
Serial.print(GoodMood);

if (RESET_COUNTS == LOW) {
GoodMood = 0;
BadMood = 0;
storeValuesToEEPROM();
}

}

void loop () {

if (digitalRead(GOOD) == LOW) {
while (digitalRead(GOOD) == LOW); //we don’t want to keep on counting, do we???
GoodMood++;
//playTune();
}

if (digitalRead(BAD) == LOW) {
while (digitalRead(BAD) == LOW); //we don’t want to keep on counting, do we???
BadMood++;
//playTune();
}

}

Which is still giving me the same error message from earlier
expected ‘;’ before ‘}’ token
while (i < 30000) { i ++}

Any thoughts on what’s wrong?

Im really grateful for your help both of you btw!

Too many curly braces.

unsigned long BadMood;
unsigned long GoodMood; 

void playTune()    {
unsigned int i = 0; 

digitalWrite(TUNE, HIGH);
while (i < 30000)    { i ++}
digitalWrite(TUNE, LOW); 

}

}

should be:

unsigned long BadMood;
unsigned long GoodMood; 

void playTune()    
{
  digitalWrite(TUNE, HIGH);  // note this doesn't actually play a tune by itself
  delay (250);// wait a quarter of a second...or whatever you want
  digitalWrite(TUNE, LOW); 
}

void setup ()
{

etc.

(like I said - the “while” loop gave me problems.)

NB.

pinMode(INPUT, RESET_COUNTS);

As I said earlier, check the language reference for the correct form of a “pinMode”

http://arduino.cc/en/Reference/HomePage

Any thoughts on what’s wrong?
while (i < 30000) { i ++}
perhaps
while (i < 30000) { i ++;}

AWOL: A little caution, before posting?

Advice taken... thing is, I don't use those functions... I use bit masks, so to use them without knowing which bit to be used would be a problem. It would also be a problem explaining what it was doing. But yes, next time, I'll be a bit more careful about these things.

ok!

So, let's pretend that Good is in slot 2 and BAD in slot 9.

Does the following work?

When I verify it, it says

'getvaluesfromEEPROM' was not declared in scope.

button_counter_tune.cpp: In function 'void setup()': button_counter_tune:30: error: 'getValuesFromEEPROM' was not declared in this scope button_counter_tune:37: error: 'storeValuesToEEPROM' was not declared in this scope

const int GOOD = 2;     // the number of the pushbutton pin
const int BAD = 9;     // the number of the pushbutton pin

//pins...
#define GOOD    3
#define BAD       4
#define RESET_COUNTS    5
#define TUNE     6 

unsigned long BadMood;
unsigned long GoodMood; 


void playTune()    
{
  digitalWrite(TUNE, HIGH);  // note this doesn't actually play a tune by itself
  delay (250);// wait a quarter of a second...or whatever you want
  digitalWrite(TUNE, LOW); 
}

void setup ()
{

pinMode(INPUT, RESET_COUNTS);
pinMode(INPUT, GOOD);
pinMode(INPUT, BAD);
pinMode(OUTPUT, TUNE);

Serial.begin(19200); 

getValuesFromEEPROM(); 
Serial.print(BadMood);
Serial.print(GoodMood);

if (RESET_COUNTS == LOW) {
    GoodMood = 0; 
    BadMood = 0; 
    storeValuesToEEPROM();
}


}

void loop ()      { 

if (digitalRead(GOOD) == LOW)    {
    while (digitalRead(GOOD) == LOW); //we don't want to keep on counting, do we???
    GoodMood++;
    //playTune();
    }

if (digitalRead(BAD) == LOW)    {
    while (digitalRead(BAD) == LOW); //we don't want to keep on counting, do we???
    BadMood++;
    //playTune();
    }

}

Forget the EEPROM, concentrate on the "pinMode"s

Thanks. I have looked at the page http://arduino.cc/en/Reference/PinMode but there isn't much to explaining what's wrong with pinMode(INPUT, GOOD);?

It's all a bit chinese to me, are there some examples I could look into? all i could find is the tutorial one http://www.arduino.cc/en/Tutorial/Button

I wish i had the time to learn it properly, which is my long term plan btw, but just need to figure out this piece of code as quickly as i can. Best,

but there isn't much to explaining what's wrong with pinMode(INPUT, GOOD);?

The order of parameters is important. INPUT has a value of zero, so setting pin zero to be an output (GOOD is not equal to zero, so mode is NOT INPUT) may not be what you intended.

Like this is the correct way.

pinMode(RESET_COUNTS, INPUT);
pinMode(GOOD, INPUT);
pinMode(BAD, INPUT);
pinMode(TUNE, OUTPUT);

comment or delete the lines where the EEPROM functions (getvaluesfromEEPROM and storeToEEPROM) are called. That is for later. Also, you might want to add a Serial.Print(Good/Bad Mood) after the mood counters are incremented.