RX/TX communication between arduino, asking help for the code

Hi,

I'm working on an art installation using two arduinos (duemilanove) to detect sounds with one arduino and send signal to another arduino (30m away) to activate a solenoid.

In advance, I must say I have no basic knowledge for programing or electrical engineering, so I'm sorry if this post itself dosen't make any sence. I researched by myself but finally I decide to post this because of time I have left until the exhibition.

I tried to use Xbee for wiress communication first, but I couldn't even set up Xbees, so for now I'm using RX/TX (both GND connected) communication between arduinos. It worked once when I made a prototype using two arduino unos, but it stop communicating after I actually installed them.

I check the all connections of the circuit, so I'm guessing that I might accidentally changed the code, so please check the code and give me advice if you could find something wrong. (this code is basically mash up of copy and paste from I found from Google. Sorry for the mess)

Sender with a sound sensor http://dx.com/p/arduino-microphone-sound-detection-sensor-module-red-135533 :

int analogValue0;
int THRESHOLD = 500;

void setup() {
Serial.begin(9600);
}

void loop() {
analogValue0 = analogRead(0);     

if (analogValue0 >= THRESHOLD)
  Serial.write(1);
  delay(100);

}

When I use Serial.print for checking Sensor sensitivity, it seems like working well. so I'm guessing problem is receiver.

Receiver with a 12V solenoid and a tactile button:

byte incomingByte;

const int gongPin = 3;            
const int buttonPin = 2;
const int THRESHOLD = 1;

int sensorReading = 0; 
int buttonState = 0;
long randOn = 0;         
long delayTime = 0;

void setup() {
 Serial.begin(9600);
 pinMode(gongPin, OUTPUT);
 pinMode(buttonPin, INPUT);
 randomSeed (analogRead (0));
}

void loop() {
  sensorReading = Serial.read(); 
  buttonState = digitalRead(buttonPin);

  if (sensorReading >= THRESHOLD) {
  randOn = random (100, 200);           
  delayTime = random (500, 5000);
    digitalWrite(gongPin, HIGH);
    delay(randOn);
    digitalWrite(gongPin, LOW);
    delay(delayTime);
   }
  
  else if (buttonState == HIGH) {
  digitalWrite(gongPin, HIGH); 
  delay(150);                  
  digitalWrite(gongPin, LOW);  
  delay(200);
   }
  
  else {
  digitalWrite(gongPin, LOW); 
  }
  delay(100); 
}

The solenoid works when I push the tactile button, but it doesn't receive the signal from the another arduino. I'm connecting RX to TX, TX to RX and GND. The sender arduino's power is supplied by the receiver's arduino.

What could be a problem with this setting?

Thank you in advance.

Why on earth are you using two arduinos? One will suffice and you can drive the solenoide over that sort of distance very easly.

You are reading serial data without checking if there is any data to read.

