Serial never available?

void setup() {
  Serial.begin(9600); // Sets the data rate in bits per second (baud) for serial data transmission
  
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) { // Get the number of bytes (characters) available for reading from the serial port

    int val = digitalRead(12);  // read input value
    if (val == HIGH) {         // check if the input is HIGH (button released)
        Serial.print('1-1');
    } else {
        Serial.print('1-0');    
    }
  }
}

Code never seems to pass the if (Serial.available() > 0) statement. But I an upload this to my arduino so USB connection is okay. Whats wrong?

  Serial.print('1-1');

Show us the '1-1' and the '1-0' keys on your keyboard.

You never read anything from serial.

You need to send something over the Serial port (to the Arduino), for Serial.available() > 0 to become true.

Unless it receives something, it will never get beyond that point.

Ian.

aarg: You never read anything from serial.

So?

AWOL: So?

So why bother checking it with Serial.available()? :)

I don't want to read from Serial, but write to it if button on pin 12 is pressed. Seems like I got it wrong. What's the right way to do it?

Try double quotes, instead of single quotes?

You don't need available to write. That available function tells you if there is serial data waiting to be read. To write all you need is the begin statement in setup.

Thanks all. The Python program which reads what Arduino writes to the serial seems to hang. Do I need to clear something, feels like its getting flooded.

ghosttrain:
I don’t want to read from Serial, but write to it if button on pin 12 is pressed. Seems like I got it wrong. What’s the right way to do it?

Some serial/button code that might be similar to what you want. Note that the arduino loops very fast and will spew status to the serial port unless you factor that into your code.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
  Serial.begin(9600);
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      Serial.println("Button ON");
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      Serial.println("Button OFF");
      toggle = !toggle;
    }
  }
  delay(300);  //delay for debounce
}

not sure what that code does differently. I have a delay set too.

not sure what that code does differently

Well, we can't see your code, so we can't be expected to tell you.

void setup() {
  Serial.begin(9600); // Sets the data rate in bits per second (baud) for serial data transmission
  
}

void loop() {
    int val = digitalRead(12);  // read input value
    if (val == HIGH) {         // check if the input is HIGH (button released)
        Serial.print("1-1");
    } else {
        Serial.print("1-0");    
    }
    
    // don't do all this every millisecond
    delay(100);
  
}

ghosttrain: The Python program which reads what Arduino writes to the serial seems to hang. Do I need to clear something, feels like its getting flooded.

Have a look at this Python - Arduino demo

...R

Are you saying there is no problem with my above code, and maybe something wrong in my Python code?

import serial

arduinoSerialData = serial.Serial('com6', 9600)

def loopingFunction():
    if arduinoSerialData.inWaiting() > 0:
        datastr = arduinoSerialData.readline()
        print datastr

# looping above function doesnt work

Thats a rather large demo to just link to it.

ghosttrain: Thats a rather large demo to just link to it.

Does that refer to my Python - Arduino demo ?

The Arduino file is just 4k and the Python code is 4.5k

...R

1K+ code might be "just" 1K for experienced coder or the coder author, not me though. What I posted is just 6 lines.

ghosttrain: 1K+ code might be "just" 1K for experienced coder or the coder author, not me though. What I posted is just 6 lines.

Have you looked at my code ?

I suspect the relevant piece is not much different in size from your snippet.

I wrote one piece of working code and I have made it publicly available. I am not going to duplicate that effort.

...R

looping above function doesnt work

Open the Serial Monitor, instead of the python program.

If you see data from the Arduino in the Serial Monitor application, then the problem is in the python code, and this is not the place to discuss that.

If you don't see data, then the problem is (however unlikely) with the Arduino.

If you are currently opening both applications, and not getting data in the python program, well, then stop doing that shit.