Hi
Based in a question i had in ''Project Guidance section'' for what method i should use for push buttons memory ( Push Buttons memory ) all members there suggest me to follow Array method
specially one member dougp told me to follow this :
dougp:
Create an array of size 'x'. Initialize all the array elements with some number which will never naturally occur, say '99'.Initialize an array index variable to point to the first array element.
When a button is pressed store its number (or any special number you like) in the array at the position dictated by the index variable then increment the index variable. When another button is pressed, store and increment again.
Some other code will be looking at the first array position. If it sees a '99' it does nothing. When it sees a valid number it does the process 'y' associated with that number. When process 'y' is finished the first array element is destroyed by moving all higher-indexed elements one step to the next lower element:
a[0] <-- a[1], a[1] <-- a[2], etc. Decrement the index variable.
Put an invalid value in the now-vacated last array position.
As i am newbie in all this and first time i am using array (watched some youtube tutorials ) and English is not my Native language i would appreciate any help to make it step by step
The goal is not to have a code ready... but to understand how it is made
for now i wrote this code that is far away from what the other friend suggesting
int calls[4] = {18, 28, 38, 48};
byte callbutton1 = 3;//push button in the position
byte callbutton2 = 4;//push button in the position
byte callbutton3 = 5;//push button in the position
byte callbutton4 = 6;//push button in the position
int i;
int callcar1 = 0; // variable for reading the pushbutton status of the positions
int callcar2 = 0; // variable for reading the pushbutton status of the positions
int callcar3 = 0; // variable for reading the pushbutton status of the positions
int callcar4 = 0; // variable for reading the pushbutton status of the positions
boolean callFlag1 = false;
boolean callFlag2 = false;
boolean callFlag3 = false;
boolean callFlag4 = false;
void setup() {
Serial.begin(9600);
pinMode(callbutton1, INPUT_PULLUP);
pinMode(callbutton2, INPUT_PULLUP);
pinMode(callbutton3, INPUT_PULLUP);
pinMode(callbutton4, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbuttons value://
callcar1 = digitalRead(callbutton1);
callcar2 = digitalRead(callbutton2);
callcar3 = digitalRead(callbutton3);
callcar4 = digitalRead(callbutton4);
if (callcar1 == LOW && callFlag1 == false) {
calls[0] += 2;
callFlag1 = true;
}
if (callcar2 == LOW && callFlag2 == false) {
calls[1] += 2;
callFlag2 = true;
}
if (callcar3 == LOW && callFlag3 == false) {
calls[2] += 2;
callFlag3 = true;
}
if (callcar4 == LOW && callFlag4 == false) {
calls[3] += 2;
callFlag4 = true;
}
for (i = 0; i < 4; i++) {
Serial.println(calls[i]);
}
if ( calls[0] == 20 ) {
test();
}
if ( calls[1] == 30 ) {
test2();
}
}
void test() {
Serial.println("ok - 1");
calls[0] -= 2;
callFlag1 = false;
delay(2000);
}
void test2() {
Serial.println("ok - 2");
calls[1] -= 2;
callFlag2 = false;
delay(2000);
}
i know that i have to avoid delays .. i place them just to be able to see the result in serial monitor
Thanks in advance