Cleanup slave software

This commit is contained in:
Kenwood 2021-06-29 22:46:25 -04:00
parent 8fede6107b
commit c0b5d40653
1 changed files with 14 additions and 10 deletions

View File

@ -7,6 +7,7 @@
http://www.arduino.cc/en/Tutorial/Sweep http://www.arduino.cc/en/Tutorial/Sweep
*/ */
#include <Wire.h>
#include <Servo.h> #include <Servo.h>
Servo myservo; // create servo object to control a servo Servo myservo; // create servo object to control a servo
@ -17,17 +18,20 @@ int pos = 0; // variable to store the servo position
void setup() { void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object myservo.attach(9); // attaches the servo on pin 9 to the servo object
for (pos = 70; pos <= 130; pos += 1) { // goes from 0 degrees to 180 degrees Wire.begin(0x8);
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos' // Call receiveEvent when data received
delay(15); // waits 15ms for the servo to reach the position Wire.onReceive(receiveEvent);
}
for (pos = 130; pos >= 70; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
} }
void loop() { void loop() {
delay(1000); delay(10);
}
void receiveEvent(int howMany) {
while (Wire.available()) { // loop through all but the last
int pos = Wire.read(); // receive byte as a int
myservo.write(pos);
}
} }