Code: Select all
local file = "com3";
local serial = assert(io.open(file,"w"))
serial:write("0,25,255,60,1000,false")
serial:flush()
serial.close()
what i do is use a c# program to send a string to the arduino, after i've done that the lua code work perfectly fine.
Anyone here that can give me a reason why? or a potential hot fix?
It's almost like the arduino doesnt know it should receive the data.
arduino code. "still learning, gimme some slack:P"
Code: Select all
int ledR = 9;
int ledG = 10;
int ledB = 11;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
}
void someFuction(int led_red ,int led_green ,int led_blue, int hz, int duration, String fade)
{
int loops = 0;
Serial.println(fade);
if (fade== "false") {
while((1000/hz)*loops < duration)
{
analogWrite(ledR, led_red);
analogWrite(ledG, led_green);
analogWrite(ledB, led_blue);
delay( 500 / hz );
analogWrite(ledR, 0);
analogWrite(ledG, 0);
analogWrite(ledB, 0);
delay( 500 / hz );
++loops;
}
}
if (fade== "true") {
while(hz > loops)
{
for (int i = 0; i < 255; i++) {
bool sleep = false;
if (i < led_red) {analogWrite(ledR, i); sleep = true;}
if (i < led_green) {analogWrite(ledG, i); sleep = true;}
if (i < led_blue) {analogWrite(ledB, i); sleep = true;}
if (sleep) {delay(4);}
}
delay( duration - 1000 );
for (int i = 255; i > 0; i--) {
bool sleep = false;
if (i < led_red) {analogWrite(ledR, i); sleep = true;}
if (i < led_green) {analogWrite(ledG, i); sleep = true;}
if (i < led_blue) {analogWrite(ledB, i); sleep = true;}
if (sleep) {delay(4);}
}
++loops;
}
analogWrite(ledR, 0);
analogWrite(ledG, 0);
analogWrite(ledB, 0);
}
}
void loop()
{
String comMSG = Serial.readString();
// 1 = red, 2 = greem, 3 = blue, 4 = hz, 5 = duration,
int CommaIndexOne = comMSG.indexOf(',');
int commaIndexTwo = comMSG.indexOf(',', CommaIndexOne +1);
int commaIndexThree = comMSG.indexOf(',', commaIndexTwo +1);
int commaIndexFour = comMSG.indexOf(',', commaIndexThree +1);
int CommaIndexFive = comMSG.indexOf(',', commaIndexFour +1);
int CommaIndexSix = comMSG.indexOf(',', CommaIndexFive +1);
String led_red = comMSG.substring (0, CommaIndexOne);
String led_green = comMSG.substring (CommaIndexOne +1, commaIndexTwo);
String led_blue = comMSG.substring (commaIndexTwo +1, commaIndexThree);
String hz = comMSG.substring(commaIndexThree +1, commaIndexFour);
String duration = comMSG.substring(commaIndexFour +1, CommaIndexFive);
String fade= comMSG.substring(CommaIndexFive +1);
if (hz.toInt() > 0 and duration.toInt() > 0) {
someFuction(led_red.toInt() ,led_green.toInt() ,led_blue.toInt() ,hz.toInt() ,duration.toInt(), fade);
}
}