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"; if (isset($_POST["rcmd"])) { $rcmd = $_POST["rcmd"]; switch ($rcmd) { case Stop: $fp =fopen($comPort, "w"); fwrite($fp, chr(1)); fclose($fp); break; case Go: $fp =fopen($comPort, "w"); fwrite($fp, chr(2)); 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>
source share