New Code, New Mega ...
 
Notifications
Clear all

New Code, New Mega Still no solution

3 Posts
2 Users
0 Reactions
2,105 Views
Posts: 55
Topic starter
(@andsolo21)
Trusted Member
Joined: 9 years ago
[#49210]

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


#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

2 Replies
Posts: 55
Topic starter
(@andsolo21)
Trusted Member
Joined: 9 years ago

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


Reply
MartinMueller2003
Posts: 7615
(@martinmueller2003)
Illustrious Member
Joined: 14 years ago

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.


Reply
Share: