Joypad-Game-Higher and Higher

From Microduino Wiki
Jump to: navigation, search

Outline

We adopt Microduino-Joypad-Game to play the game Higher and Higher, which can be controlled by gravity perception.

Bill of Material

  • Microduino Equipment
Module Number Function
Microduino-Core 1 Core board
Microduino-USBTTL 1 Program download
Microduino-Joypad 1
Microduino-10DOF/zh 1

Download

TFT library: https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Libraries/_01_Microduino_TFT_ST7735 Joypad lib: https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Libraries/_08_Microduino_Shield_Joypad 10DOF library: https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Libraries/_05_Microduino_10DOF Code:

Debugging




  • Step 4: Open the program example
    • Acquire gravity induction and by this time, we only use the data of the Y-axis.
 void mpu6050()
{
  accelgyro.getMotion6(&ax,&ay,&az,&gx,&gy,&gz);//Acquire the acceleration and the angular velocity of the x,y,z axis. 
  //======Below are the quantized values of the acceleration
  Ax=ax/16384.00;
  Ay=ay/16384.00;
  Az=az/16384.00;
  //==========Below are the angles between the three axes and the horizontal coordinate system.  
  // Angel_accX=atan(Ax/sqrt(Az*Az+Ay*Ay))*180/3.14;
  Angel_accY=atan(Ay/sqrt(Ax*Ax+Az*Az))*180/3.14;
  //Angel_accZ=atan(Az/sqrt(Ax*Ax+Ay*Ay))*180/3.14;
}
    • Switch the code of the joystick and button control:
    int acc_readX() {
  if(Joypad.readButton(CH_SWITCH_L)&&Joypad.readButton(CH_SWITCH_R))
  {
    mpu6050();
    val= constrain(Angel_accY, -64, 64); // use Y for orientation
  }
  else
    val= -1 * constrain(Joypad.readJoystickX()/8, -64, 64); // use Y for orientation
  return(val);
  // Serial.println(val);
}

Gravity induction control is the default setting by turning the left and the right switches to the top. So, the opposite operation is joystick control. Users can change the setting according to personal needs.

  • The highest score is saved in EEPROM:
    if(score > highscore){
      if(score < 64000) {
        EEPROM.write(0, score);
        EEPROM.write(1, score>>8);
      }
      else { // bad value, zero out
        EEPROM.write(0,0);
        EEPROM.write(1,0);
      }        
      //drawString(1, s_height+3, "HIGHSCORE!", p_color_default, 1);
      // tft.drawString(1, s_height+3, "HIGHSCORE!", p_color_default, 1);
      drawInt(score, 120, s_height+3, p_color_default, background);
      highscore = score;
    }
  • Four ways of jumping:
switch (b_onUseID){
  case 0:
    drawString(1, s_height+3, "Jet pack", b_colors[b_onUseID], 1);
    //   tft.drawString(1, s_height+3, "Jet pack", b_colors[b_onUseID], 1);
    b_max = 100;
    b_remaining = b_max;
    break;
  case 1:
    drawString(1, s_height+3, "Frog legs", b_colors[b_onUseID], 1);
    //    tft.drawString(1, s_height+3, "Frog legs", b_colors[b_onUseID], 1);
    b_max = 100;
    b_remaining = b_max;
    break;
  case 2:
    drawString(1, s_height+3, "Testosterone", b_colors[b_onUseID], 1);
    //    tft.drawString(1, s_height+3, "Testosterone", b_colors[b_onUseID], 1);
    b_max = 1;
    b_remaining = b_max;
    break;
  case 3:
    drawString(1, s_height+3, "Low gravity", b_colors[b_onUseID], 1);
    //    tft.drawString(1, s_height+3, "Low gravity", b_colors[b_onUseID], 1);
    b_max = 255;
    b_remaining = b_max;
    break;
  }
  • Step 5: Compile the code and select the right board and COM port for download. After that, you can have fun with Microduino.
  • Experience:
    • Make sure the status of the two switches. If they’re all toggled upside, you should adopt gravity induction to control. Otherwise, you should use the left joystick to control.
    • You can change different way of jumping while the ball falls on different level.
    • That the ball falls on the ground means GAME OVER. If the score is higher than before, it will then be written in EEPROM.