1

I am junior high teacher teaching students how to program using Arduino. I know the basics of programing but the students wanted to program a game which is a little above my skill set. I am using the following sketch (http://www.itopen.it/arduino-pong-with-8x8-led-matrix-and-max7219/) to introduce them to programing games. Thanks to this site I understand most of the code. However I would like to add a line of code that will make a piezo buzzer buzz every time the "ball" hits the pad. I think the code should look something like the code bellow, but I am not sure if it is correct or where I would put it.

#define buz 8 // number of pin buzzer connected to 
     if (HIT_CENTER || HIT_RIGHT || HIT_LEFT) {
        digitalWrite(buz, HIGH);
        delay(100);
        digiralWrite(buz, LOW);
        }

Any help would be appreciated.

1

The condition HIT_CENTER || HIT_RIGHT || HIT_LEFT will always be true. (All three of those HIT_ values are non-zero constants.)

Instead, you need to test if getHit() is non-zero. (To see this, search the sketch for occurrences of HIT_RIGHT or HIT_LEFT.)

The sketch sets up an event timer (via ball_timer = timer.every(BALL_DELAY, moveBall); in setup()) so that ball position will update every 200 ms. The loop() sequence is: Use timer.update() to service timer events. Use setPad() to read the paddle position. Use drawGame() to update the game picture. Somehow use checkLoose() to see if game is over. Use delay(GAME_DELAY) to wait 10 ms.

Given that structure of loop(), throwing in a 100 ms delay to wait for a buzzer would mess up the game play. You could turn on the buzzer and set up a timer event for turning it off, each time the ball hits the paddle.

More straightforwardly, since a 10 ms beep is likely to be adequately long, you could test for a hit just before delay(GAME_DELAY), and always turn the buzzer off just after that delay. In other words, before the delay say: if (getHit()) digitalWrite(buz, HIGH); and after the delay say digitalWrite(buz, LOW);.

This supposes that writing HIGH to buz turns on the buzzer and that writing LOW turns it off.

0

Method 1: here is what i would have done is use this code snippet we can get the state of HIT,,,if it is not equal to HIT_NONE ,buzz it

 #define buz 8 
 if(getHit()!=HIT_NONE){
    digitalWrite(buz, HIGH);
    delay(10);
    digitalWrite(buz, LOW);
    }

This is the simpler version where in delay is small,Beaware using delay will completely halt processor operation ,buttons ,joystick serial ports etc everything will be halted.

Method 2:With Flexible Delay

  #define buz 8
  unsigned long previousMillis = 0; 
  const long interval = 200;   /////Specify the delay you want here
  void loop()
   {
     unsigned long currentMillis = millis();  //start timer
     /*
         Remaining Code Is Left As It Is
     */
   }



       ///////////Code Snippet Without Using Delay
   if(getHit()!=HIT_NONE){
   digitalWrite(buz, HIGH);
   if (currentMillis - previousMillis >= interval) 
        {
            previousMillis = currentMillis;
            digitalWrite(buz, LOW);
        }
   }   

But where to add these functions??? Use getHit() to implement ,,,just like below

  int getHit() {
  if(yball != 6 || xball < xpad || xball > xpad + PADSIZE){
    return HIT_NONE;          //returns in case of hit failure so no 
                              // buzzer ring
     } 
        digitalWrite(buz, HIGH);    //If it has not returned yet makes
        delay(10);                  //sure that it is a successful hit
        digitalWrite(buz, LOW);     //buzzer ring
  if(xball == xpad + PADSIZE / 2){
    return HIT_CENTER;
     }
   return xball < xpad + PADSIZE / 2 ? HIT_LEFT : HIT_RIGHT;
 }

yet again u can replace those 3 lines with below one to be more flexible

digitalWrite(buz, HIGH);
if (currentMillis - previousMillis >= interval) 
     {
         previousMillis = currentMillis;
         digitalWrite(buz, LOW);
     }
}  

hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.