Create/modify read string code

This commit is contained in:
Kenwood 2021-07-03 22:54:50 -04:00
parent 67daffd667
commit aa575f67a3
1 changed files with 14 additions and 5 deletions

View File

@ -10,16 +10,23 @@
// This servo is used to wipe and clean the camera lens // This servo is used to wipe and clean the camera lens
Servo windowWiperServo; Servo windowWiperServo;
// This is a variable capable of storing up to 4 items
char instruction[4] = "";
void setup() { void setup() {
// Attach the wiper servo to pin 9 // Attach the wiper servo to pin 9
windowWiperServo.attach(9); windowWiperServo.attach(9);
// This is only useful for debugging
Serial.begin(9600);
// This is the address the pi will speak to us at // This is the address the pi will speak to us at
Wire.begin(0x8); Wire.begin(0x8);
// Call receiveEvent when data received // Call receiveEvent when data received
Wire.onReceive(receiveEvent); Wire.onReceive(receiveEvent);
Serial.print("Finished Setup.");
} }
// Just loop to keep the running code alive, and wait for events to happen. // Just loop to keep the running code alive, and wait for events to happen.
@ -28,9 +35,11 @@ void loop() {
} }
// This method runs when we receive a message // This method runs when we receive a message
void receiveEvent(int howMany) { void receiveEvent(int n) {
while (Wire.available()) { // loop through all but the last for (int i = 0; i < n; i++) //n is equal to the number of data bytes received
int pos = Wire.read(); // receive byte as a int {
windowWiperServo.write(pos); instruction[i] = Wire.read();
} }
Serial.print(instruction);
} }