I understand what you are trying to do, ie find out if button 5 is pressed, but does that line of code do that ?
Can you please explain what each part of the code does in English, as the code is constructed in a very odd way, at least to my eyes.
UKHeliBob:
I understand what you are trying to do, ie find out if button 5 is pressed, but does that line of code do that ?
Can you please explain what each part of the code does in English, as the code is constructed in a very odd way, at least to my eyes.
#define MAX 50
const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int LED4 = 5;
int array[MAX];
int old_b = 0;
int val;
int counter = 0;
int i;
int temp;
int L1;
int L2;
void setup () {
pinMode (A5, INPUT_PULLUP);
Serial.begin(9600);
}
int readButtons (int pin) { //function which reads analog pin and determines which button was pressed
int b, c;
c = analogRead(pin); //read analog pin
Serial.print("analogRead = "); //what is analog values you read
Serial.println(c); //print what you read
delay(100);
if (c > 1015) b = 0;
else if (c > 70 && c < 76) b = 1;
else if (c > 122 && c < 128) b = 2;
else if (c > 169 && c < 175) b = 3;
else if (c > 209 && c < 217) b = 4;
else if (c > 247 && c < 256) b = 5;
else if (c > 280 && c < 291) b = 6;
else b = 0; //if it is none of these values (garbage values) none of the buttons was pressed
if (b == stari_b) {
old_b = b;
return 0; //there was no change in reading that is no new button pushes or button is still being pressed
}
else {
return b;
old_b = b; //change in state and return which button was pressed
}
}
void loop () {
while ((val = readButtons(5)) != 5) { //until 5th button wasn't pressed
if ((val == 1) || (val == 2) || (val == 3) || (val == 4)) { //and if any of first four was pressed
array[counter] = val; //store which button was pressed
Serial.print("In "); //print which button was saved in which element of an array
Serial.print(counter);
Serial.print(" saving ");
Serial.println(val);
delay(100);
counter++;
if (counter == MAX) { //if you reached the end, start saving from the beginning of array
counter = 0;
}
}
}
temp = counter; // when 5th button was pressed, save how many numbers were pressed
counter = 0; // when you start saving buttons again, start from the beginning
for (i = 0; i < temp; i++) { //
if (array[i] % 2 == 0) { //
L1 = 2; //
L2 = array[i] / 3 + 3; //
} // determine which LEDs need to be blinked
else { //
L2 = 5; //
L1 = array[i] % 3 + 3; //
}
if (readButtons(5) != 5) { //if in the mean time 5th buttons was pressed again, exit and start saving buttons from 1 to 4
digitalWrite (L1, HIGH); //if not light up one of LEDs
if (readButtons(5) != 5) { //if 5th was pressed exit, turon of the one LED which was turned on
digitalWrite (L2, HIGH); //if not turn on second LED
delay(1000); // hold them on for 1 second
digitalWrite (L1, LOW); //
digitalWrite (L2, LOW); // turn them off
if (readButtons(5) == 5) {
i = temp;
}
}
else {
digitalWrite (L1, LOW);
i = temp;
}
}
}
}
Sorry. I was not explicit enough. It is this line of code that I wanted an explanation of
while ((val = readButtons(5)) != 5)
I may well be wrong but that line of code does not look normal to me, even if it happens to work.
I don't see anything wrong.
Short way of doing:
val = readButtons(5);
while (val != 5) {
...
val = readButtons(5);
}
It sure looks like I am wrong (and have learnt something) but the expression in the while brackets looks odd (to me) because it is setting a variable and testing its value at the same time. The construction also looks prone to error in writing and interpretation.
Bearing in mind the questions from the OP about other aspects of the code I think it more than likely that he did not mean to write it in that form, as I alluded to in reply #16.
because it is setting a variable and testing its value at the same time.
Nothing at all wrong with that.
Consider:x = y = 0;
Assign zero to y (an expression), and assign the result of that expression to x.
The result of this assignmentc is false, but isn't tested and is so discarded.
Same principle.
x = y = 0;
Valid it might be, but I don't like it !