Go Down

Topic: PHP serial output problem (Read 777 times) previous topic - next topic

eivis

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:

<?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);

?>

Arduino:

int ledPin = 13;
int usbnumber = 0;
void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
}

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

   
        if (usbnumber  == 1){
            digitalWrite(ledPin, HIGH);
            delay(300);
           
        }else{
           
            digitalWrite(ledPin, LOW);
            delay(1000);
        }
        usbnumber = 0;
   
}


I hope somone can help. Thanks!

PaulS

Code: [Select]
    $i = (4);
Why are there () around the 4?

Code: [Select]
while (true){
    $i = (4);
    fwrite($fp, chr($i));
    sleep(3);
}

How do you expect this loop to end?

Code: [Select]
   $i = $i;
Why?

Code: [Select]
        Serial.println(usbnumber);
Why? There is nothing reading data written to the serial port?

Code: [Select]
            delay(300);
            delay(1000);

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?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy