SunFounder 3in1 Kit
void setup() {
...
Serial.begin(9600);
//Serial.println(EEPROM.length());
}
To find the size of your board’s EEPROM memory, uncomment the line Serial.println(EEPROM.
read(i)). This will print the size of EEPROM in the serial monitor, and you can change the value
of the variable resolution accordingly.
3. As soon as a button press is detected, then recording begins and the required information is entered via a poten-
tiometer. Now the board repeats your action endlessly (and flashes an LED for each repetition) until you press
the button again, recording a new action.
void loop() {
if (recording == true) {//record
for (int i = 1; i <= resolution; i++) {
digitalWrite(ledPin, HIGH); //light status led
int val = map(analogRead(A0), 0, 1023, 0, 180);
EEPROM.write(i, val);
//Serial.println(EEPROM.read(i));
myServo.write(val);
delay(recordTime);
}
digitalWrite(ledPin, LOW); //turn off status led
delay(1000);//give time for person
recording = false;
}
else {
for (int i = 1; i <= resolution; i++) {//playback
if (digitalRead(buttonPin) == 0) {//stop playback and record␣
˓→new values
recording = true;
break;
}
int readval = EEPROM.read(i);
myServo.write(readval);
//Serial.println(readval);
delay(recordTime);
}
digitalWrite(ledPin, HIGH); //show a new repeat
delay(100);
digitalWrite(ledPin, LOW);
}
}
• Make the variable recording true when the button is pressed.
• When the variable recording is true, start recording the action in the memory range.
• Read the value of the potentiometer and map it to 0-180 to store it in EEPROM and control the
rotation of the servo.
• The LED lights up at the start of recording and goes off at the end.
• Repeat the recorded action with a quick flash of the LED to remind you of a new repeat.
4.5. 5. More Syntax 193