int buttonState[10];
int last,x;
int i,z=1,win,v[10],j;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin[1], INPUT);
pinMode(buttonPin[2], INPUT);
pinMode(buttonPin[3], INPUT);
pinMode(buttonPin[4], INPUT);
pinMode(buttonPin[5], INPUT);
pinMode(buttonPin[6], INPUT);
pinMode(buttonPin[7], INPUT);
pinMode(buttonPin[8], INPUT);
pinMode(buttonPin[9], INPUT);
}
void loop()
{
for (i=1;i<=9;i++)
{ buttonState = digitalRead(buttonPin*);* _ if (buttonState == HIGH)
* { if (!v) { v=z; // digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) // delay(1000); // wait for a second* // digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW_
* // for (j=1;j<=9;j++)* * // Serial.println(v[j]);*
* switch (i)* * { * * case 1 :{ if (v[2]==z && v[3]==z) win=z;* * else if (v[4]==z && v[7]==z) win=z;* * else if (v[5]==z && v[9]==z) win=z;}* * break;* * case 2 : {if (v[1] == z && v[3] == z) win = z;* * if (v[5] == z && v[8] == z) win = z;} break;* * case 3 : {if (v[2] == z && v[1] == z) win = z;* * if (v[5] == z && v[7] == z) win = z;* * if (v[6] == z && v[9] == z) win = z;} break;* * case 4 : {if (v[1] == z && v[7] == z) win = z;* * if (v[5] == z && v[6] == z) win = z;} break;* * case 5 : {if (v[2] == z && v[8] == z) win = z;* * if (v[4] == z && v[6] == z) win = z;* * if (v[1] == z && v[9] == z) win = z;* * if (v[7] == z && v[3] == z) win = z;} break;* * case 6 : {if (v[9] == z && v[3] == z) win = z;* * if (v[4] == z && v[5] == z) win = z;} break;* * case 7 : {if (v[8] == z && v[9] == z) win = z;* * if (v[4] == z && v[1] == z) win = z;* * if (v[5] == z && v[3] == z) win = z;} break;* * case 8 : {if (v[2] == z && v[5] == z) win = z;* * if (v[9] == z && v[7] == z) win = z;} break;* * case 9 : {if (v[2] == z && v[5] == z) win = z;* * if (v[9] == z && v[7] == z) win = z;} break;*
* }* * if (win){ Serial.println("The winner is ");* * Serial.print(win);* * delay (1000000000);}*
* z++;}* * }* * }*
} [/quote] I opened the serial monitor , but i recive nothing. First , i have been tested the serial monitor whith an example and it works. Can you help me ? Plsss
Thanks all for the support , i put serialbegin in the setup and it works , but…
when i press the D11 button , i recive on serial monitor this :
100000000
120000000
and i should recive only 100000000 . What’s the problem ? Maybe some wrong connections between D11 and D12 or a code problem ? i have some problem with A3 and A4.
That’s my new code :
const int buttonPin[10] = {0,11, 12, 13, 14, 15, 16, 17, 18, 19,} ;
const int ledPin = 7;
int buttonState[10];
int last,x;
int i,z=1,win,v[10],j;
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin[1], INPUT);
pinMode(buttonPin[2], INPUT);
pinMode(buttonPin[3], INPUT);
pinMode(buttonPin[4], INPUT);
pinMode(buttonPin[5], INPUT);
pinMode(buttonPin[6], INPUT);
pinMode(buttonPin[7], INPUT);
pinMode(buttonPin[8], INPUT);
pinMode(buttonPin[9], INPUT);
}
void loop()
{
for (i=1;i<=9;i++)
{ buttonState[i] = digitalRead(buttonPin[i]);
if (buttonState[i] == HIGH)
{
if (v[i]==0) { v[i]=z;
// digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
// delay(1000); // wait for a second
// digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
for (j=1;j<=9;j++)
{Serial.print(v[j]);
Serial.print("");}
Serial.println();
switch (i)
{
case 1 :{ if (v[2]==z && v[3]==z) win=z;
else if (v[4]==z && v[7]==z) win=z;
else if (v[5]==z && v[9]==z) win=z;}
break;
case 2 : {if (v[1] == z && v[3] == z) win = z;
if (v[5] == z && v[8] == z) win = z;} break;
case 3 : {if (v[2] == z && v[1] == z) win = z;
if (v[5] == z && v[7] == z) win = z;
if (v[6] == z && v[9] == z) win = z;} break;
case 4 : {if (v[1] == z && v[7] == z) win = z;
if (v[5] == z && v[6] == z) win = z;} break;
case 5 : {if (v[2] == z && v[8] == z) win = z;
if (v[4] == z && v[6] == z) win = z;
if (v[1] == z && v[9] == z) win = z;
if (v[7] == z && v[3] == z) win = z;} break;
case 6 : {if (v[9] == z && v[3] == z) win = z;
if (v[4] == z && v[5] == z) win = z;} break;
case 7 : {if (v[8] == z && v[9] == z) win = z;
if (v[4] == z && v[1] == z) win = z;
if (v[5] == z && v[3] == z) win = z;} break;
case 8 : {if (v[2] == z && v[5] == z) win = z;
if (v[9] == z && v[7] == z) win = z;} break;
case 9 : {if (v[2] == z && v[5] == z) win = z;
if (v[9] == z && v[7] == z) win = z;} break;
}
if (win){ Serial.print("The winner is ");
Serial.println(win);
delay (1000000000);}
z++; if (z==3) z=1; }
}
}
}
That code is a real mess. Put EVERY { on a line BY ITSELF. Put EVERY } on a line BY ITSELF. Use Tools + Auto Format. Delete the commented out code. Put comments in to describe what the code is supposed to do.
AND, most importantly, STOP trying to use array indices starting at one. Accept that the C language developers knew what they were doing when they made array indices start at 0.
when i want to press a button , i move the blue wire on the port i want (if i want to have the v[3] with 2 , i connect the blue wire to D13 port and i press the button. I have only one button 'cause this is only a test and i didn’t need 9 buttons for this. In the final product I’ll have 9 buttons.
Pauls , I started this project from an example , so that’s why a have comment in code. You can ignore that if you want. And…I use arrays from 1 because my buttons are from 1. I have enough ram on my nano , so I don’t care about some free [0] arrays. I’ve been coding for 3 years in c++ at school so I have any experience in this. All the best !
Your array stores 10 elements I see where a typo (look at the last element comma with no value) not sure what the compiler will treat that.If consider that a value it will be out of bounds!
It could not be the problem but fix that ambiguity!
hugo007:
Your array stores 10 elements I see where a typo (look at the last element comma with no value) not sure what the compiler will treat that.If consider that a value it will be out of bounds!
It could not be the problem but fix that ambiguity!
const int buttonPin[10] = {
0,
11,
12,
13,
14,
15,
16,
17,
18,
19
};
int buttonState[10],i,z=1,win,v[10],j;
void setup() {
Serial.begin(9600);
pinMode(buttonPin[1], INPUT);
pinMode(buttonPin[2], INPUT);
pinMode(buttonPin[3], INPUT);
pinMode(buttonPin[4], INPUT);
pinMode(buttonPin[5], INPUT);
pinMode(buttonPin[6], INPUT);
pinMode(buttonPin[7], INPUT);
pinMode(buttonPin[8], INPUT);
pinMode(buttonPin[9], INPUT);
}
void loop()
{
for (i=1;i<=9;i++)
{ buttonState[i] = digitalRead(buttonPin[i]);
if (buttonState[i] == HIGH)
{
if (v[i]==0) { v[i]=z;
for (j=1;j<=9;j++)
{Serial.print(v[j]);
Serial.print("");}
Serial.println();
switch (i)
{
case 1 :{ if (v[2]==z && v[3]==z) win=z;
else if (v[4]==z && v[7]==z) win=z;
else if (v[5]==z && v[9]==z) win=z;}
break;
case 2 : {if (v[1] == z && v[3] == z) win = z;
if (v[5] == z && v[8] == z) win = z;} break;
case 3 : {if (v[2] == z && v[1] == z) win = z;
if (v[5] == z && v[7] == z) win = z;
if (v[6] == z && v[9] == z) win = z;} break;
case 4 : {if (v[1] == z && v[7] == z) win = z;
if (v[5] == z && v[6] == z) win = z;} break;
case 5 : {if (v[2] == z && v[8] == z) win = z;
if (v[4] == z && v[6] == z) win = z;
if (v[1] == z && v[9] == z) win = z;
if (v[7] == z && v[3] == z) win = z;} break;
case 6 : {if (v[9] == z && v[3] == z) win = z;
if (v[4] == z && v[5] == z) win = z;} break;
case 7 : {if (v[8] == z && v[9] == z) win = z;
if (v[4] == z && v[1] == z) win = z;
if (v[5] == z && v[3] == z) win = z;} break;
case 8 : {if (v[2] == z && v[5] == z) win = z;
if (v[9] == z && v[7] == z) win = z;} break;
case 9 : {if (v[2] == z && v[5] == z) win = z;
if (v[9] == z && v[7] == z) win = z;} break;
}
if (win){ Serial.print("The winner is ");
Serial.println(win);
delay (1000000000);}
z++; if (z==3) z=1; }
}
}
}
I don’t have a schematic. You can check my photo here : http://ibb.co/n4Pw2v
I will create a schematic if you really need. I’ll come back when it’s done.
Edit: there it is : https://ibb.co/iTqdhv
UUUpsss : the blue wire is connected to the left pin of button , not in right like in schematic…
The wiring of the switch looks weird. The 4 legs are for stability when connecting to a printed circuit board. The legs are connected in pairs, though, so your resistor is connected to nothing when the switch is, and is not, pressed.