Pyserial + Arduino
hey all, need major pyserial , arduino...
i able directly control multi-color led in arduino through python in terminal executing:
import serial
ser = serial.serial ('/dev/tty.usbserial-a6004p1r', 9600)
ser.write('1')
however, when import python script exact same commands, not work!
this arduino code:
this python code:
what gives???

please help!!!!!!!
i able directly control multi-color led in arduino through python in terminal executing:
import serial
ser = serial.serial ('/dev/tty.usbserial-a6004p1r', 9600)
ser.write('1')
however, when import python script exact same commands, not work!
this arduino code:
code: [select]
// output
int redpin = 9; // red led
int greenpin = 10; // green led
int bluepin = 11; // blue led
// program variables
int serval = 0; // variable store data serial port
int redval = 0; // variables store values send pins
int greenval = 0;
int blueval = 0;
void setup()
{
pinmode(redpin, output); // sets pins output
pinmode(greenpin, output);
pinmode(bluepin, output);
serial.begin(9600);
serial.flush();
}
// main program
void loop() {
serval = serial.read(); // read serial port
if (serval >= '0' && serval <= '6' ) {
serval = serval - '0'; // convert character number
for(int i=0; < serval; i++) {
{
if (serval == 1)
{
redval = 10;
greenval = 255;
blueval = 0;
}
else if (serval == 2)
{
redval = 15;
greenval = 165;
blueval = 170;
}
else if (serval == 3)
{
redval = 255;
greenval = 230;
blueval = 0;
}
else if (serval == 4)
{
redval = 200;
greenval = 80;
blueval = 0;
}
else if (serval == 5)
{
redval = 255;
greenval = 0;
blueval = 0;
}
else
{
redval = 0;
greenval = 0;
blueval = 0;
}
}
analogwrite(redpin, redval); // write current values led pins
analogwrite(greenpin, greenval);
analogwrite(bluepin, blueval);
}
}
}
this python code:
code: [select]
import serial
ser = serial.serial('/dev/cu.usbserial-a9003upq', 9600)
ser.write('1')
what gives???

please help!!!!!!!
here theory: when open serial port resets arduino. in terminal there plenty of time reset happen, when import python sketch serial write happens fast occurs during reset.
try adding delay of couple seconds using time.sleep() between opening serial port , sending data.
-br
try adding delay of couple seconds using time.sleep() between opening serial port , sending data.
-br
Arduino Forum > Forum 2005-2010 (read only) > Software > Interfacing > Pyserial + Arduino
arduino
Comments
Post a Comment