On the recieve side you use

  if (sensorReading >= THRESHOLD) {

But on the transmit you only ever send a one?

30m is a pretty good distance for a 5v signal to be used. Could be some signal degradation. Just as a communication test, you might put the below code on both the sending and receiving arduinos. Then using the serial monitor, send on and off to the sending arduino and see if the onboard LED is controlled on both the sending and receiving arduinos.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

Thank you very much for the quick response!

Grumpy_Mike

Yes, I feel like I'm wasting whole ability of arduino. However, I needed the solenoid and button to be close each other, because of structure of the location. Then sending analog value from sensor to the arduino 30m away seems to be problem. I didn't think I can handle building amplifier. So I decided to use serial communication, since I can simplify signal to 0 or 1. (but it seems like I still have same problem..)

Thank you for the comment, I will try with Serial.available() in my code.

zoomkat

Thank you so much for the comment, I will do communication test with your code, and come back!

zoomkat

I successfully upload the code to both arduinos and connected RX/TX to TX/RX and GND. I tried to send on or off using the serial monitor, but "serial on/off test 0021" comes up on the monitor every secounds and I can't send "on" or "off" to the sending arudino.

The TX light on the sender and the onboard LED on the receiver are blinking at the same time as the "serial on/off test 0021" message comes on the monitor.

Can I use the serial monitor using USB at the same time I connect RX/TX?

Best regards,

Can I use the serial monitor using USB at the same time I connect RX/TX?

No.

I would have the button and the solinoide mounted at the far end being driven from the arduino close to the analogue sensor.

Ok, Now I have installed 30m of 3 cables for RX, TX and ground. So if I add one more cable, as total 4 cables for 5V, Pin2, Pin3, and ground, I don't have any problem of these communications. huhh, it could be the easiest way. Thanks

Why do you need the TX and RX if you only have one arduino? It looks like you will need only a 4 core cable. However if you have a remote power source at the far end then you only need three cores, ground pin 2 and pin 3. How are you driving the solenoid? You can't do that direct from an arduino pin you will damage it, the arduino that is.

Grumpy_Mike: Why do you need the TX and RX if you only have one arduino?

Maybe because:

I'm working on an art installation using two arduinos (duemilanove) to detect sounds with one arduino and send signal to another arduino (30m away) to activate a solenoid.

zoomkat: Maybe because:

I thought Grumpy_Mike's suggestion was to use a single Arduino and use the wires to drive the solenoid directly rather than for comms, and his comment about freeing up the Rx and Tx wires makes sense to me in that context.

PeterH:

zoomkat: Maybe because:

I thought Grumpy_Mike's suggestion was to use a single Arduino and use the wires to drive the solenoid directly rather than for comms, and his comment about freeing up the Rx and Tx wires makes sense to me in that context.

The OP said he was using two arduinos. G_M said "Why do you need the TX and RX if you only have one arduino?" Apparent technical misunderstandings like this is why airplanes fall out of the sky due to human mistakes.

The OP said he was using two arduinos.

Yes I know and I said there was little point. Classic beginners mistake thinking you need two arduinos. You are going to have just as much trouble getting a serial connection at TTL as it is to read a button and drive a solenoid. The OP did say:-

Yes, I feel like I'm wasting whole ability of arduino.

Now if he wants to complicate things and waste an arduino then it is up to him, I would recommend against it.

kosdai:

zoomkat

I successfully upload the code to both arduinos and connected RX/TX to TX/RX and GND. I tried to send on or off using the serial monitor, but "serial on/off test 0021" comes up on the monitor every secounds and I can't send "on" or "off" to the sending arudino. The TX light on the sender and the onboard LED on the receiver are blinking at the same time as the "serial on/off test 0021" message comes on the monitor. Can I use the serial monitor using USB at the same time I connect RX/TX?

Best regards,

Try disconnecting the rx/tx that is connected to the sender rx and the receiver tx. This should prevent an echo loop between the two arduinos.

Thank you guys,

I figured out how to communicate through RX/TX. I'm going to use the code now since the cables and all installations are already up. but maybe I'm going to fix the cables during exhibition and use only one arduino.

Why do you need the TX and RX if you only have one arduino? It looks like you will need only a 4 core cable.

I meant, I already installed all installations including cables one week ago. and that was 3 cables for RX, TX and ground.

However if you have a remote power source at the far end then you only need three cores, ground pin 2 and pin 3.

The remote power souce, do you mean 5V for the button? I was thinking if I use 4 cables, maybe the 30m distance could be a problem for the 5v cable connected to the button?

Best,

kosdai: I was thinking if I use 4 cables, maybe the 30m distance could be a problem for the 5v cable connected to the button?

No more than it would be a problem for serial transmission - the currents are tiny, and the voltage drop is correspondingly tiny. Connecting a switch over long wires would likely be far more reliable over that distance than trying to use TTL serial transmission, since the TTL serial will be massively more susceptible to noise.

I don't see any reason to use the second Arduino in this case and in fact it's making your solution less likely to work as well as taking more effort to implement.

The remote power souce, do you mean 5V for the button?

No, a button takes no power to use. For the solenoid.