arduino - Issues with UART not working at some point -
i connecting arduino spark core has same code framework arduino. communication between them works fine of time noticed @ point of time arduino not able send messages spark core. arduino recieves messages spark core fine though. don't understand cause issue uart though code simple.
in case, have push button connected arduino, whenever user presses it, sends 'high' spark core turns on led, , when let go sends 'low' turn off led. can me understand why uart freezes or something? how detect issue , fix it?
arduino code:
#define buttonpin 7 int buttonstate = 0; // variable reading pushbutton status int sendonce = 0; void setup() { serial.begin(9600); // serial.print("begin\n"); } void loop() { isbuttonpressed(); } void isbuttonpressed () { // read state of pushbutton value: buttonstate = digitalread(buttonpin); // check if pushbutton pressed. // if is, buttonstate high: if (buttonstate == high) { if (sendonce == 0) { serial.print("high\n"); // ser.write('1'); sendonce = 1; } } else if (buttonstate == low && sendonce == 1) { sendonce = 0; serial.print("low\n"); } }
spark core code:
#include "serial2/serial2.h" // name pins int led1 = d7; int led2 = d1; string txtmsg = ""; char s; char lightstatus[20] ; // routine runs once upon reset void setup() { // configure pins outputs pinmode(led1, output); // initialize both leds off digitalwrite(led1, low); //serial1.begin(9600); serial2.begin(9600); //serial2.println("hello spark"); } // routine loops forever void loop() { while (serial2.available() > 0) { s=(char)serial2.read(); if (s == '\n') { if(txtmsg=="high") { digitalwrite(led1, 1); strcpy(lightstatus, "light on"); } if(txtmsg=="low") { digitalwrite(led1, 0); strcpy(lightstatus, "light off"); } // serial.println(txtmsg); txtmsg = ""; } else { txtmsg +=s; } } }
Comments
Post a Comment