Hi everybody,
as a new hobby I am starting to tinker with Arduino. I saw a robot hand with Arduino that plays Rock Paper Scissors. I tried to replicate it. with some help in the forum, I ended up with this code. but it still doesn't work. can anyone help?
#include <Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <SPI.h>
#include <Wire.h>
#include <Fonts/FreeSerif9pt7b.h>
#define i2c_Address 0x3c
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G Display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Servo thumb;
Servo index;
Servo middle;
Servo ring;
Servo pinky;
int emgPin = A0; // EMG sensor pin
int emgThreshold = 500; // Adjust the threshold based on sensor readings
void setup() {
Serial.begin(9600);
thumb.attach(13); // Thumb servo connected to pin 13
index.attach(12); // Index servo connected to pin 12
middle.attach(11); // Middle servo connected to pin 11
ring.attach(10); // Ring servo connected to pin 10
pinky.attach(9); // Pinky servo connected to pin 9
thumb.write(0);
index.write(0);
middle.write(0);
ring.write(0);
pinky.write(0);
Display.clearDisplay();
Display.setTextSize(1);
Display.setFont(&FreeSerif9pt7b);
Display.setTextColor(SH110X_WHITE);
Display.setCursor(15, 10);
Display.println("Robotic Hand");
delay(10000);
Display.clearDisplay();
Display.setCursor(15, 30);
Display.println("Rock-Paper-Scissor!");
Display.setCursor(15, 60);
Display.println("Testing");
thumb.write(180);
delay(250);
index.write(180);
delay(250);
middle.write(180);
delay(250);
ring.write(180);
delay(250);
pinky.write(180);
delay(250);
thumb.write(0);
delay(250);
index.write(0);
delay(250);
middle.write(0);
delay(250);
ring.write(0);
delay(250);
pinky.write(0);
delay(250);
pinMode(emgPin, INPUT); // EMG sensor pin as input
}
void loop() {
Serial.begin(9600);
int emgValue = analogRead(emgPin);
Display.clearDisplay();
Display.setCursor(15, 30);
Display.println("Choose:");
Display.setCursor(15, 60);
Display.println("1:Rock 2:Paper 3:Scis");
if (emgValue > emgThreshold) {
int choice = getChoice();
displayChoice(choice);
playGame(choice);
}
}
int getChoice() {
while (true) {
if (Serial.available()) {
int choice = Serial.parseInt();
if (choice >= 1 && choice <= 3) {
return choice;
}
}
}
}
void displayChoice(int choice) {
Display.clearDisplay();
Display.setCursor(15, 30);
Display.println("I choice:");
switch (choice) {
case 1:
Display.setCursor(15, 60);
Display.println("Rock!");
break;
case 2:
Display.setCursor(15, 60);
Display.println("Paper!");
break;
case 3:
Display.setCursor(15, 60);
Display.println("Scissors!");
break;
}
}
void playGame(int choice) {
// Open/close fingers based on the selected choice
switch (choice) {
case 1: // Rock
thumb.write(180);
index.write(180);
middle.write(180);
ring.write(180);
pinky.write(180);
delay(2500);
thumb.write(0);
index.write(0);
middle.write(0);
ring.write(0);
pinky.write(0);
delay(500);
break;
case 2: // Paper
thumb.write(180);
index.write(180);
middle.write(180);
ring.write(180);
pinky.write(180);
delay(500);
thumb.write(0);
index.write(0);
middle.write(0);
ring.write(0);
pinky.write(0);
delay(2500);
break;
case 3: // Scissors
thumb.write(180);
index.write(0);
middle.write(0);
ring.write(180);
pinky.write(180);
delay(2500);
thumb.write(0);
index.write(0);
middle.write(0);
ring.write(0);
pinky.write(0);
delay(500);
break;
}
}