Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hello I am controlling a arduino through html/php using processing basically what it does is it has a txt file in the web server that holds the value for the state of a led that gets changed through html/css the processing scetch runs in a loop and checks to see if the state has changed anytime the state changes its coresponding command runs here is my code

processing:

   import processing.serial.*;
   Serial port;

   void setup()  {

   /* This part must be altered to fit your local settings. The number in brackets after "Serial.list()" is where you declare what COM port your Arduino is connected to.
  If you get error messages, try a different number starting from 0 (e.g. 0, 1, 2, 3...) . */
  port = new Serial(this, Serial.list()[1], 9600);  // Open the port          that the Arduino board is connected to, at 9600 baud

  }
  void draw() {

  String onoroff[] = loadStrings("http://192.168.0.143/LEDstate.txt"); // Insert the location of your .txt file
  if (onoroff[0].equals("0") == true) {
  println(" - TELLING ARDUINO TO TURN LED1 OFF");
  port.write('0'); 
  }
  else if (onoroff[0].equals("1") == true) {
  println(" - TELLING ARDUINO TO TURN LED1 ON");
  port.write('1');

  } else if (onoroff[0].equals("2") == true) {
  println(" - TELLING ARDUINO TO TURN LED2 On");
  port.write('2');  // Send "L" over serial to set LED to LOW
  } else if (onoroff[0].equals("3") == true) {
  println(" - TELLING ARDUINO TO TURN LED2 OFF");
  port.write('3');
  } else if (onoroff[0].equals("4") == true) {
  println(" - TELLING ARDUINO TO TURN LED3 ON");
  port.write('4');
 }else if (onoroff[0].equals("5") == true) {
 println(" -TELLING ARDUINO TO TURN LED3 OFF");
 port.write('5');
 }
 delay(0); // Set your desired interval here, in milliseconds
 }

arduino sketch:

  int led1 = 13;
  int led2 = 12;
  int led3 = 11;
  int led4 = 10;
  int incomingByte; 
  String stringRead;


  void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  }

  void loop() {


  // see if there's incoming serial data:
  if (Serial.available() > 0) {
  // read the oldest byte in the serial buffer:

  incomingByte = Serial.read();
  // if it's a capital H (ASCII 72), turn on the LED:
  if (incomingByte == '0') {
  digitalWrite(led1, LOW);
  }
  if (incomingByte == '1') {
  digitalWrite(led1, HIGH);
  }
  if (incomingByte == '2') {
  digitalWrite(led2, HIGH);
  }
  if (incomingByte == '3') {
  digitalWrite(led2, LOW);
  }
  if (incomingByte == '4') {
  digitalWrite(led3, HIGH);
  }
  if (incomingByte == '5') {
  digitalWrite(led3, LOW);
  }
  }
  }

html/css:

  <html>

  <title>hpa arduino controller</title>

  <body>

  <center>
  <head id="header">
  <img src="http://192.168.0.143/images/arduinobg.png"/>

  </head>
  </center>

  <div class="right" id="main">
  <p id="mainp1">
  welcome the H.P.A html,Processing,Arduino controller to get things
  started turn on your arduino unit by conecting it to a power source
  it is prefered that you set up your arduino unit up around your       main home
  computer so that you can easily hook it to you computer but dont wory
  you cant also use it through local internet if you want it set up somewhere
  else. once set up and everything is ready to go goto your computer and run the program
  that says html_processin_arduino all systems should now bo up and running
  to control your units connected to the arduino unit just type in localhost into your browser
  and you should see a page pop up with controls and this quick   guide<p>

  </div>


  <div class="left" id="controls">

  <center><h4 id="controlsh4"> Controls </h4></center>

  <p id="labels">Led1:</p>

  <p>
  <b><a id="a1on"href="led.php?state=1">ON</a></b> /
  <b><a id="a1off" href="led.php?state=0">OFF</a></b>
  </p>

  <p id="labels">Led2:</p>

  <p>
  <b><a id="a1on"href="led.php?state=2">ON</a></b> /
  <b><a id="a1off" href="led.php?state=3">OFF</a></b>
  </p>

  <p id="labels">Led3:</p>

  <p>
  <b><a id="a1on"href="led.php?state=4">ON</a></b> /
  <b><a id="a1off" href="led.php?state=5">OFF</a></b>
  </p>
  </div>


  </body>
  </html>
  <style>
  #labels{
  color: #E25D15;
  }
  #controlsh4{
  color: #E39C0B;
  }
  #mainp1{
  color: #E39C0B;
  }
  img{
  width: 50%;
  height: 25%;
  }
  #controls{
  padding: 5px;
  height: 100%;
  width: 22%;
  background: #00747D;


  }

  #a1off:visited{
  color: grey;
  }
  #a1on{
  color: grey;
  }
  #a1on:hover{
  color: green;
  }
  #a1off:hover{
  color: red;
  }
  a{
  text-decoration: none;
  }
  #header{
  background-color: #00747D;
  }
  #main{
  align: right;
  width: 75%;
  height: 100%;
  background-color: #079CA3;
  }
  .right {
  float: right;
  padding: 10px;
  }
  .left {
  float: left;
  padding: 0px;
  }
  .center {
  float: center;
  padding: 0px;
  }
  </style>

