system
August 17, 2010, 12:18pm
1
Hello,
I'm using this code to increment a string selection in a string array.
I would like to know how to implement a loop while the counter equal the maximum, it returns to the beginning.
Here the code :
/*
*/
const int buttonPin = 2;
int buttonPushCounter = -1; // counter
char* Str2[ ] = {
"AZER", "REZA", "POIU", "MLKJ", "CVBN", "WXCV", "FGHJ"};
int buttonState = 0; // current state
int lastButtonState = 0; // previous state
void setup() {
pinMode(buttonPin, INPUT);
Serial3.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial3.print("$CLEAR\r\n");
Serial3.print("$GO 1 1\r\n");
Serial3.print("$PRINT ");
Serial3.print (Str2[buttonPushCounter]);
Serial3.print("\r\n");
}
}
lastButtonState = buttonState;
}
Thx
DS
system
August 17, 2010, 12:22pm
2
At a guess (no C compiler handy)
...
buttonPushCounter++;
if (buttonPushCounter == sizeof(Str2))
buttonPushCounter = 0;
...
Edit:
Actually, this might not work thinking about it. Bloody pointers!
If you want buttonPushCounter to wrap around to zero after getting to 6, then just add
if (buttonPushCounter == 7) {
buttonPushCounter = 0;
}
after
buttonPushCounter++;
That's the long hand way of doing it. If you don't want to be able to understand what you wrote when you come back to it in 5 year's time, you could use this for the increment and wrap around all in one:
buttonPushCounter = ++buttonPushCounter % 7;
Andrew
system
August 17, 2010, 12:27pm
4
I found that finaly
/*
*/
const int buttonPin = 2;
int buttonPushCounter = -1; // counter
char* Str2[ ] = {
"AZER", "REZA", "POIU", "MLKJ", "CVBN", "WXCV", "FGHJ"};
int buttonState = 0; // current state
int lastButtonState = 0; // previous state
void setup() {
pinMode(buttonPin, INPUT);
Serial3.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial3.print("$CLEAR\r\n");
Serial3.print("$GO 1 1\r\n");
Serial3.print("$PRINT ");
Serial3.print (Str2[buttonPushCounter]);
Serial3.print("\r\n");
if(buttonPushCounter == 6)
{
buttonPushCounter = -1;
}
}
lastButtonState = buttonState;
}
}
But is there a way to use :
val = (int value = 0; value > 8; value++)
Thx
system
August 17, 2010, 12:35pm
5
But is there a way to use :
val = (int value = 0; value > 8; value++)
For what purpose? Do you want val to have 8 different values simultaneously? One at a time?
By the way, that loop won't work. value will be 0 initially, and the conditional test is for value greater than 8, which is false, so no looping will occur.
system
August 17, 2010, 12:46pm
6
True, I don't need to read 8 values.
I just understand the thing.
Now it is working well, thanks for support.
DS
system
August 17, 2010, 12:51pm
7
I know what I was thinking of..
...
buttonPushCounter++;
if (buttonPushCounter == (sizeof(Str2)/sizeof(char*)))
buttonPushCounter = 0;
...
No use to you now but it was bugging me.