PHP serial output problem

I am trying to turn a LED on and OFF on the arduino trough a piece of PHP code that is on my computer.

I have managed to get communication between the arduino an the computer, and the arduino to react accordingly to the code I hve written. But i have one big problem. The PHP code seems to “remeber” all the characters I have put in to $i. Ex: I start the PHP code with the character $i = (0), I then get 0 0 0 0… in my serial monitor. But if I change the $i value to ex: $i = (1) I then get 0 1 0 1 0 1… in my serial monitor and if I again change it to $i = (3) the serial monitor will diplay 0 1 3 0 1 3 0 1 3…

So my question is, are there any ways to get the PHP code to only output the value of $i at that given moment, and not all the values $i has been.

Sorry about my poor english grammar.

Here are the code for my little project.


<?php exec("mode /dev/du.usbmodem3d11: BAUD=9600 PARITY=N data=8 stop=1 xon=off"); $fp =fopen("/dev/cu.usbmodem3d11", "w"); while (true){ $i = (4); fwrite($fp, chr($i)); sleep(3); } $i = $i; fclose($fp); ?>


int ledPin = 13;
int usbnumber = 0;
void setup() {
pinMode(ledPin, OUTPUT);

void loop() {
if (Serial.available() > 0) {
usbnumber =;

if (usbnumber == 1){
digitalWrite(ledPin, HIGH);


digitalWrite(ledPin, LOW);
usbnumber = 0;


I hope somone can help. Thanks!

    $i = (4);

Why are there () around the 4?

while (true){
    $i = (4);
    fwrite($fp, chr($i));

How do you expect this loop to end?

   $i = $i;



Why? There is nothing reading data written to the serial port?


Why? You are sending serial data every 3 seconds? Why wait before checking for more?

In general, why are you changing the pin state if no new serial data has arrived?