Signal sending

Hello everyone,
I was hoping someone could help us. We are trying to send a remote control command through an IR LED. Sending works fine but the problem is that the arduino sends the code too many times :~, causing the device to constantly respond to the code that is constantly sent. We need it to send the code just once and then rest untill told to send another code.

Any ideas anyone? :slight_smile:

Sounds like your sketch is bad. Show the sketch and maybe someone will be able to tell you where it's bad.

Thank you for replying, i forgot to mention that we are using both Arduino and Processing.

This is our Arduino script:

const int ledPin2 = 10;
const int ledPin3 = 12;
#include <IRremote.h>

IRsend irsend;

int incomingByte; // a variable to read incoming serial data into

void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
//pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, 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 (incomingByte == ‘A’) {
for (int i = 0; i < 1; i++) {
irsend.sendMitsubishi(0xC4D36480,0x0024C010,0x00000000,0x00220000);
}
}
if (incomingByte == ‘U’) {
for (int i = 0; i < 3; i++) {
irsend.sendMitsubishi(0xC4D36480,0x0004C010,0x00000000,0x00020000);
}
}

// if it’s a capital A (ASCII 72), turn on the LED:
// if (incomingByte == ‘A’) {

//digitalWrite(ledPin, HIGH);
// }
// if it’s an U (ASCII 76) turn off the LED:
// if (incomingByte == ‘U’) {

//digitalWrite(ledPin, LOW);
// }
}

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 == ‘H’) {
digitalWrite(ledPin2, HIGH);
}
// if it’s an L (ASCII 76) turn off the LED:
if (incomingByte == ‘L’) {
digitalWrite(ledPin2, LOW);
}

}

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 == ‘O’) {
digitalWrite(ledPin3, HIGH);
}
// if it’s an L (ASCII 76) turn off the LED:
if (incomingByte == ‘F’) {
digitalWrite(ledPin3, LOW);
}
}
}

This is our Processing script:

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…) . */
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600); // Open the port that the Arduino board is connected to, at 9600 baud

}
void draw() {

String onoroff = loadStrings(“http://127.0.0.1:90/TestfolderAirco/LEDstate.txt”); // Insert the location of your .txt file
print(onoroff[0]);
if (onoroff[0].equals(“1”) == true) {
println(" - TELLING ARDUINO TO TURN LED 1 ON");
port.write(‘A’); // Send “H” over serial to set LED 1 to HIGH

} else if(onoroff[0].equals(“0”) == true) {

println(" - TELLING ARDUINO TO TURN LED 1 OFF");
port.write(‘U’); // Send “L” over serial to set LED 1 to LOW
}
else if (onoroff[0].equals(“2”) == true) {
println(" - TELLING ARDUINO TO TURN LED 2 ON");
port.write(‘H’); //Send “H” over serial to set LED 2 to HIGH

} else if(onoroff[0].equals(“3”) == true) {
println(" - TELLING ARDUINO TO TURN LED 2 OFF");
port.write(‘L’); //Send “L” over serial to set LED 2 to LOW
}

delay(0000); // Set your desired interval here, in milliseconds
}

i should also explain why the code is like this. Previously we used this exact code to controll 3 LEDs online. After its succes we have only replaced what the ON and OFF buttons do for LED 1. Instead of turning LED 1 on and off, it sends a ON and OFF signal for an Airconditioner we use. the code for the rest of the LEDs remains the same.
Thats why modifying only the arduino script was necessary. This is the site where we originally started from. /projects/posts/how-to-led-arduino-php-proc/

Typically you would read the incoming serial character once and act in the value. The way you have it written once character is checked for A or U, the next is checked for H or L and the next for O or F. If you get an H, L, O or F for the first character in loop() it won’t be acted on.

A series of IF statements all testing an integer variable against integer constants is so commonly used there is a shortcut for it called a ‘switch’ statement.

Try something like this instead:

void loop() {
  // see if there's incoming serial data:
  if (!Serial.available())
     return; // No input so nothing more to do

    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();

   switch (incomingByte) {
case 'A':
    irsend.sendMitsubishi(0xC4D36480,0x0024C010,0x00000000,0x00220000);
    break;

case 'U':
    for (int i = 0; i < 3; i++) {
       irsend.sendMitsubishi(0xC4D36480,0x0004C010,0x00000000,0x00020000);
    }
   break;

case 'H': 
    digitalWrite(ledPin2, HIGH);
    break;

case 'L':
      digitalWrite(ledPin2, LOW);
      break;
    
case 'O':
      digitalWrite(ledPin3, HIGH);
      break;

case 'F':
      digitalWrite(ledPin3, LOW);
      beak;
  }
}

Thanks for your code, but we are still experiencing the same issues.
Perhaps it could be processing that is causing this? check out this screenshot here. This is processing, sending the code over and over continuously(i believe).
http://img109.imageshack.us/img109/359/0fu6.jpg

Yes, it looks like the mistake is in the Processing sketch.

any ideas as to modifying the processing code? no matter what i try i keep getting "found one too many{characters without a} to match it" when trying to put in something like "switch (incomingByte) {" i also get the same when trying to use for statements. As break statements will not work outside of "for"- or "switch" statements.

P.S (Not a pro at this stuff) :roll_eyes:

The issue is that loadStrings() doesn't remove the text from your text file. The draw function is called 30 times per second, which means processing sends Serial data potentially up to 30 times per second.

Try this:

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...) . */
  println(Serial.list());
  port = new Serial(this, Serial.list()[0], 9600);  // Open the port that the Arduino board is connected to, at 9600 baud
}

void draw() {
  String filename = "http://127.0.0.1:90/TestfolderAirco/LEDstate.txt";
  String onoroff[] = loadStrings(filename); // Insert the location of your .txt file
  if ((onoroff == null) || (onoroff.length == 0)) {
    return; //prevent a crash if there is nothing in the file.
  }
  print(onoroff[0]);  
  
  try {
    PrintWriter writer = new PrintWriter(filename);
    writer.print("");
    writer.close(); //clear the text file.
  } catch (Exception e){
    println();
    println("File couldn't be cleared"); //There was an exception raised, so handle it somehow.
    return;
  }
  
  if (onoroff[0].equals("1") == true) {
    println(" - TELLING ARDUINO TO TURN LED 1 ON");
    port.write('A'); // Send "H" over serial to set LED 1 to HIGH
  } else if (onoroff[0].equals("0") == true) {
    println(" - TELLING ARDUINO TO TURN LED 1 OFF");
    port.write('U');  // Send "L" over serial to set LED 1 to LOW
  } else if (onoroff[0].equals("2") == true) {
    println(" - TELLING ARDUINO TO TURN LED 2 ON");
    port.write('H'); //Send "H" over serial to set LED 2 to HIGH
  } else if (onoroff[0].equals("3") == true) {
    println(" - TELLING ARDUINO TO TURN LED 2 OFF");
    port.write('L'); //Send "L" over serial to set LED 2 to LOW
  }

}

(Note the use of CODE TAGS - the little # symbol in the forum text editor. Also note the use of properly indented code which makes everything much easier to follow).