PythonAndArduino

From Fabelier
Jump to: navigation, search

Python and Ardino is a project from ProgrammingWorkshop, we're interfacing Python and Arduino over serial.

The first try was to control a LED from the keyboard.

First, the Arduino part, that read on serial :

 /*
   Blink
   Turns on an LED on for one second, then off for one second, repeatedly.
 
   This example code is in the public domain.
  */
 
 // Pin 13 has an LED connected on most Arduino boards.
 // give it a name:
 int led = 13;
 
 // the setup routine runs once when you press reset:
 void setup() {
   // initialize the digital pin as an output.
   Serial.begin(57600);
   pinMode(led, OUTPUT);
 }
 
 // the loop routine runs over and over again forever:
 void loop() {
   if(Serial.available() >= 1){
     if (byte(Serial.read())==0) {
       digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
     }
     else {
       digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
     }
   }
 }

Then, the Python part, that read from keyboard and write to serial :

 # -*- coding: utf-8 -*-
 import serial
 
 # Setup the Serial Object
 ser = serial.Serial()
 # Set the Serial Port to use
 ser.setPort("/dev/ttyACM0")
 # Set the Baudrate (Arduino Sketch is expecting 57600 for smooth transitions in the GUI)
 ser.baudrate = 57600
 # Open the Serial Connection
 ser.open()
 loopVar = True
 
 import time
 
 i = 0
 if (ser.isOpen()):
   # Start a main loop
   while (loopVar):
     i += 1
     car = raw_input("Entrer un car ")
     if car == 'a' :
       print 'allume'
       ser.write('a')
     elif car == 'q' :
       break
     else :
       print '├ęteint'
       ser.write('\0')
   ser.close()
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox