Advertisement
talofer99

Aduino DMX pan tilt with RGB LED

Nov 2nd, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. // **********************************************************
  2. // Code by: Tal OFer (talofer99@hotmail.com)
  3. // pan and tilt SERVO with RGB light on it.
  4. // using DMX proejct from http://www.mathertel.de/Arduino/DMXSerial.aspx
  5.  
  6.  
  7.  
  8. #include <DMXSerial.h>
  9. #include <Servo.h>
  10.  
  11. // Constants for demo program
  12. const int RedPin =    11;  // PWM output pin for Red Light.
  13. const int GreenPin =  6;  // PWM output pin for Green Light.
  14. const int BluePin =   5;  // PWM output pin for Blue Light.
  15. const int PanPin =   3;  // Pan servo pin.
  16. const int TiltPin =   4;  // Tilt servo pin.
  17.  
  18. Servo pan_seervo;
  19. Servo tilt_seervo;
  20.  
  21. // default values
  22. #define RedDefaultLevel   0
  23. #define GreenDefaultLevel 0
  24. #define BlueDefaultLevel  0
  25. #define PanDefaultLevel  90
  26. #define TiltDefaultLevel  90
  27.  
  28. // CHANNLES IDS
  29. //#define FIRSTMACHINE
  30. #ifdef FIRSTMACHINE
  31. #define RED_CH 1
  32. #define GREEN_CH 2
  33. #define BLUE_CH 3
  34. #define PAN_CH 4
  35. #define TILT_CH 5
  36. #else
  37. #define RED_CH 6
  38. #define GREEN_CH 7
  39. #define BLUE_CH 8
  40. #define PAN_CH 9
  41. #define TILT_CH 10
  42.  
  43. #endif
  44.  
  45. void setup () {
  46.   Serial.begin(9600);
  47.   Serial.println("System started");
  48.   DMXSerial.init(DMXReceiver);
  49.  
  50.   // set some default values
  51.   DMXSerial.write(RED_CH, RedDefaultLevel);
  52.   DMXSerial.write(GREEN_CH, GreenDefaultLevel);
  53.   DMXSerial.write(BLUE_CH, BlueDefaultLevel);
  54.   DMXSerial.write(PAN_CH, 128);
  55.   DMXSerial.write(TILT_CH, 128);
  56.  
  57.   // enable pwm outputs
  58.   pinMode(RedPin,   OUTPUT); // sets the digital pin as output
  59.   pinMode(GreenPin, OUTPUT);
  60.   pinMode(BluePin,  OUTPUT);
  61.  
  62.   digitalWrite(RedPin, HIGH);
  63.   delay(500);
  64.   digitalWrite(RedPin, LOW);
  65.   digitalWrite(GreenPin, HIGH);
  66.   delay(500);
  67.   digitalWrite(GreenPin, LOW);
  68.   digitalWrite(BluePin, HIGH);
  69.   delay(500);
  70.   digitalWrite(GreenPin, LOW);
  71.  
  72.   // attach servos
  73.   pan_seervo.attach(PanPin);
  74.   tilt_seervo.attach(TiltPin);
  75.   pan_seervo.write(PanDefaultLevel);
  76.   tilt_seervo.write(TiltDefaultLevel);
  77.  
  78. }
  79.  
  80.  
  81. void loop() {
  82.   // Calculate how long no data backet was received
  83.   unsigned long lastPacket = DMXSerial.noDataSince();
  84.  
  85.   if (lastPacket < 5000) {
  86.     // read recent DMX values and set pwm levels
  87.     analogWrite(RedPin,   DMXSerial.read(RED_CH));
  88.     analogWrite(GreenPin, DMXSerial.read(GREEN_CH));
  89.     analogWrite(BluePin,  DMXSerial.read(BLUE_CH));
  90.     pan_seervo.write(map(DMXSerial.read(PAN_CH), 0, 255, 0, 180));
  91.     tilt_seervo.write(map(DMXSerial.read(TILT_CH), 0, 255, 0, 180));
  92.   } else {
  93.     // Show pure red color, when no data was received since 5 seconds or more.
  94.     analogWrite(RedPin,   RedDefaultLevel);
  95.     analogWrite(GreenPin, GreenDefaultLevel);
  96.     analogWrite(BluePin,  BlueDefaultLevel);
  97.     pan_seervo.write(PanDefaultLevel);
  98.     tilt_seervo.write(TiltDefaultLevel);
  99.   } // if
  100. }
  101.  
  102. // The End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement