From c0b5d40653c2c72130dd41e187068efaae1857fe Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Tue, 29 Jun 2021 22:46:25 -0400 Subject: [PATCH] Cleanup slave software --- .../arduino/crawler_slave/crawler_slave.ino | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/crawler_software/arduino/crawler_slave/crawler_slave.ino b/crawler_software/arduino/crawler_slave/crawler_slave.ino index b38d788..2388bec 100644 --- a/crawler_software/arduino/crawler_slave/crawler_slave.ino +++ b/crawler_software/arduino/crawler_slave/crawler_slave.ino @@ -7,6 +7,7 @@ http://www.arduino.cc/en/Tutorial/Sweep */ +#include #include Servo myservo; // create servo object to control a servo @@ -17,17 +18,20 @@ int pos = 0; // variable to store the servo position void setup() { 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 - // in steps of 1 degree - myservo.write(pos); // tell servo to go to position in variable 'pos' - delay(15); // waits 15ms for the servo to reach the position - } - 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 - } + Wire.begin(0x8); + + // Call receiveEvent when data received + Wire.onReceive(receiveEvent); + } 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); + } }