Sequential reading of Arduino

I am working on a web-based rover and use the serial port to communicate with the Arduino . I have written some PHP that just use fwrite() and write ASCII 1 or ASCII 2 to the serial port. Arduino listens on this port and makes stuff based on what it hears. I know that my PHP works, because whenever I tell him to send stuff, Arduino receives it. Here is the Arduino code:

 //This listens to the serial port (USB) and does stuff based on what it is hearing. int motor1Pin = 13; //the first motor port number int motor2Pin = 12; //the second motor port number int usbnumber = 0; //this variable holds what we are currently reading from serial void setup() { //call this once at the beginning pinMode(motor1Pin, OUTPUT); //Tell arduino that the motor pins are going to be outputs pinMode(motor2Pin, OUTPUT); Serial.begin(9600); //start up serial port } void loop() { //main loop if (Serial.available() > 0) { //if there is anything on the serial port, read it usbnumber = Serial.read(); //store it in the usbnumber variable } if (usbnumber > 0) { //if we read something if (usbnumber = 49){ delay(1000); digitalWrite(motor1Pin, LOW); digitalWrite(motor2Pin, LOW); //if we read an ASCII 1, stop } if (usbnumber = 50){ delay(1000); digitalWrite(motor1Pin, HIGH); digitalWrite(motor2Pin, HIGH); //if we read an ASCII 2, drive forward } usbnumber = 0; //reset } } 

So it should be pretty straight forward. Right now, when I send ASCII 1 or ASCII 2, the LED that I am testing (on pin 13) turns on and stays on. But if I send another ASCII 1 or 2, it will turn off and then on again. The goal is to enable it only if ASCII 1 was the last one sent and stayed until the second message was sent.

Edit: Here is my PHP:

 <?php $verz="0.0.2"; $comPort = "com3"; /*change to correct com port */ if (isset($_POST["rcmd"])) { $rcmd = $_POST["rcmd"]; switch ($rcmd) { case Stop: $fp =fopen($comPort, "w"); fwrite($fp, chr(1)); /* this is the number that it will write */ fclose($fp); break; case Go: $fp =fopen($comPort, "w"); fwrite($fp, chr(2)); /* this is the number that it will write */ fclose($fp); break; default: die('???'); } } ?> <html> <head><title>Rover Control</title></head> <body> <center><h1>Rover Control</h1><b>Version <?php echo $verz; ?></b></center> <form method="post" action="<?php echo $PHP_SELF;?>"> <table border="0"> <tr> <td></td> <td> </td> <td></td> </tr> <tr> <td> <input type="submit" value="Stop" name="rcmd"><br/> </td> <td></td> <td> <input type="submit" value="Go" name="rcmd"><br /> </td> </tr> <tr> <td></td> <td><br><br><br><br><br> </td> <td></td> </tr> </table> </form> </body> </html> 
+4
source share
4 answers

If it is C, then you have an assignment instead of a comparison in both tests, therefore both of them are true , therefore all entries are executed every time. Compile with a high warning level (e.g. -Wall -pedantic in GCC). Try the following:

 int a = 0; if ( a == 1 ) printf( "a is not one: %d\n", a ); if ( a = 1 ) printf( "a is one: %d\n", a ); 

From the PHP code you posted (I'm not an expert here), it looks like you are writing binary 1 as char, which is not ASCII 49, but ASCII 1 (soh), the same for 2. Try changing this to '1' in PHP code (or 1 in C. code)

Here's a link to an article on serial port management using PHP - I searched googled, I don’t know its quality - but it doesn’t seem enough to just write an integer in "com1" - this is from my domain, so good luck :)

+1
source

As Nikolai noted, it looks like you are doing the assignment (=), not the comparison (==) in your if operations.

A good habit some C programmers have is to put rvalues ​​on the left side of the comparisons so that the compiler generates an error if you accidentally use the assignment operator instead of the comparison operator:

  if (50 == usbnumber) {// This is okay.
     ...
 }

 if (50 = usbnumber) {// The compiler will generate an error here.
     ...
 }

This works no matter what compiler flags or warning level you use, because assigning r is illegal.

I have to add that this β€œsafety net” does not work if you need to compare two values.

+1
source

It may be too late, but I think your problem is that serial.read () reads only one character at a time. If you send β€œ49” from the PC when you call usbnumber = serial.read (), you will get β€œ4” first loop and β€œ9” second loop. None of them satisfy the conditions, so nothing is done, and usbnumber is reset to 0.

To fix, you can either change the serial.available condition as

 if (Serial.available() == 2) 

and then to convert to a number, follow these steps:

 usbnumber = Serial.read() * 10 + Serial.read(); 

Another option is to use the TextFinder library - I wrote a quick guide on my website http://mechariusprojects.com/thunderbolt/?p=60

Fur

0
source

I found your answer in search of other material, anyway, I just ran into (I think) the same problem as yours.

If you have not solved this yet, I think that the problems are not your code, but the automatic reset mechanism on the arduino board. That is: every time a new connection is established on the serial port, the arduino reset board allows you to download new firmware when programming through the serial port.

To verify this, try flashing the LED in your setup() function, if it flashes every time the page loads, this confirms my thesis.

Take a look here: http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

I decided by holding a 120 ohm resistor between + 5V and RESET. Just remember to remove it every time you want to download a new firmware onto your board.

0
source

All Articles