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?
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
}
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.
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
Looking at a few thousand lines of code is not easy nor fast if you're inexperienced in that language or libraries used in that code.
You can choose how you prefer to help people. Thanks either way, no doubt a little more experienced programmers will find it helpful and easily navigate through your code.
Now, I do see output in the console. Even though it doesn't get "cleared" (one line). Is that normal or the reason the Python program gets flooded and hangs? The code I got from a webseries on using Python and Arduino together, not my code.