Assymetrical Blink controlled via Serial Comm
hello all,
i have swallowed newbie pride , realized need beyond smashing code , praying arduino gods results work.
what trying use serial communication tell arduino rate @ want led blink on , off indefinitely. catch want able control timing both led states independently (e.g. led pin set high 500 millis low 200 millis).
an example sketch achieves close result looking arduinopc2 sketch robin2 found here.
the differences between arduinopc2 sketch , trying written control 2 leds , servo motor , flashled function appears simplified beyond able break down.
the second example code achieves trying without serial communication found here written ukhelibob.
below attempt merge codes bidding (the squeamish may wish avert eyes part
):
obviously, above code not achieve result i'm looking for, must not using correct syntax (or language in cases ;p). ideally, i'd love arduino respond serial communication formatted <200,500> , able respond setting times high , low led states such (i.e. low = 200 millis, high = 500 millis; in whatever order simplest code). makes sense?
are of fine, learned folk willing newbie understand how make code above function want?
for reference, actual circuit looks like:

i have swallowed newbie pride , realized need beyond smashing code , praying arduino gods results work.
what trying use serial communication tell arduino rate @ want led blink on , off indefinitely. catch want able control timing both led states independently (e.g. led pin set high 500 millis low 200 millis).
an example sketch achieves close result looking arduinopc2 sketch robin2 found here.
the differences between arduinopc2 sketch , trying written control 2 leds , servo motor , flashled function appears simplified beyond able break down.
the second example code achieves trying without serial communication found here written ukhelibob.
below attempt merge codes bidding (the squeamish may wish avert eyes part
):code: [select]
const byte ledpin = 2;
unsigned long previousmillis = 0;
const byte states[] = {high, low};
unsigned long intervals[] = {200, 500};
unsigned long previntervals[] = {0, 0};
byte state;
const byte buffsize = 40;
char inputbuffer[buffsize];
const char startmarker = '<';
const char endmarker = '>';
byte bytesrecvd = 0;
boolean readinprogress = false;
boolean newdatafrompc = false;
char messagefrompc[buffsize] = {0};
int newintervals = 0;
unsigned long curmillis;
unsigned long prevreplytopcmillis = 0;
unsigned long replytopcinterval = 1000;
//=============
void setup()
{
serial.begin(9600);
pinmode(ledpin, output);
serial.println("<arduino ready>");
}
//=============
void loop() {
curmillis = millis();
getdatafrompc();
updateflashinterval();
replytopc();
flashled();
}
//=============
void getdatafrompc() { // receive data pc , save inputbuffer
if(serial.available() > 0) {
char x = serial.read(); // order of these if clauses significant
if (x == endmarker) {
readinprogress = false;
newdatafrompc = true;
inputbuffer[bytesrecvd] = 0;
parsedata();
}
if(readinprogress) {
inputbuffer[bytesrecvd] = x;
bytesrecvd ++;
if (bytesrecvd == buffsize) {
bytesrecvd = buffsize - 1;
}
}
if (x == startmarker) {
bytesrecvd = 0;
readinprogress = true;
}
}
}
//=============
void parsedata() { // split data parts
char * strtokindx; // used strtok() index
strtokindx = strtok(inputbuffer,","); // first part - string
strcpy(messagefrompc, strtokindx); // copy messagefrompc
strtokindx = strtok(null, ","); // continues previous call left off
newintervals = atoi(strtokindx); // convert part integer
}
//=============
void replytopc() {
if (newdatafrompc) {
newdatafrompc = false;
serial.print("<msg ");
serial.print(messagefrompc);
serial.print(" time ");
serial.print(newintervals);
serial.println(">");
}
}
//=============
void updateflashinterval() { // illustrates using different inputs call different functions
if (strcmp(messagefrompc, "l") == 0) {
updateintervals();
}
}
//=============
void updateintervals() {
if (newintervals > 100) {
intervals[0] = newintervals;
}
}
//=============
void flashled()
{
unsigned long currentmillis = millis();
if (currentmillis - previousmillis >= intervals[state % 2])
{
previousmillis = currentmillis;
digitalwrite(ledpin, states[state++ % 2]);
}
}obviously, above code not achieve result i'm looking for, must not using correct syntax (or language in cases ;p). ideally, i'd love arduino respond serial communication formatted <200,500> , able respond setting times high , low led states such (i.e. low = 200 millis, high = 500 millis; in whatever order simplest code). makes sense?
are of fine, learned folk willing newbie understand how make code above function want?
for reference, actual circuit looks like:

obviously, above code not achieve result i'm looking forand easier debug if fill in on different looking for.
Arduino Forum > Using Arduino > Programming Questions > Assymetrical Blink controlled via Serial Comm
arduino
Comments
Post a Comment