Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <Servo.h>
- Servo myServo;
- SoftwareSerial hc06(2,3);
- char cmd[100];
- int cmdIndex;
- void setup(){
- //Initialize Serial Monitor
- Serial.begin(9600);
- //Initialize Bluetooth Serial Port
- hc06.begin(9600);
- cmdIndex = 0;
- myServo.attach(4);
- }
- void loop(){
- //Write data from HC06 to Serial Monitor
- if (hc06.available()){
- char c = hc06.read();
- if(c=='\n') {
- cmd[cmdIndex] = 0;
- exeCmd(); // execute the command
- cmdIndex = 0; // reset the cmdIndex
- } else {
- cmd[cmdIndex] = c;
- if(cmdIndex<99) cmdIndex++;
- }
- }
- }
- void exeCmd() {
- // "servo" is the servo motor id
- if(cmd[0]=='0' &&
- cmd[1]==' ') {
- int val = 0;
- for(int i=2; cmd[i]!=0; i++) {
- val = val*10 + (cmd[i]-'0');
- }
- Serial.println(val);
- myServo.write(val);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement