Saving statesof 6 push buttons

Greetings everybody. I have connected 6 push buttons (schematics) and I'm reading their state on analog pin 5. Last button on the right side is number 1 and going to the left the go 2, 3, 4, 5, 6. Using this code:

int a=0;

void setup()
{
  Serial.begin(9600);
  pinMode(A5, INPUT_PULLUP);
}

void loop()
{
  a = analogRead(5);
  Serial.print("  analogRead() ");
  Serial.print("  value is :");
  Serial.println(a);
  delay(50);
}

I have determined that if none of the buttons is pressed reading is 1023, if number 1 is pressed reading is 73, if number 2 is pressed reading is 126, if number 3 is pressed reading is 173, if number 4 is pressed reading is 215, if number 5 is pressed reading is 253 and if number 6 is pressed reading is 287. Is it possible to make a function which would, whenever called, check is any of the buttons pressed and if it is, store the value of pressed number in an array of 50 elements. When it reaches the end of array it should start saving from the beginning of array that is overwrite the old values. For instance if I press 4th button, then wait for 2 minutes and press the first button three times and then after 10 minutes if I press third button, array should contain 4, 1, 1, 1, 3.

Forgot the schematics in the first post.

Ye, it is very possible to make a function to return the "digit" of the button.

int readA5button() {
int value = analogRead(A5) ;
if ( A5 < 30 ) return 1 ;
if ( A5 < 95 ) return 2 ;
  : and so on
}

Now you simply use Pushed = readA5button() ; in you code.
Note I've choosen values "halfway" - this is because the analog read value will be slightly different wvery time and sometime a bit more. The push buttons "bounce", you may se that you get a "wrong" value occasionally.

The second part is the same as doing button debouncing, if current button is different from last check it has just been pushed/released. You may need to include a small check with millis()/timer to avoid those button bounces.

To store the values in the circular buffer you need to have 2 indexes for the buffer - a get and a put index. You always read a value using the 'get' index and then increment to the next value, and you store a new value at the 'put' index and then increment to the next index. Things to watch out for:

  1. When reading values if 'get' == 'put' then the buffer is empty
  2. When you increment you need to check for a rollover of the index. The simplest way is to use the modulus function (++index % buffer_size).
    3, When ((put + 1) % buffer_size) == 'get' the buffer is full and you need to decide what to do. Options:
    a, Throw the latest value away - after all the buffer is full, or
    b. Overwrite the oldest value (at 'get'). In this case you need to increment both get an put indexes.

I have come up with this. This version would currently only detect, that is return which button was pressed(if I'm not overlooking something). It is (should be) detecting changes from LOW to HIGH. I have added bigger interval for the values, but they don't change so much, maybe plus/minus 3. If none of the buttons it should return 0:

int old_button = 0;

int readButtons (int pin) 
{
  int button, temp;
  temp = analogRead (pin);
  if (temp > 1015) button = 0;
  else if (temp > 70 && temp < 76) button = 1;
  else if (temp > 122 && temp < 128) button = 2;
  else if (temp > 169 && temp < 175) button = 3;
  else if (temp > 209 && temp < 217) button = 4;
  else if (temp > 247 && temp < 256) button = 5;
  else if (temp > 280 && temp < 291) button = 6;
  else button = 0;
  if (button = old_button) {
    return 0;
    old_button = button;
  } 

  else 
  {
    return button;
    old_button = button; 
  }
}

void setup 
{
  ...
}

void loop 
{
  ...
}

Looks reasonable. In my code repository I have a small class to handle analog buttons like yous if you want to see how I did it. My situation was similar and I was having to handle the buttons on an LCD shield.

Looks reasonable.

No it doesn't. It looks like sh*t. After using Tools + Auto Format to correct the horrid indenting, then it would look reasonable.

marco_c:
Looks reasonable. In my code repository I have a small class to handle analog buttons like yours if you want to see how I did it. My situation was similar and I was having to handle the buttons on an LCD shield.

I would gratefully appreciate it.

PaulS:

Looks reasonable.

No it doesn't. It looks like sh*t. After using Tools + Auto Format to correct the horrid indenting, then it would look reasonable.

I made the suggested changes.

I made the suggested changes.

And, the code looks a lot better. Now, if you'd just put the {s on a new line...

PaulS:

I made the suggested changes.

And, the code looks a lot better. Now, if you'd just put the {s on a new line...

Accomplished.

if (button = old_button) {Beep beep : you meant if (button == old_button) {

Here's a link to a library that does all kinds of input:

https://code.google.com/p/phi-prompt-user-interface-library/

  if (button = old_button) {
    return 0;
    old_button = button;
  }

You also need to swap the return to be after the assignment to old_button. Nothing gets executed after the return as the software returned to the calling program.

Assigning (=) the value of old_button to button isn't really useful, either. == is more likely what is needed.

Assigning (=) the value of old_button to button isn't really useful, either. == is more likely what is needed.

True, but that was already picked up previously by Msquare