Course : STEM Erasmus

Course code : 1940391312

1940391312 - ΑΝΘΙΜΟΣ ΜΙΣΑΗΛΙΔΗΣ

Course Units

PR_Ard_02 Color Game

ARDUINO CODE for MIND+

 

#include <MPython.h>
#include <U8g2lib.h>
#include <DFRobot_NeoPixel.h>

// --- Οθόνη (Διορθωμένο το U8X8_PIN_NONE με κεφαλαία) ---
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2_12832(U8G2_R0, U8X8_PIN_NONE);

// --- LED & Pins ---
#define LED_PIN 25
DFRobot_NeoPixel neoPixel_25;

int touchPins[] = {12, 13, 14, 15, 27};
int THRESHOLD = 15;

// Μεταβλητή Σκορ
int currentScore = 0;

// Τα 5 χρώματα
uint32_t myColors[] = {0xFF0000, 0x00FF00, 0xFFFF00, 0x0000FF, 0xFF1493};
String colorNames[] = {"RED", "GREEN", "YELLOW", "BLUE", "PINK"};

// Χάρτης Αντιστροφής LED
int ledMapping[] = {4, 3, 2, 1, 0};

// Συναρτήσεις Οθόνης
void oled12832Begin(uint8_t addr, uint8_t dir) {
u8g2_12832.setI2CAddress(addr << 1);
u8g2_12832.begin();
u8g2_12832.enableUTF8Print();
u8g2_12832.setFont(u8g2_font_ncenB14_tr);
u8g2_12832.setFontDirection(dir);
}

void oledPrint(String text, int x, int y) {
u8g2_12832.firstPage();
do {
u8g2_12832.setCursor(x, y);
u8g2_12832.print(text);
} while ( u8g2_12832.nextPage() );
}

// --- Γραφικά Φατσών (Dot-Matrix Style) ---

// Σχεδιάζει μια Χαρούμενη Φατσούλα στο κέντρο
void drawHappyFaceDotMatrix() {
u8g2_12832.firstPage();
do {
// Σχεδιασμός Κύκλου Face
u8g2_12832.drawFrame(48, 0, 32, 32);

// Μάτια (Dot-Matrix στυλ, κουτάκια)
u8g2_12832.drawBox(56, 8, 4, 4); // Αριστερό
u8g2_12832.drawBox(68, 8, 4, 4); // Δεξί

// Χαμόγελο (retro στυλ με γραμμές)
u8g2_12832.drawLine(54, 20, 56, 22);
u8g2_12832.drawLine(56, 22, 60, 24);
u8g2_12832.drawLine(60, 24, 68, 24);
u8g2_12832.drawLine(68, 24, 72, 22);
u8g2_12832.drawLine(72, 22, 74, 20);

} while ( u8g2_12832.nextPage() );
}

// Σχεδιάζει μια Λυπημένη Φατσούλα στο κέντρο
void drawSadFaceDotMatrix() {
u8g2_12832.firstPage();
do {
// Σχεδιασμός Κύκλου Face
u8g2_12832.drawFrame(48, 0, 32, 32);

// Μάτια (Dot-Matrix στυλ, κουτάκια)
u8g2_12832.drawBox(56, 8, 4, 4); // Αριστερό
u8g2_12832.drawBox(68, 8, 4, 4); // Δεξί

// Λυπημένο Στόμα (retro στυλ με γραμμές)
u8g2_12832.drawLine(54, 24, 56, 22);
u8g2_12832.drawLine(56, 22, 60, 20);
u8g2_12832.drawLine(60, 20, 68, 20);
u8g2_12832.drawLine(68, 20, 72, 22);
u8g2_12832.drawLine(72, 22, 74, 24);

} while ( u8g2_12832.nextPage() );
}

void clearEverything() {
for(int i=0; i<5; i++) neoPixel_25.setRangeColor(i, i, 0x000000);
u8g2_12832.clearDisplay();
}

int getTouchedButton() {
for(int i=0; i<5; i++) {
if(touchRead(touchPins[i]) < THRESHOLD) {
delay(50);
if(touchRead(touchPins[i]) < THRESHOLD) {
return i;
}
}
}
return -1;
}

void waitForRelease() {
bool isTouching = true;
while(isTouching) {
isTouching = false;
for(int i=0; i<5; i++) {
if(touchRead(touchPins[i]) < THRESHOLD) {
isTouching = true;
}
}
delay(10);
}
delay(200);
}

void setup() {
Serial.begin(115200);

mPython.begin();
buzz.redirect(D8);

oled12832Begin(0x3c, 0);
neoPixel_25.begin(LED_PIN, 5, 180);

clearEverything();
delay(1000);
}

void loop() {
// Σιγουρευόμαστε ότι το δάχτυλο έχει σηκωθεί
waitForRelease();

// Εμφάνιση του START...
oledPrint("START...", 20, 25);

// Φάση 1: ΑΝΑΜΟΝΗ
int startBtn = -1;
while(startBtn == -1) {
startBtn = getTouchedButton();
delay(10);
}

buzz.freq(1000, 100);
randomSeed(millis());
waitForRelease();

oledPrint("LOOK!", 35, 25);

// Ανακάτεμα
for (int i = 4; i > 0; i--) {
int j = random(0, i + 1);
uint32_t tempColor = myColors[i];
myColors[i] = myColors[j];
myColors[j] = tempColor;

String tempName = colorNames[i];
colorNames[i] = colorNames[j];
colorNames[j] = tempName;
}

// Άναμμα LED
for (int i = 0; i < 5; i++) {
int mappedLed = ledMapping[i];
neoPixel_25.setRangeColor(mappedLed, mappedLed, myColors[i]);
}
delay(3000);

clearEverything();
int targetIdx = random(0, 5);
String targetName = colorNames[targetIdx];

delay(200);
oledPrint(targetName, 35, 25);

// Φάση 2: ΑΠΑΝΤΗΣΗ
int answerBtn = -1;
while(answerBtn == -1) {
answerBtn = getTouchedButton();
delay(10);
}

// Φάση 3: ΕΛΕΓΧΟΣ ΚΑΙ ΒΑΘΜΟΛΟΓΙΑ
if(answerBtn == targetIdx) {
// Σωστό: +1 βαθμός
currentScore += 1;

// Εμφάνιση Graphical Χαρούμενης Φατσούλας
drawHappyFaceDotMatrix();

int mappedAnswerLed = ledMapping[answerBtn];
neoPixel_25.setRangeColor(mappedAnswerLed, mappedAnswerLed, myColors[answerBtn]);

buzz.freq(800, 150);
delay(150);
buzz.freq(1200, 250);

} else {
// Λάθος: -1 βαθμός
currentScore -= 1;
if (currentScore < 0) {
currentScore = 0;
}

// Εμφάνιση Graphical Λυπημένης Φατσούλας
drawSadFaceDotMatrix();

int mappedTargetLed = ledMapping[targetIdx];
neoPixel_25.setRangeColor(mappedTargetLed, mappedTargetLed, myColors[targetIdx]);

buzz.freq(200, 500);
}

delay(1500);

// Εμφάνιση Σκορ
String scoreText = "SCORE: " + String(currentScore);
oledPrint(scoreText, 15, 25);

delay(2000);
clearEverything();
}