bluetooth - SoftwareSerial issues. Only when on power jack -
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 slower fades int hold = 0; // optional hold when color complete, before 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 led outputs void setup() { pinmode(redpin, output); // sets pins 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 order of crossfades void loop() { //read bluetooth , write 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 usb serial bluetooth if(serial.available()) { char tosend = (char)serial.read(); bluetooth.print(tosend); } }
if run code, works great. until plug power source , nothing else.
if plug power source, program doesn't start (no bluetooth response). if plug usb , power or usb only, program works. if unplug usb after plugging usb , power source program still works! have tried debugging as can, don't know error is. power supply rated @ 12v 2 amps light led strips.
update: found out if press reset button after power on starts work. there way automatically reset arduino on startup???
i think using arduino leonardo.
try @ beginning of setup:
while(!serial){} while(!bluetooth){}
the arduino leonardo prepare serial port after while , may cause problems.
Comments
Post a Comment