0

Ok so what I'm trying to do is turn an LED on with one python script and off with another one. Now the problem I'm facing is my python script has to keep hanging for the LED to stay on. I can't figure out how to read something from serial, close the coms while leaving the LED on.

'g' is what I'm sending from the on python script and 'h' will be sent from the off python script.

The arduino:

void setup(){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  Serial.write('g');
  Serial.write('h'); 
}

void loop(){ 
  if(Serial.read() == 'g' ){    
    digitalWrite(13, HIGH);
    Serial.end();
  }
  if(Serial.read() == 'h' ){    
    digitalWrite(13, LOW);
     Serial.end();
  }
} 

And the python

#! /usr/bin/python
## import the serial library
import serial

## Boolean variable that will represent 
## whether or not the arduino is connected
connected = False

## open the serial port that your ardiono 
## is connected to.
ser = serial.Serial("/dev/cu.wchusbserial1410", 9600)

## loop until the arduino is ready
while not connected:
    serin = ser.read()
    connected = True

ser.write("g")


while ser.read() == 'g':
    ser.read()

## close the port
ser.close()

the 'while ser.read() parts at the bottom was just me messing about trying to figure out what I need but so far no such luck.

Thanks in advance!

1
  • 1
    It seems to me you have several issues here. One of them: On Arduino side, do not close the serial port just after having read a value (Serial.end())... Commented Feb 16, 2015 at 17:13

2 Answers 2

1

In python code instead of using this serial command, simply use print command. Suppose you wanna send character g on the serial port then simply write:

print "g"

and it will be sent over to serial port. Worked for me while using Arduino YUN.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the feedback. I used a different method and thought it would be a good idea to share the code incase anyone is interested in doing the same.

Python:

import serial
import time

arduino = serial.Serial('/dev/tty.wchusbserial1410', 9600)
time.sleep(0.1) # wait
print("initialising")

arduino.write('off') # turns LED off
print("LED OFF")
time.sleep(0.1) # wait



arduino.close() # close serial

This is the code used to turn the light off. If you want to turn it on, it's the same procedure but create another script replacing arduino.write('off') with arduino.write('on')

And Arduino:

int led = 13; // Pin 13

void setup()
{
    pinMode(led, OUTPUT); // Set pin 13 as digital out

    // Start up serial connection
    Serial.begin(9600);
    Serial.flush();
}

void loop()
{
    String input = "";

    // Read any serial input
    while (Serial.available() > 0)
    {
        input += (char) Serial.read(); // Read in one char at a time
        delay(5); // Delay for 5 ms so the next char has time to be received
    }

    if (input == "on")
    {
        digitalWrite(led, HIGH); // on
    }
    else if (input == "off")
    {
        digitalWrite(led, LOW); // off
    }
}

The one problem with this script is after the serial coms close the light turns off. To fix this, I used a 10uF electrolytic capacitor between the ground and reset pin to keep the serial port open. (Please note: only put the cap in AFTER you've programmed the Arduino. If you need to reprogram, pull it out first.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.