Help with Serial. Functions

Hi so I have been working on a serial interface for a while for my Arduino and I am basically right now just trying to set up a basic system of telling the "home" program that the Arduino is ready to start talking to it. I did that on my laptop by just creating a loop that sends one number and waits 1 second to receive another number back. I checked and my "home" program works great but I think I managed to mess up my Arduino code. I say this because it never sends any data back to my laptop. I checked using all the same parameters for serial with some generic example codes and everything works. I believe it just boils down to my improper use of the Arduino Serial type functions. So if you guys would look at my code and critique it a bit (tell me that I was a complete idiot and that I can't use that function like that) it would be greatly appreciated. As a little bit of a forewarning I am very new to programming Arduino and basically anything in general so expect some badly written code. So while doing so testing I found that the Rx led lights up when the "home" program is sending it's one number but it never goes anywhere from there. It also resets when I first open up to port like it should.

Arduino Code

int readSerial = 0;

void setup() {
  pinMode(5, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  readSerial = Serial.read();
  if (readSerial == 3) {
    Serial.write(4);
    readSerial = Serial.read();
    if (readSerial == 2) {
      Serial.write(3);
      readSerial = Serial.read();
      Serial.print(readSerial);
    }
  }
  
}

The Important parts of the Python code

er = serial.Serial("/dev/ttyACM0", 9600, timeout=1)
        connected = 0
        while connected == 0: 
            print("not connected") #Just debug message
            ser.write("3") #Seeing if the arduino is open to com yet
            if ser.read() == 4: #Arduino saying it is open to com
                 connected = 1 #Another debug message
                 print("connected")
            time.sleep(1)

            print("it has slept")
        while connected == 1:
              print("it worked")
              Some random code to do fun things but we never get this far

I can never seem to get it to go p

Hi

Couple of things to try.

You are sending the character '3'. This is sent as the ASCII code with decimal value 51. So your Arduino program needs to be checking for '3' in the if statement, not the decimal number 3.

Use Serial.available() before reading from serial, to check that a character has been received. Examples here: Data Input demo sketch - Programming Questions - Arduino Forum

Regards

Ray

something like this...

char myChar;
void setup() 
{
  pinMode(5, OUTPUT);
  Serial.begin(9600);
}
void loop() 
{
  if (Serial.available())
  {
    myChar = Serial.read();
    if (myChar == '3') 
    {
      Serial.write(4);
    }
    else if (myChar == '2')
    {
      Serial.write(3);
    }
  }
}

EDIT nested the if's

3_141592_Co:
trying to set up a basic system of telling the "home" program that the Arduino is ready to start talking to it.

Much simpler solution is to get the Arduino to send a message from setup() and for the PC program to wait until it receives that.

That is the basis of the program in this Thread which is probably a better examle than the somewhat similar demo of mine that @Hackscribble linked to.

...R