Results 1 to 5 of 5

Thread: Arduino Uno usb servos controller

  1. #1
    Join Date
    Oct 2020
    Location
    Manilva (Málaga), Spain
    Posts
    23
    Post Thanks / Like

    Default Arduino Uno usb servos controller

    Hi, everybody. I want to do something like this: https://www.instructables.com/Intern...ic-Jack-OLant/
    I try to do a secuence with xligths, and use 4 servos. When i put the code into the arduino uno the servos all works at the same time, making crazy movements. The code y modified for this is this: https://pastebin.com/vM7i6Vyd What I´m doing wrong?
    Thanks

  2. #2
    Join Date
    May 2007
    Posts
    5,739
    Post Thanks / Like

    Default Re: Arduino Uno usb servos controller

    Two things that I've noticed on a quick scan:

    1) The const int NUM_SERVOS variable is set to '1'. This is used to set the size of the 'servos' array variable. I think that code later on in the sketch might be reading data from beyond the end of the servos array. Setting NUM_SERVOS to 4 might improve things.

    2) The end of the sketch outputs random values if serial data has not been received within the last second. I wonder if there is something wrong in the communications with XLights that is preventing the reception of appropriately formatted serial data and thus activating the random movement mode.
    Phil

  3. #3
    Join Date
    Oct 2020
    Location
    Manilva (Málaga), Spain
    Posts
    23
    Post Thanks / Like

    Default Re: Arduino Uno usb servos controller

    Hi Phil, I put now this code:
    Code:
    /*
      This code runs vixen through the arduino, usining pins 3, 5, and 6 with
      3 seperate servos; pins 9, 10, and 11 are used for 3 LED circuits with PWM;
      pins 12, 13 are extra digital pins.
    
      Adapted from code by GSRVAhiker17, zparticle, victor_pv, Si_champion, and Neil Tapp.
    */
    
    #include <Servo.h>    // Servos
    #include <avr/wdt.h>  // Raw AVR watchdog timer
    
    // Testing mode (treat servos as simple PWM)
    const bool TESTINGMODE = false;
    
    // Vixen header (identifies a light sequence)
    const int HEADER_LEN = 2;
    const char seqHeader[] = {'~','!'};
    
    // Timeout waiting for serial input before going to random mode (in milliseconds).
    const int TIME_OUT = 1000;
    
    // Channels mapped to pin numbers
    const int BOLA1 = 9;
    const int BOLA2 = 10;
    const int BOLA3 = 11; 
    const int BOLA4 = 12; 
    const int LUCES1 = 3; 
    const int LUCES2 = 5;
    const int LUCES3 = 6;  
    const int LUCES4 = 7;   
    
    // List of active channels
    const int channels[] = {BOLA1, BOLA2, BOLA3, BOLA4, LUCES1,LUCES2,LUCES3,LUCES4
                            
                           };
    
    // Number of active channels
    const int NUM_ACTIVE_CHANNELS = 8;
    
    // PWM map
    const int isPWM[] = {true, true, true, true, true, true, true, true
                        };
    
    // Servos
    const int SERVO_DELAY = 15; // delay after servo is activated (allow it to move)
    const int NUM_SERVOS = 4;
    const int NEUTRAL = 120;  // Neutral position
    Servo servos[NUM_SERVOS];
    
    // Min servo opening in degrees from neutral position
    const int servoMin[] = {0, 0, 0, 0 };
    
    // Max servo opening in degrees from neutral position
    const int servoMax[] = {180, 180, 180, 180 };
    //const int servoMax[] = {20, 15, 15};
    
    // Servo channel map
    const int NO_SERVO = -1;
    const int servoNumber[] = {1, 2, 3, 0, NO_SERVO, NO_SERVO, NO_SERVO, NO_SERVO
                            
                           };
                           
    // Servo direction
    const int CLOCKWISE = 1;
    const int COUNTERCLOCKWISE = -1;
    const int servoDirection[] = {COUNTERCLOCKWISE, COUNTERCLOCKWISE, COUNTERCLOCKWISE };
    
    // Serial
    const long COM_SPEED = 115200;
    int incomingByte[NUM_ACTIVE_CHANNELS];  // array to store the channel values from the serial port
    
    // Misc
    int i = 0;                              // Loop counter
    int j = 0;                              // Loop counter
    volatile unsigned long  timer_a = 0;    // Timer
    
    
    //setup the pins/ inputs & outputs
    void setup()
    {
      // enable the watchdog timer with a time of 1 second. If the board freezes, it will reset itself after 1 second.
      wdt_enable(WDTO_1S);
    
      // specifically for the UNO
      sei();
    
      // initalize PWM Channels / Pins
      for (i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
        pinMode(channels[i], OUTPUT);
        if ((servoNumber[i] != NO_SERVO) && !TESTINGMODE) {
          servos[servoNumber[i]].attach(channels[i]);
        }
      }
    
      // set all the channels off to begin
      for (i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
        digitalWrite(channels[i], LOW);
        if ((servoNumber[i] != NO_SERVO) && !TESTINGMODE) {
          servos[servoNumber[i]].write(NEUTRAL);
        }
      }
    
      test_sequence(); // brief test
      Serial.begin(COM_SPEED);   // set up Serial
    }
    
    void loop()
    {
      if (Serial.available() >= NUM_ACTIVE_CHANNELS + HEADER_LEN) {
        wdt_reset(); // resets the watchdog (prevents board lockup)
        timer_a = millis ();  // Mark the time when a message was received
    
        // read the header to verify this is in fact a light sequence
        // probably overkill, but borrowing from the above sources...
        for (int i = 0; i < HEADER_LEN; i++) {
          if (seqHeader[i] != Serial.read()) { return; }
          //Serial.read();
        }
        
        // read the oldest byte in the serial buffer:
        for (int i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
          // read each byte
          incomingByte[i] = Serial.read();
    
          if ((servoNumber[i] != NO_SERVO) && !TESTINGMODE) {
            // SERVOS ------------------------------
            int angle = map(incomingByte[i], 0, 255, servoMin[servoNumber[i]], servoMax[servoNumber[i]]);
            angle *= servoDirection[servoNumber[i]];
            servos[servoNumber[i]].write(NEUTRAL + angle);
            delay(SERVO_DELAY);
          } else if (isPWM[i]) {
            // PWM ---------------------------------
            analogWrite(channels[i], incomingByte[i]);
          } else {
            // DIGITAL (A2D) -----------------------
            if (incomingByte[i] <= 127) {
              digitalWrite(channels[i], LOW);
            } else {
              digitalWrite(channels[i], HIGH);
            }
          }
        }
    
      } else {
        // Random mode starts if no serial input has been received in TIME_OUT milliseconds
        wdt_reset(); // resets the watchdog (prevents board lockup)
        unsigned long diff = millis() - timer_a;
        if (diff >= TIME_OUT) {
          timer_a = millis ();
          int random_a = 0;
          for (i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
            if ((servoNumber[i] != NO_SERVO) && !TESTINGMODE) continue;
            random_a = random(0, 2);
            if (random_a == 0) {
              digitalWrite(channels[i], LOW);
            } else {
              digitalWrite(channels[i], HIGH);
            }
          }
        }
      }
    }
    
    // Test the setup briefly
    void test_sequence() {
      for (i = 0; i < NUM_ACTIVE_CHANNELS; i++) {
        wdt_reset(); // resets the watchdog
        digitalWrite(channels[i], HIGH);
        delay (500);
        digitalWrite(channels[i], LOW);
      }
    }
    But also it doesn´t work. It moves one servo only on pin number 9. The other servos don´t continue the sequence. Also my problem was I do the sequence in xlights, and all my sequences was in vixen. Now I run with the vixen sequences but only take BOLA1 properly. BOLA2, BOLA3,BOLA4 don´t work. I don´t know were is the problem. Some help?? Thanks

  4. #4
    Join Date
    Oct 2014
    Location
    Cleethorpes, England
    Posts
    99
    Post Thanks / Like

    Default Re: Arduino Uno usb servos controller

    Hi Tripishadow
    this is the link that got me started a few years back and the code worked a treat and i still use it today
    https://www.youtube.com/watch?v=z0hRF4pyCD0

    This was my 1st attempt with vixen using the above code
    https://www.youtube.com/watch?v=QAtjPhybS8E

  5. #5
    Join Date
    Oct 2020
    Location
    Manilva (Málaga), Spain
    Posts
    23
    Post Thanks / Like

    Default Re: Arduino Uno usb servos controller

    Hi. Everybody. I have solve the problem my self. The problem was the communication between vixen and the arduino. When I patch the element to the controller, it was color degrade or something like that, not was a direct line like the first one. When I put a direct line, parched both, it runs perfectly. Only the 4th servo don't work, but I will change the code to the one that shows @qble. I will tell you how it goes. Also I tried a led, and worked perfectly with the three servos.

    Thanks for all.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •