Problem with Arduino and pySerial

I got a problem. I recently bought an Arduino Uno board. I tried to make something funny like controlling an input from my computer. I used Python with pySerial and the program is the following:

arduino = serial.Serial(portaCOM, 9600, timeout = 1)
... in loop ->
arduino.write(value)


  def sliderUpdate(self, event):
        pos = self.slider.GetValue()
        arduino.write(pos)
        time.sleep(.1)
        print arduino.readline()

try:
    arduino = serial.Serial(portaCOM, 9600, timeout = 1)
except:
    print "Errore di connessione alla porta seriale"

The write value should send the value to my board though USB. The program loaded on board is:

 const int ledPin = 11;
 byte brightness;

 void setup(){
     Serial.begin(9600);
     pinMode(ledPin, OUTPUT);
 }

 void loop(){
     while(Serial.available()){
         brightness = Serial.read();
         Serial.print(brightness);
         analogWrite(ledPin, brightness); //LED doesn't refresh the brightness
         delay(10);
     }
 }

My LED is working properly. I tried with the Fade example provided by Arduino and it's working..

I checked if the program is sending properly the data. Yes, it is. It returns the same thing I sent before.

It should retrieve the value sent and set analaogWriter(pin, VALUE), but something is wrong or not working.

How can I fix this problem?

Solution

The Arduino code

const int ledPin = 11;
byte valoreLed;

void setup(){
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
}

void loop(){
    while(Serial.available()){
        valoreLed = Serial.read();
        analogWrite(ledPin, valoreLed);
        delay(20);
    }
}

Python script code:

pos = self.slider.GetValue()
arduino.write(chr(pos))

Thank you to everybody!! :)

Answers


  1. First of all, make sure your LED is properly connected. Anode (longer pin) to PWM 11 port and cathode (shorter pin) to ground, also you may need to add a resistor between cathode and ground depending on LED.
  2. Make sure you're writing to the right port from python (that FTDI cable is associated with in your OS).
  3. If you're not using FTDI cable with USB connector, make sure that all of the pins are connected to the right inputs.
  4. What is the value of value in your example? Try arduino.write(chr(0xFF)), does LED stay lit?

Need Your Help

Library to use css rules to style a NSView

css cocoa nsview

Is there a way to style NSViews using css like rules? So that you don't have to use drawings commands? is there a library for this?

What do F and D mean at the end of numeric literals?

java

I've seen some of this symbols, but I cannot find anything strange with it,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.