Copy in example servo code, pwm is way better!

This commit is contained in:
Kenwood 2021-06-28 01:23:43 -04:00
parent 7cc49a0406
commit c2439c8fa1
1 changed files with 22 additions and 28 deletions

View File

@ -1,37 +1,31 @@
/* /* Sweep
Arduino Slave for Raspberry Pi Master by BARRAGAN <http://barraganstudio.com>
i2c_slave_ard.ino This example code is in the public domain.
Connects to Raspberry Pi via I2C
modified 8 Nov 2013
DroneBot Workshop 2019 by Scott Fitzgerald
https://dronebotworkshop.com http://www.arduino.cc/en/Tutorial/Sweep
*/ */
// Include the Wire library for I2C #include <Servo.h>
#include <Wire.h>
// LED on pin 13 Servo myservo; // create servo object to control a servo
const int ledPin = 13; // twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() { void setup() {
// Join I2C bus as slave with address 8 myservo.attach(9); // attaches the servo on pin 9 to the servo object
Wire.begin(0x8);
// Call receiveEvent when data received
Wire.onReceive(receiveEvent);
// Setup pin 13 as output and turn LED off
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
} }
// Function that executes whenever data is received from master void loop() {
void receiveEvent(int howMany) { for (pos = 70; pos <= 130; pos += 1) { // goes from 0 degrees to 180 degrees
while (Wire.available()) { // loop through all but the last // in steps of 1 degree
char c = Wire.read(); // receive byte as a character myservo.write(pos); // tell servo to go to position in variable 'pos'
digitalWrite(ledPin, c); 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
} }
} }
void loop() {
delay(100);
}