could someone PLEASE tell me how to instead of using an interval i could use a string to pass through and could someone PLEASE help me make my code better by telling me if i have an error

share|improve this question
    
Couple of questions: 1) is something not working the way you expect? Sorry if I'm being dense, but I'm not getting what the error you're wondering about is. I'm also not sure what it is doing. Without those bits of information it is hard to know how to get started. 2) Are you wanting to set the delay interval using a string passed in through HTML? – dlu Jan 4 at 0:14
    
i am wanting to be able to use a string of text instead of an interval that way i could have more posibiltes instead of only 0-9 but i dont know where to get started – Nik Hendricks Jan 4 at 3:25
    
Well, you could use a bitmask. pass a hex value to the serial consisting of two bytes, so possible values would be 00 - FF. Use the first byte as the position of the LED, giving you 0-F (A possibility of 16 LEDS) and use the second byte as the ON/OFF state, meaning that the last one would always only be 0 or 1. with only 5 LEDS in your example, you possible commands would look like [00 turn off LED 0, 01 turn ON LED 0, 10 turn OFF LED1, 11 turn ON LED 1, ... 41 turn ON LED 4]... – FatherStorm Jan 4 at 21:29
    
Then in the Arduino code you can use MOD to see if the LED should be on or off [if(hexRecvd %2==1){ ledState =1;}else{ledState=0} ] and [ledByPosition =hexRecvd << 1;] now you have the first byte, which is the target LED, and the state you want to set the LED to. – FatherStorm Jan 4 at 21:40
    
Copy & Save what you have at this moment. Then you should start to try to change it into sending/receiving strings. First try to make your HTML save strings in your textfile. Second, try making your processing application send them to your arduino. And then try to make your arduino read them, and execute whatever function you want for that string. – Paul Jan 4 at 22:41

Well, you could use a bitmask. pass a hex value to the serial consisting of two bytes, so possible values would be 00 - FF. Use the first byte as the position of the LED, giving you 0-F

(A possibility of 16 LEDS)

and use the second byte as the ON/OFF state, meaning that the last one would always only be 0 or 1.

With only 5 LEDS in your example, you possible commands would look like

00 turn off LED 0 01 turn ON LED 0 10 turn OFF LED1 11 turn ON LED 1 ... 41 turn ON LED 4...

Then in the Arduino code you can use MOD (%) to see if the LED should be on or off

if(hexRecvd % 2==1){ ledState =HIGH; }else{ ledState=LOW; }

and by shifting the last byte off the value, get just the position.

ledByPosition =hexRecvd << 1; digitalWrite(ledByPosition , ledState);

now you have the first byte, which is the target LED, and the state you want to set the LED to.

share|improve this answer
    
I would suggest sending strings, rather than using "advanced optimalisation" to push as much info into a single value. I believe the "question asker" wanted to send strings and bitmasks/shifting might be a little overkill for his case. Fancy solution, probably not easy. – Paul Jan 4 at 22:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.