Note that there are some explanatory texts on larger screens.

plurals
  1. POArduino serial reading
    text
    copied!<p>I am working on a web controlled rover and am using a serial port to communicate with an <a href="http://en.wikipedia.org/wiki/Arduino" rel="nofollow noreferrer">Arduino</a>. I wrote some PHP that just uses <code>fwrite()</code> and writes an ASCII 1 or an ASCII 2 to the serial port. The Arduino is listening to that port and does stuff based on what it hears. I know my PHP is working, because whenever I tell it to send stuff, the Arduino does receive it. Here is the Arduino code:</p> <pre><code>//This listens to the serial port (USB) and does stuff based on what it is hearing. int motor1Pin = 13; //the first motor's port number int motor2Pin = 12; //the second motor's 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() &gt; 0) { //if there is anything on the serial port, read it usbnumber = Serial.read(); //store it in the usbnumber variable } if (usbnumber &gt; 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 } } </code></pre> <p>So this should be fairly straight forward. Right now, when I send either an ASCII 1 or an ASCII 2, the LED I am testing with (on pin 13) turns on and stays on. But, if I send another ASCII 1 or 2, it turns off and then turns back on. The goal is to have it turn on only if an ASCII 1 was the last thing sent and to stay on until a 2 was the last thing sent.</p> <p>Edit: Here's my PHP:</p> <pre><code>&lt;?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('???'); } } ?&gt; &lt;html&gt; &lt;head&gt;&lt;title&gt;Rover Control&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;center&gt;&lt;h1&gt;Rover Control&lt;/h1&gt;&lt;b&gt;Version &lt;?php echo $verz; ?&gt;&lt;/b&gt;&lt;/center&gt; &lt;form method="post" action="&lt;?php echo $PHP_SELF;?&gt;"&gt; &lt;table border="0"&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="submit" value="Stop" name="rcmd"&gt;&lt;br/&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt; &lt;input type="submit" value="Go" name="rcmd"&gt;&lt;br /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload