#include #include /* The circuit: * LCD RS pin to digital pin 2 * LCD Enable pin to digital pin 3 * LCD D4 pin to digital pin 4 * LCD D5 pin to digital pin 5 * LCD D6 pin to digital pin 6 * LCD D7 pin to digital pin 7 * LCD R/W pin to ground */ LiquidCrystal lcd(2, 3, 4, 5, 6, 7); Servo futaba; // create a servo object const int game_Pin = 8; const int button_Pin = 11; const int ledpower_Pin = 12; const int led_Pin = 13; const int pot1_Pin = A0; // analog pin connected to the linear potentiometer (position of the handle) int pot1_Value = 0; // value read from the linear potentiometer const int pot2_Pin = A1; // analog pin connected to the circular potentiometer (parameters choice) int pot2_Value = 0; // value read from the level potentiometer int counter; // counter used to select the parameters in the beginning int level; // level of difficulty int exercises; // number of exercises to do to achieve the level int target; // target position for the motor int mode; // to know the mode the user has chosen (reflex and video game modes are off by default) int done; // number of exercices that are done (0 at the begining) int ledState; // state of the led for the reflex game (turned off by default) int timeOn; // the time during which the led has been turned on int complete; // check if the movement of the handle is completed before the led turn off (in reflex mode) int check; // check if the exercise is done completely int gender; // choose the gender of the user int pot1_lcd; // values used to display the progress bar int old_pot1_lcd; void setup() { futaba.attach(9); // the digital pin 9 will control the servo motor futaba.write(0); // initialize the motor at the upper position (level 1) counter = 0; level = 1; done = 0; mode = 0; ledState = 0; timeOn = 0; check = 0; pot1_lcd = 0; old_pot1_lcd = 0; lcd.begin(20, 4); lcd.setCursor(3, 0); lcd.print("Welcome to the"); lcd.setCursor(0, 1); delay(800); lcd.print("Rehabilitation Robot"); delay(2500); lcd.clear(); lcd.setCursor(4, 0); lcd.print("Choose Your"); lcd.setCursor(4, 1); lcd.print("Parameters:"); delay(2000); lcd.clear(); pinMode(button_Pin, INPUT); // the button is an input pinMode(led_Pin, OUTPUT); // the led is an output pinMode(ledpower_Pin, OUTPUT); // led which indicates that the device is on pinMode (game_Pin, OUTPUT); // digital output for the joystick digitalWrite(ledpower_Pin, HIGH); // turn on the power led digitalWrite(game_Pin, HIGH); } //****************************************************************************************// void parameters() { while (counter < 4) { if (counter == 0) { lcd.setCursor(0,0); int value = analogRead(pot2_Pin); level = map(value,0,1023,1,10); target = map(value,0,1023,0,130); // to know where the motor should go to be at the chosen level lcd.print("Level:"); lcd.print(level); lcd.print(" "); } if (counter == 1) { lcd.setCursor(0,1); lcd.print("Exercises number:"); exercises = analogRead(pot2_Pin); exercises = map(exercises,0,1023,1,50); lcd.print(exercises); lcd.print(" "); } if (counter == 2) { lcd.setCursor(0,2); lcd.print("Mode:"); if (analogRead(pot2_Pin) < 250) { lcd.print("Challenge "); mode = 0; } else if (analogRead(pot2_Pin) > 250 && analogRead(pot2_Pin) < 500) { lcd.print("Reflex "); mode = 1; } else if (analogRead(pot2_Pin) > 500 && analogRead(pot2_Pin) < 750) { lcd.print("Video Game"); mode = 2; } else if (analogRead(pot2_Pin) > 750) { lcd.print("Classic "); mode = 3; } } if (counter == 3) { lcd.setCursor(0,3); lcd.print("Gender:"); if (analogRead(pot2_Pin) < 500) { lcd.print("Man "); gender = 0; } else { lcd.print("Woman"); gender = 1; } } if (digitalRead(button_Pin) == 0) // when the selection button is pressed { delay(750); counter++; } } if (counter == 4) // the choice of the parameters is finished { lcd.clear(); if (gender == 1) target = target/2; // if the user is a woman, the difficutly will be easier counter++; } } void setMotorPos() { futaba.write(target); } void ProgressBar() { pot1_Value = analogRead(pot1_Pin); pot1_Value = constrain(pot1_Value, 150, 900); // the handle potentiometer is not at zero when the handle is at rest pot1_lcd = map(pot1_Value, 150, 900, 20, 0); // change the scale of pot1_Value if (pot1_lcd > old_pot1_lcd) // check if the number changes to a higher number { for (int i=0; i <= pot1_lcd; i++) // start at the bottom left and go on { lcd.setCursor(i, 3); lcd.write(1023); // display a block } } if (pot1_lcd <= old_pot1_lcd) // check if the number changed to a smaller number { for (int j=19; pot1_lcd <= j; j--) // start at the bottom right and go back { lcd.setCursor(j, 3); lcd.write(1022); // display a blank } } old_pot1_lcd = pot1_lcd; } void exerciseCounter() { if (analogRead(pot1_Pin) > 850) check = 0; if (analogRead(pot1_Pin) < 250 && check == 0) // if the handle is pulled enough { done++; check = 1; } } void VideoGameMode() { lcd.setCursor(5,0); lcd.print ("Welcome to"); lcd.setCursor(5,1); lcd.print ("Mario Kart"); if (analogRead(pot1_Pin) > 800) { check = 0; digitalWrite(game_Pin, HIGH); // the button of the joystick is not "pushed" } if (analogRead(pot1_Pin) < 350 && check == 0) // if the handle is pulled enough { digitalWrite(game_Pin, LOW); // "push" the button of the joystick check = 1; } if (digitalRead(button_Pin) == 0) // when the selection button is pressed { delay(750); resetGame(); // we get out of the video game mode and we restart the game } } void reflexMode() { timeOn = 0; ledState = 0; digitalWrite(led_Pin, ledState); delay(random (700,5000)); ledState = 1; digitalWrite(led_Pin, ledState); while (timeOn < 750) // check if the exercice was done before the led turned off { if (analogRead(pot1_Pin) < 350 && check == 0) { done++; check = 1; } delay(1); timeOn++; } if (check==0 && done != 0) done--; // if the user didn't achieved to pull before the led turned off, he loses one exercice } void classicMode() { if (done == exercises) { lcd.clear(); lcd.setCursor(2,0); lcd.print ("Congratulations!"); lcd.setCursor(3,1); if (gender == 0) lcd.print ("You're the man!"); else lcd.print("That's my girl!"); delay (2000); lcd.clear(); resetGame(); } } void challengeMode() { if (done == exercises) { level++; target = target + 13; // set the position of the motor because the level has increased done = 0; } if (level == 11) { lcd.clear(); lcd.setCursor(2,0); lcd.print ("Congratulations!"); lcd.setCursor(3,1); if (gender == 0) lcd.print ("You're the man!"); else lcd.print("That's my girl!"); delay (2000); lcd.clear(); resetGame(); } } // reset of every parameters void resetGame() { lcd.clear(); counter = 0; level = 1; exercises = 1; target = 0; done = 0; check = 0; setMotorPos(); } //****************************************************************************************// void loop() { parameters(); setMotorPos(); if (mode!=1) ProgressBar(); if (mode != 2) // if we are not in video game mode { exerciseCounter(); // exerciceCounter returns the number of exercices already done lcd.setCursor(0,0); lcd.print ("Level:"); lcd.print (level); lcd.print (" "); lcd.setCursor(0,1); lcd.print ("Exercises done:"); lcd.print (done); lcd.print (" "); } if (mode == 1) { reflexMode(); // if the user has chosen the reflex mode classicMode(); // to know if the user won the reflex mode game } else if (mode == 2) VideoGameMode(); else if (mode == 3) classicMode(); else challengeMode(); }