诛仙动漫在线观看全集免费观看al Project 4.00 Dice

The Dice program lesson teaches students how a 诛仙动漫在线观看全集免费观看 program works by turning the 1ST Maker Frog into a digital dice roller. Students choose to roll one or two dice using buttons, watch an animated roll with sound effects, and see the final dice values displayed on the OLED screen.

The Dice program lesson teaches students how a 诛仙动漫在线观看全集免费观看 program works by turning the 1ST Maker Frog into a digital dice roller. Students choose to roll one or two dice using buttons, watch an animated roll with sound effects, and see the final dice values displayed on the OLED screen.

Project Code:

				
					
/**************************************************
 * Program: Dice
 * Copyright (C) 京野ななか, Inc. 2025
 *
 * Description:
 *   This program implements a digital dice roller using
 *   the 1ST Maker Frog hardware. On startup, the user
 *   selects either 1-die or 2-dice mode using the
 *   buttons. Pressing the roll button triggers an
 *   animated roll sequence with sound effects, and the
 *   final dice value(s) are displayed on the OLED
 *   screen as graphical dice faces.
 *
 * Hardware:
 *   - 1ST Maker Frog board
 *   - OLED display (SSD1306)
 *   - Two buttons (BUTTON_ONE, BUTTON_TWO)
 *   - Piezo buzzer (PIEZO)
 **************************************************/

#include "1ST_Maker_Frog.h"

// --- Global Variables ---
/**************************************************
 * Section: Global Variables
 * Purpose:  Store game state and configuration.
 **************************************************/
int numberOfDice = 1;           // Game mode: 1 or 2 dice
int diceValue1 = 1;             // Value of the first die
int diceValue2 = 1;             // Value of the second die (if used)
int lastDiceValue1 = 0;         // Tracker for the first die to prevent redraws
int lastDiceValue2 = 0;         // Tracker for the second die
bool buttonWasPressed = false;  // For button debouncing

// --- Function Prototypes ---
/**************************************************
 * Section: Function Prototypes
 * Purpose:  Declare functions before they are used.
 **************************************************/
void drawOneDie(int val, int offsetX);
void drawPipHelper(int x, int y, int offsetX);


/**************************************************
 * Function: setup
 * Purpose:  Initializes hardware, shows the options
 *           menu, and sets the initial state.
 **************************************************/
void setup() {
  Serial.begin(9600);
  
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.clearDisplay();
  
  pinMode(BUTTON_ONE, INPUT_PULLUP);
  pinMode(BUTTON_TWO, INPUT_PULLUP);

  showOptionsMenu();

  randomSeed(analogRead(A1));
}

/**************************************************
 * Function: showOptionsMenu
 * Purpose:  Displays a menu and waits for the user to
 *           select either 1-Dice or 2-Dice mode.
 **************************************************/
void showOptionsMenu() {
  while (true) {
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0);
    display.println(" Dice Roll");
    display.println(""); 
    display.println("SW1 1-DIE");
    display.println("SW2 2-DICE");
    display.display();

    if (digitalRead(BUTTON_ONE) == PRESSED) {
      numberOfDice = 1;
      tone(PIEZO, 1200, 100);
      break;
    }
    if (digitalRead(BUTTON_TWO) == PRESSED) {
      numberOfDice = 2;
      tone(PIEZO, 1400, 100);
      break;
    }
    delay(10);
  }

  while (digitalRead(BUTTON_ONE) == PRESSED || digitalRead(BUTTON_TWO) == PRESSED) {
    delay(10);
  }
}

/**************************************************
 * Function: loop
 * Purpose:  Handles input and updates the display.
 **************************************************/
void loop() {
  handleInput(BUTTON_TWO);
  updateDisplay();
}

/**************************************************
 * Function: handleInput
 * Purpose:  Checks for button presses and triggers the
 *           appropriate dice roll animation.
 *
 * Parameters:
 *   rollButton - The digital input pin used as the roll button.
 **************************************************/
void handleInput(int rollButton) {
  if (digitalRead(rollButton) == PRESSED) {
    if (!buttonWasPressed) {
      if (numberOfDice == 1) {
        int finalValue = random(1, 7);
        playRollAnimationOneDie(finalValue);
        diceValue1 = finalValue;
        lastDiceValue1 = diceValue1;
      } else {
        int finalValue1 = random(1, 7);
        int finalValue2 = random(1, 7);
        playRollAnimationTwoDice(finalValue1, finalValue2);
        diceValue1 = finalValue1;
        diceValue2 = finalValue2;
        lastDiceValue1 = diceValue1;
        lastDiceValue2 = diceValue2;
      }
    }
    buttonWasPressed = true;
  } else {
    buttonWasPressed = false;
  }
}

/**************************************************
 * Function: playRollAnimationOneDie
 * Purpose:  Plays the roll animation and sound for a
 *           single die roll.
 *
 * Parameters:
 *   finalValue - The final value to display after animation.
 **************************************************/
