/*
*/
int MaxVal = 65000;
int CH1PWM = 0;
int CH2PWM = 0;
int CH3PWM = 0;
int CH4PWM = 0;
int DataStamp = 20000;
bool GotData ;
String incomingStr ;
String ReadStr ;
// we are using 4 pwm ch pins 5,6,9,10
void setup() {
analogWriteResolution(16);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
analogWriteFrequency(5, 108);
analogWriteResolution(16);
analogWriteFrequency(6, 108);
analogWriteResolution(16);
analogWriteFrequency(9, 108);
analogWriteResolution(16);
analogWriteFrequency(10, 108);
analogWriteResolution(16);
Serial.setTimeout(1);
Serial.begin(115200);
analogWriteResolution(16);
}
void loop() {
// set the pwm outs
ReadDataStream();
analogWrite(5, CH1PWM);
analogWrite(6, CH2PWM);
analogWrite(9, CH3PWM);
analogWrite(10, CH4PWM);
//analogWrite(13, (CH1PWM + CH2PWM) / 2);
digitalWrite(13, GotData);
if (millis() > DataStamp + 5000){
CH1PWM = 0 ;
CH2PWM = 0 ;
CH3PWM = 0 ;
CH4PWM = 0 ;
analogWrite(5, 0);
analogWrite(6, 0);
analogWrite(9, 0);
analogWrite(10, 0);
GotData = false ;
digitalWrite(13, GotData);
}
}
void ReadDataStream()
{
if (Serial.available() > 0) {
GotData = not(GotData);
DataStamp = millis() ;
incomingStr = Serial.readString();
Serial.println(incomingStr);
if (incomingStr.startsWith("CH1")) {
ReadStr = incomingStr.substring(3, 9) ;
CH1PWM = ReadStr.toInt();
if (CH1PWM > MaxVal){
CH1PWM = MaxVal ;
}
}
if (incomingStr.startsWith("CH2")) {
ReadStr = incomingStr.substring(3, 9) ;
//Serial.println(ReadStr);
CH2PWM = ReadStr.toInt();
//Serial.println(CH2PWM);
if (CH2PWM > MaxVal){
CH2PWM = MaxVal ;
}
}
if (incomingStr.startsWith("CH3")) {
ReadStr = incomingStr.substring(3, 9) ;
//Serial.println(ReadStr);
CH3PWM = ReadStr.toInt();
//Serial.println(CH3PWM);
if (CH3PWM > MaxVal){
CH3PWM = MaxVal ;
}
}
if (incomingStr.startsWith("CH4")) {
ReadStr = incomingStr.substring(3, 9) ;
//Serial.println(ReadStr);
CH4PWM = ReadStr.toInt();
//Serial.println(CH4PWM);
if (CH4PWM > MaxVal){
CH4PWM = MaxVal ;
}
}
}
}
Any chance you could try a program which automatically sweeps the PWM gradually, without requiring communication from your computer?
While you did give us a complete program, it appears to depend on receiving certain data to actually do anything. We can't just run that on a Teensy to reproduce the problem. A program which just gradually changes the PWM without USB communication would be ideal for trying to troubleshoot only the PWM stuff.
If this does turn out to be a problem with the communication stuff, we might be able to help with that too... but without having the data or program which sends the data, pretty much impossible to try reproducing the problem.
int PWMVal = 0;
int I = 0;
int Step = 1;
void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
analogWriteResolution(16);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(5, I);
analogWrite(6, I);
analogWrite(9, I);
analogWrite(10, I);
I = I + Step;
if (I <= 0 || I >= 65535) {
Step = -Step;
}
delay(1);
}
what do i need to do to improve serial comms and make sure data is not missed / ignored ?
also should I be using a proper hardware serial port rather than the usb over serial ?
void ReadDataStream()
{
if (Serial.available() > 0) {
GotData = not(GotData);
DataStamp = millis() ;
incomingStr = Serial.readString();
Serial.println(incomingStr);
if (incomingStr.startsWith("CH1")) {
ReadStr = incomingStr.substring(3, 9) ;
CH1PWM = ReadStr.toInt();
if (CH1PWM > MaxVal){
CH1PWM = MaxVal ;
}
}
if (incomingStr.startsWith("CH2")) {
ReadStr = incomingStr.substring(3, 9) ;
//Serial.println(ReadStr);
CH2PWM = ReadStr.toInt();
//Serial.println(CH2PWM);
if (CH2PWM > MaxVal){
CH2PWM = MaxVal ;
}
}
if (incomingStr.startsWith("CH3")) {
ReadStr = incomingStr.substring(3, 9) ;
//Serial.println(ReadStr);
CH3PWM = ReadStr.toInt();
//Serial.println(CH3PWM);
if (CH3PWM > MaxVal){
CH3PWM = MaxVal ;
}
}
if (incomingStr.startsWith("CH4")) {
ReadStr = incomingStr.substring(3, 9) ;
//Serial.println(ReadStr);
CH4PWM = ReadStr.toInt();
//Serial.println(CH4PWM);
if (CH4PWM > MaxVal){
CH4PWM = MaxVal ;
}
}
}