#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>
#include <EEPROM.h>
int moveCount[20];
int motorPower = 255;
int turnSpeed = 77;
int sensorValue = 0;
boolean isMoving = false;
void setup() {
Robot.begin();
Serial.begin(9600);
Robot.beginSpeaker();
Robot.beginTFT();
Robot.beginSD();
Robot.fillScreen(0x0099FF);
Robot.drawRect(0,0,128,20, 0xFFFFFF);
Robot.fillRect(0,0,128,20, 0xFFFFFF);
Robot.stroke(0,0,0);
Robot.text(" Arduino Robot",0, 5);
Robot.drawBMP("buttons.bmp",95,127);
}
void loop() {
clearMoves();
addMoves();
delay(1000);
executeMoves();
}
void clearMoves() {
for(int i = 0; i < 20; i++) {
moveCount[i]=-1;
}
}
void addMoves() {
Robot.stroke(255,255,255);
Robot.text("-Buttons to add moves", 0, 24);
Robot.stroke(255,255,255);
Robot.text("-Middle to end input", 0, 33);
Robot.drawLine(0,45,128,45, 0xFFFFFF);
for(int i =0; i < 20;) {
int key = Robot.keyboardRead();
if(key == BUTTON_MIDDLE) {
break;
} else if(key == BUTTON_NONE) {
continue;
}
moveCount[i]=key;
PrintSequence(i,50);
delay(100);
i++;
}
}
void executeMoves() {
for(int i =0; i < 20; i++) {
switch(moveCount[i]) {
case BUTTON_LEFT:
turnLeft();
break;
case BUTTON_RIGHT:
turnRight();
break;
case BUTTON_UP:
moveForward();
break;
case BUTTON_DOWN:
moveBackward();
break;
}
Robot.text("Performing moves...", 5,70);
Robot.stroke(255,0,0);
PrintSequence(i,86);
delay(1000);
Robot.motorsStop();
delay(1000);
Robot.stroke(255,255,255);
if(moveCount[i] == (-1))
{
Robot.text("Completed!",5,103);
isMoving = false;
delay(2000);
setup();
break;
}
}
}
void moveForward() {
Robot.motorsWrite(motorPower, motorPower);
delay(200);
Robot.motorsStop();
isMoving = true;
}
void moveBackward() {
Robot.motorsWrite(-motorPower, -motorPower);
delay(200);
Robot.motorsStop();
isMoving = true;
}
void turnLeft() {
Robot.motorsWrite(turnSpeed, -turnSpeed);
isMoving = true;
}
void turnRight() {
Robot.motorsWrite(-turnSpeed, turnSpeed);
isMoving = true;
}
char keyToChar(int key) {
switch(key) {
case BUTTON_LEFT:
return '<';
case BUTTON_RIGHT:
return '>';
case BUTTON_UP:
return '^';
case BUTTON_DOWN:
return 'v';
}
}
void PrintSequence(int i, int originY) {
Robot.text(keyToChar(moveCount[i]), i%14*8+5, i/14*10+originY);
}
void playMelody() {
char aTinyMelody[] = "8eF#AdB";// This is what we will play
Robot.playMelody(aTinyMelody);// Play the melody
}
I am working with the code and robot above. I want to use EEPROMs on my robot but I don't know how or where to use it. I've had a look for examples but had no luck. What I want to do is store the move sequences from the addMoves method and when I turn it off and back on, it should retain and perform the most recent move sequences.
Can somebody help on how I could implement this?