Pages: [1] 2   Go Down
Author Topic: Program game does not work on chip, whats wrong with program  (Read 587 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

/*Ping pong, Version 1.0.9 Chip: Atmega 328P ( required parts wire Nine 220ohm resistor, Nine LEDs,
Two push buttons, and bread board) made April 24th, 2013. Point of the game is to score 5 point on opposing player,
you push the button once to block the net, and the light will bounce back from opposing player to other.*/

int Led1 = 8;//led 1 on digital pin 8//
int Led2 = 7;//led 2 on digital pin 7//
int Led3 = 6;//led 3 on digital pin 6//
int Led4 = 5;//led 4 on digital pin 5//
int Led5 = 3;//led 5 on digital pin 3//
int Led6 = 9;//led 6 on digital pin 9//
int Led7 = 10;//led 7 on digital pin 10//
int Led8 = 11;//led 8 on digital pin 11//
int Led9 = 12;//led 9 on digital pin 12//
int Button1 = 13;//button 1 on digital pin 13//
int Button2 = 4;//button 2 on digital pin 4//
int State1 = 0; // current state of the buttons //
int State2 = 0; // counter of number of button press //
int State11 = 0; // current state of the buttons //
int State22= 0; // counter of number of button press //
int State33 = 0;
int State44 = 0;
int FTL = 200; //delay for half a second//



void setup () { //set leds an buttonas output,and input//
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Led4, OUTPUT);
pinMode(Led5, OUTPUT);
pinMode(Led6, OUTPUT);
pinMode(Led7, OUTPUT);
pinMode(Led8, OUTPUT);
pinMode(Led9, OUTPUT);
pinMode(Button1, INPUT);
pinMode(Button2, INPUT);
}


void loop () {
digitalWrite(Led5, HIGH);
delay(FTL);
digitalWrite(Led5, LOW);
delay(FTL);
digitalWrite(Led6, HIGH);
delay(FTL);
digitalWrite(Led6, LOW);
delay(FTL);
digitalWrite(Led7, HIGH);
delay(FTL);
digitalWrite(Led7, LOW);
delay(FTL);
digitalWrite(Led8, HIGH);
 
State33=0;
while(State33<1000) //check led, and button 1000 times//
{
State1 = digitalRead(Button2);// read the button state, and the next button state //
if (State1 == HIGH && Led8 == HIGH)break;
State33++;
}
if (State1 == HIGH && Led8 == HIGH)
{
digitalWrite(Led8, LOW);
delay(FTL);
digitalWrite(Led7, HIGH);
delay(FTL);
digitalWrite(Led7, LOW);
delay(FTL);
digitalWrite(Led6, HIGH);
delay(FTL);
digitalWrite(Led6, LOW);
delay(FTL);
digitalWrite(Led5, HIGH);
delay(FTL);
digitalWrite(Led5, LOW);
delay(FTL);
digitalWrite(Led4, HIGH);
delay(FTL);
digitalWrite(Led4, LOW);
delay(FTL);
digitalWrite(Led3, HIGH);
delay(FTL);
digitalWrite(Led3, LOW);
delay(FTL);
digitalWrite(Led2, HIGH);
}
else
{
digitalWrite(Led8, LOW);
delay(FTL);  
digitalWrite(Led9, HIGH);// if not the ball move into net, and you press the restart  button to begin the play again //
}

State44=0;
while(State44<1000)
{
State11 = digitalRead(Button1);// compare the button state to the last button state //
if (State11 == HIGH && Led2 == HIGH)break;
State44++;
}
if (State11 == HIGH && Led2 == HIGH) //if the button is pushed, and led 2 or the blocker is high, send the ball back to the other net //
{
delay(FTL);
digitalWrite(Led2, LOW);
delay(FTL);
digitalWrite(Led3, HIGH);
delay(FTL);
digitalWrite(Led3, LOW);
delay(FTL);
digitalWrite(Led4, HIGH);
delay(FTL);
digitalWrite(Led4, LOW);
delay(FTL);
digitalWrite(Led5, HIGH);
delay(FTL);
digitalWrite(Led5, LOW);
delay(FTL);
digitalWrite(Led6, HIGH);
delay(FTL);
digitalWrite(Led6, LOW);
delay(FTL);
digitalWrite(Led7, HIGH);
delay(FTL);
digitalWrite(Led7, LOW);
delay(FTL);
digitalWrite(Led8, HIGH);
}
else
{
digitalWrite(Led2, LOW);
delay(FTL);
digitalWrite(Led1, HIGH); // if not the ball move into net, and you press the restart  button to begin the play again //
}
}
« Last Edit: May 02, 2013, 07:45:27 pm by Seanflame » Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Led8 == HIGH
Led8=11, it does not create a magic reference to your pin, you need to use digitalRead to get the pin state

Also please format (ctrl+T in IDE) and post in code tags, it's a pain to try and read code like this
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my teacher told me that you cannot digital read  a pin set to ouput smiley-eek
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Forget code for a moment. Exactly how do you see your game playing out?
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my teacher told me that you cannot digital read  a pin set to ouput smiley-eek

If your teacher meant it will not work, then he's wrong.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what do you mean by that like how the game will work
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes. How does the game work?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

start on the fifth led in the the middle, it starts blinking through the led 6, 7, 8 once it reaches the 8th led the player presses the button, and it sends the ball back to the opposing player, the other player has to do the same once the light reaches the 2nd led. If that dosen t happen the light will reach 9th led meaning the opposing player scored, other player the 1st led will light up
« Last Edit: May 02, 2013, 08:47:41 pm by Seanflame » Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you say "presses the button" do you mean press and hold it or press and release it? Could I, as a player simply hold the button down?

