1

I'd like for a 16x2 LCD monitor from Sparkfun to display sensor data from a 4-directional tilt sensor from Parallax. The Arduino board is a Duemillanove. The board will run and power both programs and modules, but the serial monitor displaying the tilt sensor data will not relay the info to the LCD display. Any suggestions?

Here are the two pieces of code I am trying to get to work together—they are both from Arduino:

For the tilt:

void setup() { 
   Serial.begin(9600); 
} 
void loop() { 
   Serial.print("Photo 1: "); 
   Serial.print(digitalRead(6), DEC); // Display Out 1 value 
   Serial.print("; "); 
   Serial.print("Photo 2: "); 
   Serial.println(digitalRead(7), DEC); // Display Out 2 value 
   delay(250); // Wiat 1/4 second 
}

For the LCD:

#include <SoftwareSerial.h> // Attach the serial display's RX line to digital pin 2 
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused) 
void setup() { 
  mySerial.begin(9600); // set up serial port for 9600 baud 
  delay(500); // wait for display to boot up 
} 
void loop() { 
  mySerial.write(254); // move cursor to beginning of first line    
  mySerial.write(128); 
  mySerial.write(" "); // clear display 
  mySerial.write(" "); 
  mySerial.write(254); // move cursor to beginning of first line 
  mySerial.write(128); 
  mySerial.write("movement recorded"); 
  while(1); // 
  wait forever 
}

Update:

I'm still tinkering with the suggested program from @Ariser shown below, but keep getting a compile error:

tilt_lcd_ase.cpp: In function ‘void loop()’:
tilt_lcd_ase.cpp:24:43: error: expected ‘)’ before ‘;’ token
tilt_lcd_ase.cpp:25:43: error: expected ‘)’ before ‘;’ token

I've done some research and one person had a similar problem, but it was within #include not void loop. The search continues, but if anyone has a suggestion please chime in. The liquid crystal library is also turning up similar errors but it needs a closer look.

2
  • Please give us your code, so we can work on it.
    – Ariser
    Nov 2, 2014 at 22:47
  • Try using the liquidcrystal library.
    – Handoko
    Nov 10, 2014 at 11:06

2 Answers 2

0

The serial monitor cannot relay data from different peripheral unit in an arduino system.

I think you have certain misconception of the hard and software. Your USB connection to the microcontroller on the arduino is just a trunk for a asynchronous serial transmission. In the past this was done via a RS-232 interface.

So, what is it, that you see in your serial monitor? That's just a bunch of characters sent by the microcontroller on your arduino. This data is sent from the microcontroller, because some piece of software fills the fifo of its serial interface. In fact, such commands are executed, when you call the method "Serial.print()". Data gets then sent to your PC, Mac or whatever you are running your IDE on.

Now on the very same microcontroller the software for your tilt sensor is being executed. How could the serial monitor (which resides on your PC) route data from your tilt sensor to your LCD display? I'll tell you: It can't.

(In fact you could write a terminal script on your PC which reads data from serial and writes it back to the same interface, but it may cross your mind, that this might be a very big indirection)

So what do you wanna accomplish? You want to put data read from a device connected to your Arduino to another device connected to your Arduino. So you better tell it the Arduino. The serial monitor will not be involved, hence you won't need any calls of Serial.print() to accomplish that.

To help you with that task, we need to see the code you have written so far.

Ok. first thing you need to know: You can't have two arduino sketches running simultaneously on your arduino. This is, because there is nothing like an operating system on your arduino which might provide a scheduler to run both programs. (If you want, look up scheduler on wiki, as it is very useful to know what you can't have)

So you need two pieces of code to run on the same microcontroller (the big black thing on your arduino) one doing the tilt sensor readings and the other printing data on your display. Hence you have to make one program out of two. To accomplish this, you have two problems to solve.

  1. you have to execute both code fragments alterantely, which, because the controller is fast enough, boils down to do both task nearly simultaneously

  2. you have to bring the data which is acquired by code part 1 from the tilt sensor to code part 2 writing to the display. This is (for people into programming) rather simple as it simply has to be stored in a variable.

I recommend you to look up some programming basics at the arduino home page concerning the "setup" function and the "loop" function. But for now: You can have only one of each running on your arduino. Everything which is inside "setup" is executed only once after powerup. Everything which is inside "loop" is going to be executed over and over again consecutively (if nothing breaks).

Your program might look like this:

#include <SoftwareSerial.h> // Attach the serial display's RX line to digital pin 2 
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused) 

char gl_senstr1[15];
char gl_senstr2[15];


void setup() { 
  mySerial.begin(9600); // set up serial port for 9600 baud 
  delay(500); // wait for display to boot up 
} 

void loop() { 
  mySerial.write(254); // move cursor to beginning of first line    
  mySerial.write(128); 
  mySerial.write(" "); // clear display 
  mySerial.write(" "); 
  mySerial.write(254); // move cursor to beginning of first line 
  mySerial.write(128); 
  mySerial.write("movement recorded"); 

sprintf(gl_senstr1, "%d", digitalRead(6);

sprintf(gl_senstr2, "%d", digitalRead(7);

  sprintf(gl_senstr1, "%d", digitalRead(6)); 
  sprintf(gl_senstr2, "%d", digitalRead(7)); 

  mySerial.write("sensor1: ");
  mySerial.write(gl_senstr1);
  mySerial.write(" sensor2: ");
  mySerial.write(gl_senstr2);

   delay(250); // Wiat 1/4 second 

}

I did not test it, because I neither own your display nor that sensor. But you can give it a try. You eventually have to dive into the art of programming if you want to accomlish little more than this. This is where the rubber meets the road.

1
  • I appended your code to your question, because it is much better to read there.
    – Ariser
    Nov 8, 2014 at 11:09
0

by the looks of it you need to close the brackets of the sprintf() statement shown below if you had copied the code from Ariser:

sprintf(gl_senstr1, "%d", digitalRead(6);
sprintf(gl_senstr2, "%d", digitalRead(7);

replace it with:

sprintf(gl_senstr1, "%d", digitalRead(6));
sprintf(gl_senstr2, "%d", digitalRead(7));

That is the error you are seeing because of sprintf() not seeing a close to its statement ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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