SoftwareSerial issues. Only when on power jack

266 Views Asked by At

My Code:

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(2,3);
// Output
int redPin = 6;   // Red LED, 
int grnPin = 11;  // Green LED,
int bluPin = 5;  // Blue LED, 

// Color arrays
int black[3]  = { 0, 0, 0 };
int white[3]  = { 100, 100, 100 };
int red[3]    = { 100, 0, 0 };
int green[3]  = { 0, 100, 0 };
int blue[3]   = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1]; 
int bluVal = black[2];

int wait = 10;      // 10ms internal crossFade delay; increase for slower fades
int hold = 0;       // Optional hold when a color is complete, before the next crossFade
int r = 0;
int g = 0;
int b = 0;
char mode = '\0';
// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;

// Set up the LED outputs
void setup()
{
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT); 

 Serial.begin(9600);
 delay(1000);
 bluetooth.begin(115200);
 delay(100);
 bluetooth.print("$$$");
 delay(100);
 bluetooth.println("U,9600,N");
 bluetooth.begin(9600);
 delay(100);
 analogWrite(redPin, 0); 
 analogWrite(grnPin, 0);      
 analogWrite(bluPin, 0); 
 Serial.println("Bluetooth initiated.");
}

void printHEX() {
  Serial.print(r, HEX);
  Serial.print(g, HEX);
  Serial.println(b, HEX);
  bluetooth.print(r, HEX);
  bluetooth.print(g, HEX);
  bluetooth.println(b, HEX);
}

// Main program: list the order of crossfades
void loop()
{
  //Read from bluetooth and write to usb serial
  while(bluetooth.available())
  {
    if(mode == '\0') {
      mode = (char)bluetooth.read();
    }
    if(mode == 'c'){
      int r1 = bluetooth.parseInt();
      int g1 = bluetooth.parseInt();
      int b1 = bluetooth.parseInt();
      if (bluetooth.read() == '\n') {
        if(r1 != r || g1 != g || b1 != b) {
          r = r1;
          g = g1;
          b = b1;
          analogWrite(redPin, r);
          analogWrite(grnPin, g);      
          analogWrite(bluPin, b); 
          printHEX();
          mode = '\0';
        } else {
          printHEX();
          mode = '\0';
        }
      }
  } else if(mode == 'p') {
    if (bluetooth.read() == '\n') {
      printHEX();
      mode = '\0';
    }
  }
}

  //Read from usb serial to bluetooth
  if(Serial.available())
  {
    char toSend = (char)Serial.read();
    bluetooth.print(toSend);
  }
}

If I run this code, everything works great. That is until I plug it into the power source and nothing else.

If I plug it into the power source, the program doesn't start (no bluetooth response). If I plug it into usb and power or usb only, the program works. If I unplug usb after plugging usb and power source the program still works! I have tried debugging as much as I can, but I don't know where the error is. The power supply is rated at 12V 2 Amps to light up the LED strips.

Update: I found out that if I press the reset button after power on everything starts to work. Is there a way to automatically reset arduino on startup???

1

There are 1 best solutions below

1
On

I think you are using arduino leonardo.

Try this at very beginning of setup:

while(!Serial){}
while(!bluetooth){}

The arduino leonardo prepare the serial port after some while and may cause some problems.