Results 1 to 4 of 4

Thread: New Code, New Mega Still no solution

  1. #1
    Join Date
    Jan 2018
    Location
    North Wales, United Kingdom
    Posts
    46
    Post Thanks / Like

    Default New Code, New Mega Still no solution

    Hi Guys,
    racking my brain a bit here, have even gone back to basics. after months of trying to sequence and the shows starting to take place, I did the inevitable the other week and tided up as we had guests coming, on them leaving i got back out the project, to find that my mega r3 wasnt responding correctly, i was getting lights not lighting freezing shows, missing events etc etc. so i looked over the code again saw no difference, but decided to re upload it, this didnt solve it so i checked my settings in vixen (2.1.1.0) everything matched with my code, still the same. I removed my dirkcheap817 from the show and went back to my test breadboard (just as a process of elimination) still getting the same results. So I ordered a new board, which arrived today and guess what im still suffering with the same problem. any ideas or suggestions will be greatly received code is as follows bellow A-J are the light outputs and C11-C15 are servo outputs the *16......... in the void loop section are the conversion factor for the pca9685 board that im using to drive the servos

    Code:
    #include <Wire.h>
    #include <Adafruit_PWMServoDriver.h>
    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
    
    int A = 2; //pwm pin mega
    int B = 3; //pwm pin mega
    int C = 4; //pwm pin mega
    int D = 5; //pwm pin mega
    int E = 6; //pwm pin mega
    int F = 7; //pwm pin mega
    int G = 8; //pwm pin mega
    int H = 10; //pwm pin mega
    int I = 11; //pwm pin mega
    int J = 12; //pwm pin mega
    int C11 = 00;
    int C12 = 01;
    int C13 = 02;
    int C14 = 03;
    int C15 = 04;
    
    #define M_NUMBER_OF_DATA_BYTES 15
    static const char Header[4] = "Andy";
    
    // index into the Header indicating the next expected byte
    int HeaderIndex = 0;
    
    // Allocate space to hold received data for easy access
    int incomingByte[M_NUMBER_OF_DATA_BYTES + 1];
    
    #define SERVOMIN  150 
    #define SERVOMAX  600 
    
    void setup()
    {
      Serial.begin(57600);
      Wire.beginTransmission(0x40);
      Wire.begin();
      
      pwm.begin();
      pwm.setPWMFreq(500);
      
      uint8_t twbrbackup = TWBR;
      
      TWBR = 12;
      pinMode(A, OUTPUT);
      pinMode(B, OUTPUT);
      pinMode(C, OUTPUT);
      pinMode(D, OUTPUT);
      pinMode(E, OUTPUT);
      pinMode(F, OUTPUT);
      pinMode(G, OUTPUT);
      pinMode(H, OUTPUT);
      pinMode(I, OUTPUT);
      pinMode(J, OUTPUT);
      pwm.setPWM(C11, 0, 0);
      pwm.setPWM(C12, 0, 0);
      pwm.setPWM(C13, 0, 0);
      pwm.setPWM(C14, 0, 0);
      pwm.setPWM(C15, 0, 0);
    } // setup
    
    
    void loop()
    {
      // check to see if we have the minimum number of expected data in the input buffer
      if (Serial.available() >= M_NUMBER_OF_DATA_BYTES ) 
      {
        // has the entire header been processed?
        if( sizeof(Header) <= HeaderIndex )
        {
          // header is complete. That means the rest of the bytes are channel data
    
          // read in the data into a holding buffer
          for (int i=0; i <= M_NUMBER_OF_DATA_BYTES ; i++)
          {
            // read in a single channel and stroe it in a local buffer
            incomingByte[i] = Serial.read();
          }
        
          // output the data to the PWM generator
          analogWrite(A, incomingByte[0]);  // Pin 2
          analogWrite(B, incomingByte[1]);  // Pin 3
          analogWrite(C, incomingByte[2]);  // Pin 4
          analogWrite(D, incomingByte[3]);  // Pin 5
          analogWrite(E, incomingByte[4]);  // Pin 6
          analogWrite(F, incomingByte[5]);  // Pin 7
          analogWrite(G, incomingByte[6]);  // Pin 8
          analogWrite(H, incomingByte[7]);  // Pin 9
          analogWrite(I, incomingByte[8]);  // Pin 10
          analogWrite(J, incomingByte[9]);  // Pin 11
          pwm.setPWM(C11, 0, incomingByte[10]*16.05882352941176);
          pwm.setPWM(C12, 0, incomingByte[11]*16.05882352941176);
          pwm.setPWM(C13, 0, incomingByte[12]*16.05882352941176);
          pwm.setPWM(C14, 0, incomingByte[13]*16.05882352941176);
          pwm.setPWM(C15, 0, incomingByte[14]*16.05882352941176);
          
          // start looking for the next header
          HeaderIndex = 0;
    
        } // end process data after header was reveived
    
        // else lets see if the data has the correct header
        else if( Header[HeaderIndex] == Serial.read())
        {
            // This IS the expected header byte. Move to the next header byte
            HeaderIndex++;
    
        } // end header match
        else // opps, there is a problem
        {
            // This is NOT the expected header byte. Start waiting for the begining of the header
            HeaderIndex = 0;
    
        } // end header miss-match
      } // End there was enough data in the buffer to process
    } // loop
    Last edited by Andsolo21; 09-19-2018 at 11:52 AM.

  2. #2
    Join Date
    Oct 2014
    Location
    Sauk City, WI USA
    Posts
    943
    Post Thanks / Like

    Default Re: New Code, New Mega Still no solution

    Code looks ok. Small thing. Your if (Serial.available() >= M_NUMBER_OF_DATA_BYTES ) should account for the header and the data. But the code should work as written. I'm wondering though if it might be possible you are not syncing and causing a receive buffer problem. That would account for some of the issues.

    It would be better if this was written in a state machine fashion. Look for code from Martin Mueller...this code is untested but is a better way to process the serial input. You will need to play with the writes a bit to make sure you are doing analogWrite and pwm.setPWM on the right bytes. http://doityourselfchristmas.com/for...089#post495089

    Speed set to 57600 in Vixen and on the comm port? Did you have the Arduino IDE running? How are you powering the mega? How are you powering the lights and servos? Video of problem?

  3. #3
    Join Date
    Jan 2018
    Location
    North Wales, United Kingdom
    Posts
    46
    Post Thanks / Like

    Default Re: New Code, New Mega Still no solution

    Hi Mike
    Thanks for the reply I’ve checked everything and all the settings for the com port are identical throughout the machine,software and code,
    Martin actually went over this code for me originally and it was running perfect, I can’t believe that disturbance factor has actually caused me these issues

  4. #4
    Join Date
    Dec 2012
    Location
    Hudson MA
    Posts
    2,954
    Post Thanks / Like

    Default Re: New Code, New Mega Still no solution

    The code Mike is talking about is the next generation. It solves the buffering issues and spreads processing out over time to prevent frame slip and jitter issues.


    2018 - Moving and going to visit my Daughter in New Zealand. Most likely I will be dark or nearly dark, Some static stuff that is simple to put up.

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
  •