-
-
Save streetalchemist/669983fa9b9bd074d604 to your computer and use it in GitHub Desktop.
Corgibot control program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int wagit(String command); //declare the wagit function so Particle can call it via API | |
// This #include statement was automatically added by the Particle IDE. | |
#include "PhotonWaveOut/PhotonWaveOut.h" | |
// connect a 8Ohm speaker to pin D1 and d2 | |
// using two pins, we can output PWM on both pins D1 for positive wave parts and D2 for the | |
// negative parts (AC DDS) | |
// if we run out of pins, we can also connect the speaker to GND and D1 and only write to D1 | |
// this will be quiter and put more stress onto the speaker (DC DDS) | |
// | |
// add a capacitor between both pins and add a resist on one pin to form a low-pass filter | |
const int pwm_p = D1; | |
const int pwm_n = D2; | |
WaveOut *wave; | |
#include "wave_data.h" | |
Servo myservo; // create servo object to control a servo | |
// a maximum of eight servo objects can be created | |
int pos = 60; // variable to store the servo position | |
bool playing = false; | |
void setup() | |
{ | |
// register the Particle function | |
Particle.function("wagit", wagit); | |
myservo.attach(D0); // attaches the servo on the D0 pin to the servo object | |
wave = new WaveOut(pwm_p, pwm_n); //setup the pins to output sound to | |
} | |
void wag () { | |
for(pos = 60; pos > 45; pos -= 1) // goes from 60 degrees to 45 degrees | |
{ // in steps of 1 degree | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(5); // waits 5ms for the servo to reach the position | |
} | |
for(pos = 45; pos<=60; pos+=1) // go back to the starting position, with a 9ms delay between steps | |
{ //to slow this motion down slightly | |
myservo.write(pos); | |
delay(9); | |
} | |
} | |
int wagit (String command) { | |
//Start sound | |
wave->play(wave_data, sizeof(wave_data), true); | |
//Wag head twice | |
wag(); | |
delay(500); | |
wag(); | |
//Wait until sound is done then stop it | |
delay(400); | |
wave->stop(); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment