Reading problems between Arduino and Python

Hello!

As a beginner in using Arduino and Python, I’m trying to make run some examples from the Internet in order to have a greater challenge for the future.

I’m Trying to make run the program from this website : http://www.stealthcopter.com/blog/2010/02/python-interfacing-with-an-arduino/

Actually, I am using these codes:

For my Arduino (Uno):

     int ledPin = 13;  // choose the pin for the LED  
    int val = 0;      // variable for reading the pin status  
    char msg = ' ';   // variable to hold data from serial  
      
      
    void setup() {  
      pinMode(ledPin, OUTPUT);      // declare LED as output  
      Serial.begin(9600);  
      loop();
      Serial.print("Program Initiated\n");    
    }  
      
    void loop(){  
            // While data is sent over serial assign it to the msg  
        while (Serial.available()>0){   
            msg=Serial.read();  
        }  
       Serial.print("Program really Initiated\n");   
      if (msg=='Y') {              
        digitalWrite(ledPin, HIGH);  // turn LED ON  
        Serial.print("LED Activated\n");  
        msg=' ';  
      } else if (msg=='N') {  
        digitalWrite(ledPin, LOW); // turn LED OFF
        Serial.print("LED Not Activated\n");   
      }  else { Serial.print("Cannot read...\n");}
    }

For Python :

import serial  
import time  
  
locations = ['COM1', 'COM2','COM3','COM4','COM5','COM6','COM7','COM8','COM9']  
    
for device in locations:    
    try:    
        print "Trying...",device  
        arduino = serial.Serial(device, 9600)   
        break  
    except:    
        print "Failed to connect on",device     
  
try:    
    arduino.write('Y')    
    time.sleep(1)  
    print arduino.readline()  
except:    
    print "Failed to send!"

My problem is that that the console display the messages, until the condition “if”. The board doesn’t work, it soesn’t display the message in the “if” condition and the led are not blinking for just run. I don’t know what to do… Do you know what is the problem?

Thanks!

    void setup() {  
      pinMode(ledPin, OUTPUT);      // declare LED as output  
      Serial.begin(9600);  
      loop();
      Serial.print("Program Initiated\n");    
    }

Why are you calling loop() from setup()?

            // While data is sent over serial assign it to the msg  
        while (Serial.available()>0){   
            msg=Serial.read();  
        }

The comment is wrong. What it should say is

// While data is sent over serial, throw away all by the last character, which get stored in msg

You get output from the Python program that you didn't bother to share. You see, or do not see, the LEDs on the Arduino blink. You didn't share that information, either.

I think the exact same code is the subject of this thread Serial communication from Linux to arduino nano ok. mega 2560 not activating. - #8 by Robin2 - Interfacing w/ Software on the Computer - Arduino Forum

It may be worth looking at.

...R