Need some help with the basics.
I need to put the value from allButtons which is the 10 digital inputs from my board into a variable that I can check with a switch statement.
* *if (anyChange) { Serial.print("Buttons: "); for (int i=0; i<numButtons; i++) { Serial.print(allButtons[i], DEC); } Serial.println();* *
This is section of my code is giving me the state of the digital inputs that I need; a 10 digit binary number, but I cannot use the allButtons byte in my switch statement as I get the error that the quantity is not a integer.
I am sure its my comprehension of C at this point that is giving me grief. Just not sure how to take a value that is obtained by a for(int=0, etc) statement into an value that I can use.
Would you show us all your code?
Here is where I am up to so far Larry.
const int numButtons = 8;
void setup() {
for (int i=2; i<numButtons; i++) {
pinMode(i, INPUT_PULLUP);
}
Serial.begin(9600);
Serial.println("Begin Complete Joystick Test");
}
int positionX;
byte allButtons[numButtons];
byte prevButtons[numButtons];
void loop() {
// read digital pins and use them for the buttons
for (int i=2; i<numButtons; i++) {
if (digitalRead(i)) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons[i] = 0;
} else {
// when a pin reads low, the button is connecting to ground.
allButtons[i] = 1;
}
}
// check to see if any button changed since last time
boolean anyChange = false;
for (int i=0; i<numButtons; i++) {
if (allButtons[i] != prevButtons[i]) anyChange = true;
prevButtons[i] = allButtons[i];
}
// if any button changed, print them to the serial monitor
if (anyChange) {
Serial.print("Buttons: ");
for (int i=0; i<numButtons; i++) {
Serial.print(allButtons[i], DEC);
}
Serial.println();
}
switch (allButtons) {
case 0x200: positionX = 1023; break;
case 0x300: positionX = 921; break;
case 0x180: positionX = 819; break;
case 0x0C0: positionX = 717; break;
case 0x060: positionX = 615; break;
case 0x030: positionX = 513; break;
case 0x018: positionX = 411; break;
case 0x00C: positionX = 309; break;
case 0x006: positionX = 207; break;
case 0x003: positionX = 105; break;
case 0x001: positionX = 0; break;
default: positionX = -1;
}
}
allButtons[i]
is an array of numbers, not an integer.
unsigned int buttnumber = 0;
for (int i=0; i<numButtons; i++) {
Serial.print(allButtons[i], DEC);
buttnumber = (1<<buttnumber) | allButtons[i];
}
will place your 0/1 allButtons[i]
values into the single integer number "buttnumber",
for example buttnumber = 0x0111000101 = 0x1C5
then simply
..
switch (buttnumber) {
case 0x200: positionX = 1023; break;
case 0x300: positionX = 921; break;
..
Thank you so much for the quick reply. I will give it a go
I am almost there but still having some trouble. I added the code as pito showed (thanks again! I never would have worked that out) and had some success. Here is my current code.
const int numButtons = 10;
void setup() {
for (int i=0; i<numButtons; i++) {
pinMode(i, INPUT_PULLUP);
}
Serial.begin(9600);
Serial.println("Begin Complete Joystick Test");
}
int positionX;
byte allButtons[numButtons];
byte prevButtons[numButtons];
unsigned int buttonState = 0;
// get input into: tenBitValue, then
void loop() {
// read digital pins and use them for the buttons
for (int i=0; i<numButtons; i++) {
if (digitalRead(i)) {
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons[i] = 0;
} else {
// when a pin reads low, the button is connecting to ground.
allButtons[i] = 1;
}
}
// check to see if any button changed since last time
boolean anyChange = false;
for (int i=0; i<numButtons; i++) {
if (allButtons[i] != prevButtons[i]) anyChange = true;
prevButtons[i] = allButtons[i];
}
// if any button changed, print them to the serial monitor
if (anyChange) {
Serial.print("Buttons: ");
for (int i=0; i<numButtons; i++) {
buttonState = (1<<buttonState) | allButtons[i];
Serial.print(allButtons[i], DEC);
}
Serial.println();
switch (buttonState) {
case 0x200: positionX = 1023; break;
case 0x300: positionX = 921; break;
case 0x180: positionX = 819; break;
case 0x0C0: positionX = 717; break;
case 0x060: positionX = 615; break;
case 0x030: positionX = 513; break;
case 0x018: positionX = 411; break;
case 0x00C: positionX = 309; break;
case 0x006: positionX = 207; break;
case 0x003: positionX = 105; break;
case 0x001: positionX = 22; break;
//default: positionX = -1;
}
Joystick.Z(positionX);
Serial.print("Position: ");
Serial.print(positionX);
Serial.println();
Serial.print("ButtonState: ");
Serial.print(buttonState, BIN);
Serial.println();
Serial.println();
}
}
But this is the results -
So it appears that not all of the data in the allButtons array is being transferred into the button state?
What am I over looking?
What am I over looking?
The bitWrite() function.
Thanks Paul. Will look into it further.
Just wanted to say thanks Paul. I was struggling with bitWrite as I am only starting out with this coding caper but after you pushed me twice in that direction, I bit the bullet. Due to that I not only have it working, I actually worked out the 'for' statement and used it for my code -
for (int count=0; count<10; count++) {
bitWrite(buttonState,count,digitalRead(count));
And I understand how it works!! lol
Thanks again for the help gents.