void playRollAnimationOneDie(int finalValue) {
  int delayTime = 50, pitch = 1600;
  for (int i = 0; i < 8; i++) {
    display.clearDisplay();
    drawOneDie(random(1, 7), 39);
    display.display();
    tone(PIEZO, pitch, 40);
    delay(delayTime);
    delayTime += 30;
    pitch -= 150;
  }
  display.clearDisplay();
  drawOneDie(finalValue, 39);
  display.display();
  tone(PIEZO, 1200, 80);
  delay(100);
  tone(PIEZO, 1400, 150);
}

/**************************************************
 * Function: playRollAnimationTwoDice
 * Purpose:  Plays the roll animation and sound for
 *           two dice.
 *
 * Parameters:
 *   finalValue1 - Final rolled value for the left die.
 *   finalValue2 - Final rolled value for the right die.
 **************************************************/
void playRollAnimationTwoDice(int finalValue1, int finalValue2) {
  int delayTime = 50, pitch = 1600;
  for (int i = 0; i < 8; i++) {
    display.clearDisplay();
    drawOneDie(random(1, 7), 9);
    drawOneDie(random(1, 7), 69);
    display.display();
    tone(PIEZO, pitch, 40);
    delay(delayTime);
    delayTime += 30;
    pitch -= 150;
  }
  display.clearDisplay();
  drawOneDie(finalValue1, 9);
  drawOneDie(finalValue2, 69);
  display.display();
  tone(PIEZO, 1200, 80);
  delay(100);
  tone(PIEZO, 1400, 150);
}

/**************************************************
 * Function: updateDisplay
 * Purpose:  Updates the displayed dice only if their
 *           values have changed.
 **************************************************/
void updateDisplay() {
  bool needsRedraw = (diceValue1 != lastDiceValue1) || (diceValue2 != lastDiceValue2);
  
  if (needsRedraw) {
    display.clearDisplay();
    if (numberOfDice == 1) {
      drawOneDie(diceValue1, 39);
    } else {
      drawOneDie(diceValue1, 9);
      drawOneDie(diceValue2, 69);
    }
    display.display();
    lastDiceValue1 = diceValue1;
    lastDiceValue2 = diceValue2;
  }
}

/**************************************************
 * Function: drawPipHelper
 * Purpose:  Draws a single pip at the specified location.
 *
 * Parameters:
 *   x       - X position of pip within die face.
 *   y       - Y position of pip within die face.
 *   offsetX - Horizontal positioning of the die on screen.
 **************************************************/
void drawPipHelper(int x, int y, int offsetX) {
  display.fillCircle(x + offsetX, y + 7, 3, SSD1306_WHITE);
}

/**************************************************
 * Function: drawOneDie
 * Purpose:  Draws one die graphic at a given horizontal
 *           position on the display.
 *
 * Parameters:
 *   val     - Die value (1–6).
 *   offsetX - Horizontal offset for die placement.
 **************************************************/
void drawOneDie(int val, int offsetX) {
  display.drawRoundRect(offsetX, 7, 50, 50, 5, SSD1306_WHITE);
  
  switch (val) {
    case 1:
      drawPipHelper(25, 25, offsetX);
      break;
    case 2:
      drawPipHelper(13, 13, offsetX); 
      drawPipHelper(37, 37, offsetX);
      break;
    case 3:
      drawPipHelper(13, 13, offsetX); 
      drawPipHelper(25, 25, offsetX); 
      drawPipHelper(37, 37, offsetX);
      break;
    case 4:
      drawPipHelper(13, 13, offsetX); 
      drawPipHelper(37, 13, offsetX); 
      drawPipHelper(13, 37, offsetX); 
      drawPipHelper(37, 37, offsetX);
      break;
    case 5:
      drawPipHelper(13, 13, offsetX); 
      drawPipHelper(37, 13, offsetX); 
      drawPipHelper(25, 25, offsetX); 
      drawPipHelper(13, 37, offsetX); 
      drawPipHelper(37, 37, offsetX);
      break;
    case 6:
      drawPipHelper(13, 13, offsetX); 
      drawPipHelper(37, 13, offsetX); 
      drawPipHelper(13, 25, offsetX); 
      drawPipHelper(37, 25, offsetX); 
      drawPipHelper(13, 37, offsetX); 
      drawPipHelper(37, 37, offsetX);
      break;
  }
}
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

京野ななか More In Our Free Instructional Guidebook

This comprehensive guidebook for the 京野ななか 诛仙动漫在线观看全集免费观看 Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of 诛仙动漫在线观看全集免费观看s, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on 京野ななかing, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Project 1.00 Blink

In this project, you’ll 京野ななか how to blink an LED!

Project 1.01 Blink x2

In this project, you’ll 京野ななか how to blink more than one LED!