User Tools

Site Tools


multiple-serial-servo-control

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

multiple-serial-servo-control [2014/09/02 20:34]
zoza created
multiple-serial-servo-control [2015/01/22 15:36]
zoza
Line 2: Line 2:
  
 <code java> <code java>
-/************************************************************************************************************************************ +/* 
- ​* ​ShiftPWM blocking RGB fades example, (c) Elco Jacobs, updated August + ------------------------------ 
-2012.+   MultipleSerialServoControl 
 + ​* ​------------------------------
  *  *
- ​* ​ShiftPWM blocking RGB fades example. This example uses simple delay + ​* ​Uses the Arduino Serial library 
-loops to create fades+ *  (http://​arduino.cc/​en/​Reference/​Serial) 
- ​* ​If you want to change ​the fading mode based on inputs ​(sensors, + ​* ​and the Arduino Servo library 
-buttons, serial), use the non-blocking example as starting point+ ​*  ​(http://​arduino.cc/​en/​Reference/​Servo) 
- ​* ​Please go to www.elcojacobs.com/shiftpwm for documentation,​ fuction + * to control multiple servos from PC using a USB cable
-reference and schematics+ * 
- ​* ​If you want to use ShiftPWM with LED strips or high power LED'​s,​ + * Dependencies:​ 
-visit the shop for boards+ ​* ​  ​Arduino 0017 or higher 
- ​************************************************************************************************************************************/+ ​* ​    ​(http://​www.arduino.cc/en/​Main/​Software) 
 + *   ​Python servo.py module 
 + ​* ​    (http://​principialabs.com/​arduino-python-4-axis-servo-control/​) 
 + * 
 + Created: ​ 23 December 2009 
 + Author: ​  Brian D. Wendt 
 +   (http://​principialabs.com/​) 
 + Version: ​ 1.1 
 + License: ​ GPLv3 
 +   (http://​www.fsf.org/​licensing/​) 
 + * 
 + */
  
 +// Import the Arduino Servo library
 +#include <​Servo.h> ​
  
-// You can choose the latch pin yourself. +// Create a Servo object for each servo 
-const int ShiftPWM_latchPin=8;+Servo servo1; 
 +Servo servo2; 
 +Servo servo3; 
 +Servo servo4;
  
-const bool ShiftPWM_invertOutputs = false; 
  
-const bool ShiftPWM_balanceLoad ​false;+// Common servo setup values 
 +int minPulse ​600  // minimum servo position, us (microseconds) 
 +int maxPulse = 2400;  // maximum servo position, us
  
-#include <​ShiftPWM.h> ​  // include ShiftPWM.h after setting the pins!+// User input for servo and position 
 +int userInput[3]; ​   // raw input from serial buffer, 3 bytes 
 +int startbyte; ​      // start byte, begin reading input 
 +int servo; ​          // which servo to pulse? 
 +int pos;             // servo angle 0-180 
 +int i;               // iterator
  
-unsigned char maxBrightness = 128; +// LED on Pin 13 for digital on/off demo 
-unsigned char pwmFrequency = 75; +int ledPin ​13
-int numRegisters ​3+int pinState ​LOW;
-int numRGBleds ​4;+
  
-void setup(){ +void setup() ​ 
-  ​Serial.begin(115200);+{  
 +  ​// Attach each Servo object to a digital pin 
 +  servo1.attach(9, minPulse, maxPulse);​ 
 +  servo2.attach(10,​ minPulse, maxPulse);​ 
 +  servo3.attach(11,​ minPulse, maxPulse);​ 
 +  servo4.attach(12,​ minPulse, maxPulse);​ 
 +   
 +  // LED on Pin 13 for digital on/off demo 
 +  pinMode(ledPin, OUTPUT);
  
-  // Sets the number of 8-bit registers that are used. +  // Open the serial connection, 9600 baud 
-  ​ShiftPWM.SetAmountOfRegisters(numRegisters);+  ​Serial.begin(9600); 
 +
  
 +void loop() ​
 +
 +  // Wait for serial input (min 3 bytes in buffer)
 +  if (Serial.available() > 2) {
 +    // Read the first byte
 +    startbyte = Serial.read();​
 +    // If it's really the startbyte (255) ...
 +    if (startbyte == 255) {
 +      // ... then get the next two bytes
 +      for (i=0;​i<​2;​i++) {
 +        userInput[i] = Serial.read();​
 +      }
 +      // First byte = servo to move?
 +      servo = userInput[0];​
 +      // Second byte = which position?
 +      pos = userInput[1];​
 +      // Packet error checking and recovery
 +      if (pos == 255) { servo = 255; }
  
-  ShiftPWM.SetPinGrouping(1); +      // Assign new position to appropriate servo 
 +      switch ​(servo) { 
 +        case 1
 +          servo1.write(pos); ​   // move servo1 to '​pos'​ 
 +          break; 
 +        case 2: 
 +          servo2.write(pos);​ 
 +          break; 
 +        case 3: 
 +          servo3.write(pos);​ 
 +          break; 
 +        case 4: 
 +          servo4.write(pos)
 +          break;
  
-  ShiftPWM.Start(pwmFrequency,​maxBrightness);​ 
  
 +        // LED on Pin 13 for digital on/off demo
 +        case 99:
 +          if (pos == 180) {
 +            pinState == LOW;
 +          }
 +          if (pos == 0) {
 +            pinState = LOW;
 +          }
 +        digitalWrite(ledPin,​ pinState);
 +          break;
 +      }
 +    }
 +  }
 } }
  
- 
- 
-void loop() 
-{ 
- 
-  /*for(int led=0;​led<​numRGBleds;​led++){ // loop over all LED's 
-  for(int intensity=0;​intensity<​255;​intensity++){ // loop over all LED's 
-    ShiftPWM.SetRGBW(led,​ intensity, 0, 0, 0); 
-    delay(5); 
-   } 
-   ​for(int intensity=255;​intensity>​0;​intensity--){ // loop over all LED's 
-    ShiftPWM.SetRGBW(led,​ intensity, 0, 0, 0); 
-    delay(5); 
-   } 
-   }*/ 
-   while (Serial.available() > 0) { 
-     int led = Serial.parseInt();​ 
-     int red = Serial.parseInt();​ 
-     int green = Serial.parseInt();​ 
-     int blue = Serial.parseInt();​ 
-     //int white = Serial.parseInt();​ 
-     ​ShiftPWM.SetRGBW(led,​ red, green, blue, 0); 
-   } 
-} 
 </​code> ​ </​code> ​
multiple-serial-servo-control.txt · Last modified: 2015/01/22 15:36 by zoza