Link Bauteile
| Teil | Anzahl |
|---|
| Arduino Nano | 1x |
| LCD / I²C Display (16×2) | 1x |
| Kippschalter | 9x |
| Button (Check) | 1x |
| Box.stl | 1x |
| Deckel.stl | 1x |
| Boden.stl | 1x |
| Stromversorgung (6V) | 1× |
| Kippschalter Power | 1x |
| |
| Bauteil | Arduino |
|---|
| Switch 1 (1) | D6 |
| Switch 2 (2) | D5 |
| Switch 3 (4) | D4 |
| Switch 4 (8) | D3 |
| Switch 5 (16) | A3 |
| Switch 6 (32) | A2 |
| Switch 7 (64) | A1 |
| Switch 8 (128) | A0 |
| Check Button | D2 |
| Easy/Hard | A6 |
| LCD Pin | Arduino Nano |
|---|
| GND | GND |
| VCC | 5V |
| SDA | A4 |
| SCL | A5 |
Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pins (Binary)
const int Pin0 = 6; // 1
const int Pin1 = 5; // 2
const int Pin2 = 4; // 4
const int Pin3 = 3; // 8
const int Pin4 = A3; // 16
const int Pin5 = A2; // 32
const int Pin6 = A1; // 64
const int Pin7 = A0; // 128
const int buttonPin = 2;
const int easyMode = A6;
long randNumber;
void setup() {
lcd.begin(); // wichtig für deine Library
lcd.backlight();
pinMode(Pin0, INPUT_PULLUP);
pinMode(Pin1, INPUT_PULLUP);
pinMode(Pin2, INPUT_PULLUP);
pinMode(Pin3, INPUT_PULLUP);
pinMode(Pin4, INPUT_PULLUP);
pinMode(Pin5, INPUT_PULLUP);
pinMode(Pin6, INPUT_PULLUP);
pinMode(Pin7, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(easyMode, INPUT_PULLUP);
randomSeed(analogRead(A7));
newNumber();
}
void newNumber() {
lcd.clear();
if (digitalRead(easyMode) == HIGH)
randNumber = random(0, 15);
else
randNumber = random(0, 255);
lcd.setCursor(0,0);
lcd.print("Your number is");
lcd.setCursor(0,1);
lcd.print(randNumber);
}
int readValue() {
int v = 0;
if (digitalRead(Pin0) == LOW) v += 1;
if (digitalRead(Pin1) == LOW) v += 2;
if (digitalRead(Pin2) == LOW) v += 4;
if (digitalRead(Pin3) == LOW) v += 8;
if (digitalRead(Pin4) == LOW) v += 16;
if (digitalRead(Pin5) == LOW) v += 32;
if (digitalRead(Pin6) == LOW) v += 64;
if (digitalRead(Pin7) == LOW) v += 128;
return v;
}
void printBinary() {
int pins[8] = {Pin7,Pin6,Pin5,Pin4,Pin3,Pin2,Pin1,Pin0};
for(int i=0;i<8;i++)
{
if(digitalRead(pins[i]) == LOW)
lcd.print("1");
else
lcd.print("0");
if(i==3) lcd.print(" ");
}
}
void loop() {
lcd.setCursor(7,1);
printBinary();
if(digitalRead(buttonPin)==LOW)
{
delay(50); // entprellen
int value = readValue();
if(value == randNumber)
{
lcd.clear();
lcd.print("Correct!");
lcd.setCursor(0,1);
lcd.print(randNumber);
lcd.print(" is ");
printBinary();
delay(2500);
newNumber();
}
else
{
lcd.clear();
lcd.print("Try again");
lcd.setCursor(0,1);
lcd.print(randNumber);
delay(1500);
newNumber();
}
while(digitalRead(buttonPin)==LOW); // warten bis losgelassen
}
}