I can see your description in your code but would suggest that you rename some variables. "State22" might be better as "player2buttonCounter". Use descriptive variable names and use copy / paste to avoid typos and undeclared variable errors.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

/*Ping pong, Version 1.0.9 Chip: Atmega 328P ( required parts wire Nine 220ohm resistor, Nine LEDs,
Two push buttons, and bread board) made April 24th, 2013. Point of the game is to score 5 point on opposing player,
you push the button once to block the net, and the light will bounce back from opposing player to other.*/

int Led1 = 8;//led 1 on digital pin 8//
int Led2 = 7;//led 2 on digital pin 7//
int Led3 = 6;//led 3 on digital pin 6//
int Led4 = 5;//led 4 on digital pin 5//
int Led5 = 3;//led 5 on digital pin 3//
int Led6 = 9;//led 6 on digital pin 9//
int Led7 = 10;//led 7 on digital pin 10//
int Led8 = 11;//led 8 on digital pin 11//
int Led9 = 12;//led 9 on digital pin 12//
int Button1 = 13;//button 1 on digital pin 13//
int Button2 = 4;//button 2 on digital pin 4//
int State1 = 0; // current state of the buttons //
int State2 = 0; // counter of number of button press //
int State11 = 0; // current state of the buttons //
int State22= 0; // counter of number of button press //
int State33 = 0;
int State44 = 0;
int State3 = 0;
int State4 = 0;
int FTL = 200; //delay for half a second//

void setup () { //set leds an buttonas output,and input//
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Led4, OUTPUT);
pinMode(Led5, OUTPUT);
pinMode(Led6, OUTPUT);
pinMode(Led7, OUTPUT);
pinMode(Led8, OUTPUT);
pinMode(Led9, OUTPUT);
pinMode(Button1, INPUT);
pinMode(Button2, INPUT);
}

void loop () {
digitalWrite(Led5, HIGH);
delay(FTL);
digitalWrite(Led5, LOW);
delay(FTL);
digitalWrite(Led6, HIGH);
delay(FTL);
digitalWrite(Led6, LOW);
delay(FTL);
digitalWrite(Led7, HIGH);
delay(FTL);
digitalWrite(Led7, LOW);
delay(FTL);
digitalWrite(Led8, HIGH);
 
State33=0;
while(State33<3000) //check led, and button 1000 times//
{
State11 = digitalRead(Button2);// read the button state, and the next button state //
State22 = digitalRead(Button2);
State3 = digitalRead(Led8);
State33++;
}
if (State11 == HIGH && State22 == HIGH && Led8 == HIGH) // button pused twice send ball back, to opposing player
{
digitalWrite(Led8, LOW);
delay(FTL);
digitalWrite(Led7, HIGH);
delay(FTL);
digitalWrite(Led7, LOW);
delay(FTL);
digitalWrite(Led6, HIGH);
delay(FTL);
digitalWrite(Led6, LOW);
delay(FTL);
digitalWrite(Led5, HIGH);
delay(FTL);
digitalWrite(Led5, LOW);
delay(FTL);
digitalWrite(Led4, HIGH);
delay(FTL);
digitalWrite(Led4, LOW);
delay(FTL);
digitalWrite(Led3, HIGH);
delay(FTL);
digitalWrite(Led3, LOW);
delay(FTL);
digitalWrite(Led2, HIGH);
}
else
{
digitalWrite(Led8, LOW);
delay(FTL); 
digitalWrite(Led9, HIGH);// if not the ball move into net, and you press the restart  button to begin the play again //
}

State44=0;
while(State44<3000)
{
State1 = digitalRead(Button1);// compare the button state to the last button state //
State2 = digitalRead(Button1);
State4 = digitalRead(Led2);
State44++;
}
if (State1 == HIGH && State2 == HIGH && Led2 == HIGH ) //if the button is pushed twice, and led 2 or the blocker is high, send the ball back to the other net //
{
delay(FTL);
digitalWrite(Led2, LOW);
delay(FTL);
digitalWrite(Led3, HIGH);
delay(FTL);
digitalWrite(Led3, LOW);
delay(FTL);
digitalWrite(Led4, HIGH);
delay(FTL);
digitalWrite(Led4, LOW);
delay(FTL);
digitalWrite(Led5, HIGH);
delay(FTL);
digitalWrite(Led5, LOW);
delay(FTL);
digitalWrite(Led6, HIGH);
delay(FTL);
digitalWrite(Led6, LOW);
delay(FTL);
digitalWrite(Led7, HIGH);
delay(FTL);
digitalWrite(Led7, LOW);
delay(FTL);
digitalWrite(Led8, HIGH);
}
else
{
digitalWrite(Led2, LOW);
delay(FTL);
digitalWrite(Led1, HIGH); // if not the ball move into net, and you press the restart  button to begin the play again //
}
}

this is the revised version any problems you see?
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is the revised version any problems you see?

Not posted with code tags, and no use of the auto format tool to fix the complete lack of indenting.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just created an account today I don't now how to post it in code format
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just created an account today I don't now how to post it in code format

Then perhaps your should review the thread at the top entitled "How to use this forum - please read"
Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 44
Posts: 2810
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please read the sticky note.. I got tired of that code after about 20 lines... I would have liked to help.. But
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You haven't done anything to fix the problem:
Code:
if (State11 == HIGH && State22 == HIGH && Led8 == HIGH)
You are still using Led8 to try and check the state of the pin, the variable Led8=11, unless you specifically write otherwise.
Logged

Pages: [1] 2   Go Up
Jump to: