Members | Sign In
Legacy MOVI User Community Forum (readonly) > MOVI Question & Answers
avatar

Looking for sample code using a servo

posted Oct 19, 2017 17:41:51 by James Sherman
Hi all,
I'm looking for examples controlling servos. Can anyone help me with a simple code example?

Thanks
[Last edited Oct 21, 2017 01:37:50]
page   1
1 reply
avatar
ThroyeMoon said Oct 21, 2017 06:40:04
Well after a few hours of tinkering with the LightSwitch example and a Servo Sweep sketches I figured it out. I have tested it and verified it works. BTW i'm just a beginner so for those looking for a quick MOVI servo sketch here it is:
/****************************************************************************
* This is an example for the use of Audeme's MOVI(tm) Voice Control Shield *
* ----> http://www.audeme.com/MOVI/ *
* This code is inspired and maintained by Audeme but open to change *
* and organic development on GITHUB: *
* ----> https://github.com/audeme/MOVIArduinoAPI *
* Written by Gerald Friedland for Audeme LLC. *
* Contact: fractor@audeme.com *
* BSD license, all text above must be included in any redistribution. *
****************************************************************************
*
* This basic example shows how to use MOVI(tm)'s API to train the call
* sign "Arduino" and two sentences. When the sentences are recognized
* they switch on and off an LED on PIN D13. Many boards have a hardwired
* LED on board already connected to D13.
*
* Circuitry:
* LED is pin D13 and GND
* Arduino UNO R3, MEGA2560 R3, or Arduino Leonardo R3.
* Connect speaker to MOVI.
* IMPORTANT: Always use external power supply with MOVI.
*
* Other Arduino-compatible boards:
* Consult MOVI's User Manual before connecting MOVI.
*
* If you long-press the button on the MOVI (for a couple seconds),
* MOVI will revert back to the call sign and sentences trained here.
*/

#include "MOVIShield.h" // Include MOVI library, needs to be *before* the next #include
#include <Servo.h>

#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_PIC32)
#include <SoftwareSerial.h> // This is nice and flexible but only supported on AVR and PIC32 architecture, other boards need to use Serial1
#endif

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position
MOVI recognizer(true); // Get a MOVI object, true enables serial monitor interface, rx and tx can be passed as parameters for alternate communication pins on AVR architecture

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object

recognizer.init(); // Initialize MOVI (waits for it to boot)

//*
// Note: training can only be performed in setup().
// The training functions are "lazy" and only do something if there are changes.
// They can be commented out to save memory and startup time once training has been performed.
recognizer.callSign("Arduino"); // Train callsign Arduino (may take 20 seconds)
recognizer.addSentence("Shield up"); // Add sentence 1
recognizer.addSentence("Shield down"); // Add sentence 2
recognizer.train(); // Train (may take 20seconds)
//*/

// recognizer.setThreshold(5); // uncomment and set to a higher value (valid range 2-95) if you have a problems due to a noisy environment.
}

void loop() // run over and over
{
signed int res=recognizer.poll(); // Get result from MOVI, 0 denotes nothing happened, negative values denote events (see docs)
if (res==1) { // Sentence 1.
for (pos = 0; pos <= 180; 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
}
if (res==2) { // Sentence 2
for (pos = 180; pos >= 0; 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
}
// Do more ...
}
[Last edited Oct 21, 2017 06:41:14]
Login below to reply: