Lua serial [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
whiteland92
Prole
Posts: 12
Joined: Fri Jul 04, 2014 1:30 am

Lua serial [SOLVED]

Post by whiteland92 »

i got this code to turn some leds on a arduino.

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()
The problem i have is that when the arduino first turns on it doesn't receive it, at least the data in led doesn't blink on the arduino..
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);
    }
}
Last edited by whiteland92 on Fri Nov 04, 2016 10:22 pm, edited 2 times in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Lua serial

Post by bartbes »

Presumably you need to configure the com port, on windows' side. The C# program probably sets the baud rate, byte width, etc, and those settings remain as long as the com port exists, which is probably until you unplug the arduino.
User avatar
whiteland92
Prole
Posts: 12
Joined: Fri Jul 04, 2014 1:30 am

Re: Lua serial

Post by whiteland92 »

Fixed.
added this line of code

Code: Select all

os.execute("powershell $port= new-Object System.IO.Ports.SerialPort COM4,9600,None,8,one;$port.open();$port.close();")
working serial, windows only

Code: Select all

local port = "com4";
local baud = 9600

os.execute("powershell $port= new-Object System.IO.Ports.SerialPort "..port..","..baud..",None,8,one;$port.open();$port.close();")

local serial = io.open(port,"w")
serial:write("0,5,255,2,1000,true")
serial:flush()
serial:close())
i know i know. it's bodgy
Last edited by whiteland92 on Sat Nov 05, 2016 12:23 am, edited 1 time in total.
MachineCode
Citizen
Posts: 70
Joined: Fri Jun 20, 2014 1:33 pm

Re: Lua serial [SOLVED]

Post by MachineCode »

I haven't tried on windows, but lua-periphery.Serial seems to work very well. It is on luarocks and easy to install. In fact, it installs on Raspberry Pi without any dramas and I was able to talk to a serial device connected to a RPi via VNC using my linux machine. It's just a wrapper, but it makes life easier.

sudo luarocks install lua-periphery

local periphery = require('periphery')
local Serial = periphery.Serial

https://github.com/vsergeev/lua-periphe ... /serial.md
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], slime and 171 guests