#include "SevSeg.h" // Include the 7-segment library
#include <Arduino.h>
SevSeg sevseg; // Create an instance of the 7-segment display
const int ledPin = 2; // Pin for the LED
int stickmanPosition = 0; // Position of the stickman (0-9)
bool isAlive = true; // Status of the stickman
unsigned long previousMillis = 0; // For timing
const long interval = 500; // Update interval (milliseconds)
const int obstacleCount = 3; // Number of obstacles
int obstaclePositions[obstacleCount]; // Array to store obstacle positions
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
// Configure the 7-segment display
byte numDigits = 1;
byte digitPins[] = {};
byte segmentPins[] = {5, 18, 19, 21, 22, 23, 4}; // Adjust these pins as needed
bool resistorsOnSegments = true;
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90); // Set brightness level (0-100)
randomSeed(analogRead(0)); // Initialize random seed
// Initialize random obstacle positions
for (int i = 0; i < obstacleCount; i++) {
obstaclePositions[i] = random(0, 10);
}
}
void loop() {
if (isAlive) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Move stickman
stickmanPosition = (stickmanPosition + 1) % 10; // Loop through positions 0-9
// Check for collision with obstacles
for (int i = 0; i < obstacleCount; i++) {
if (stickmanPosition == obstaclePositions[i]) {
isAlive = false; // Stickman hits an obstacle
digitalWrite(ledPin, HIGH); // Turn on LED to indicate death
}
}
// Display stickman position or obstacles
sevseg.setNumber(stickmanPosition);
sevseg.refreshDisplay();
// Move obstacles
for (int i = 0; i < obstacleCount; i++) {
// Randomly reposition the obstacles every few iterations
if (random(0, 10) < 3) { // 30% chance to move each obstacle
obstaclePositions[i] = random(0, 10);
}
}
}
} else {
// If dead, display "X" on the 7-segment (or another indicator)
sevseg.setNumber(11); // Assuming 11 represents "X"
sevseg.refreshDisplay();
}
}