Hi. I need to put some serial data into a variable, but its on a command. So fx. if i write a command "serial in" into the terminal, it will wait until i write something else, and then store it in a variable. Can anyone help me with that?
I'm not sure what you want, is the parseInt() function useful ?
http://arduino.cc/en/Serial/parseInt
Maybe, but no idea how to use it.
I need the text to be printed on a 1602 display. Here's the source, its a operating system.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char lcdval;
char character;
String content = "";
String lcdvalue = "";
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
Serial.println("mOS v0.1 Alpha for PIC32MX320F128H (ChipKIT Uno32)");
Serial.println("All commands with small letters unless otherwise noted!");
Serial.println("Type 'help' for commands");
lcd.print("mOS v0.1 alpha");
lcd.setCursor(0, 1);
lcd.print("by Moondeck");
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
}
void loop()
{
void(* resetFunc) (void) = 0;
while(Serial.available()) {
character = Serial.read();
content.concat(character);
}
if (content == "about") {
Serial.println("mOS v0.1 Alpha for PIC32MX320F128H (ChipKIT Uno32) by Moondeck");
lcd.print("mOS v0.1 alpha");
lcd.setCursor(0, 1);
lcd.print("by Moondeck");
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
content = "";
}
if (content == "help") {
Serial.println("All commands with small letters unless otherwise noticed!");
Serial.println("List of commands:");
Serial.println("help - prints all commands");
Serial.println("about - about the system");
content = "";
}
(content == "lcd in") {
while(Serial.available() == 0){
character = Serial.read();
content.concat(content);
}
lcd.print(content);
content = "";
}
}
Before the "if (content == "about") {"
you could print it to the Serial port, so you can check what has been received.
But the "about" works, i need the "lcd in"
(content == "lcd in") {
That line needs to have an IF or an ELSE in front of it.
The way it is now it does nothing useful.
...R
it still wont work.
moondeck:
it still wont work.
I can't see what you are looking at so that doesn't tell me anything useful.
What does happen?
Why do you think it is not working?
What diagnostic tests have you done?
...R
I got it hooked up to a display, and when i type in lcd in and a text that should be displayed, it just hangs, and i need to reset the board.
This while loop will run forever, when there is nothing available.
while(Serial.available() == 0){
character = Serial.read();
content.concat(content);
}
So what do i do? Sorry, i am a noob, i have no expirience with programming, they dont learn it at school (13 yrs old).
A while-loop runs as long as something is true: while ( sky == blue ) ...
It is possible to use a number: while ( 10 ) ...
A while-loop stops if something is false: while ( moon == cheese ) ...
But it also stops at value 0 : while ( 0 ) ...
That is used with the first : while(Serial.available() ) {
It runs as long as something is available, and stops if "Serial.available()" return 0, because there was nothing available.
Do the same when you read the text after "lcd in".
if (content == "lcd in") {
while(Serial.available()) {
character = Serial.read();
content.concat(content);
}
lcd.print(content);
content = "";
}
So can i do it like
if (content == "lcd in") {
while(Serial.available() != 0) {
character = Serial.read();
content.concat(content);
}
lcd.print(content);
content = "";
}
Yes, that is the same. Perhaps even a little better, because it is now a real condition.
Cool, you already are thinking like a software engineer
Now, i changed the source a little, so the lcd has its own char and string, but it wont display anything, it would display the content of "content" string before the changes thou
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char lcdval;
char character;
String content = "";
String lcdvalue = "";
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
Serial.println("mOS v0.1 Alpha for PIC32MX320F128H (ChipKIT Uno32)");
Serial.println("All commands with small letters unless otherwise noted!");
Serial.println("Type 'help' for commands");
lcd.print("mOS v0.1 alpha");
lcd.setCursor(0, 1);
lcd.print("by Moondeck");
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
}
void loop()
{
void(* resetFunc) (void) = 0;
while(Serial.available()) {
character = Serial.read();
content.concat(character);
}
if (content == "about") {
Serial.println("mOS v0.1 Alpha for PIC32MX320F128H (ChipKIT Uno32) by Moondeck");
lcd.print("mOS v0.1 alpha");
lcd.setCursor(0, 1);
lcd.print("by Moondeck");
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
content = "";
}
if (content == "help") {
Serial.println("All commands with small letters unless otherwise noticed!");
Serial.println("List of commands:");
Serial.println("help - prints all commands");
Serial.println("about - about the system");
content = "";
}
if (content == "lcd in") {
while(Serial.available() != 0) {
lcdval = Serial.read();
lcdvalue.concat(lcdval);
}
lcd.print(lcdvalue);
content = "";
}
}
When you type in the serial monitor : "lcd in 12"
You are perhaps transmitting "lcd in 12" followed by carriage return and line feed.
I don't know what happens when you write that to the lcd.
Have a look at these two lines.
lcd.print(lcdvalue);
content = "";
Add a few lines to those.
Print a test message and show the lcdvalue on the Serial monitor
Serial.print("lcdvalue=");
Serial.println(lcdvalue);
lcd.print("test"); // the word "test" for now.
content = "";
lcdvalue = "";
If the lcdvalue looks okay on the serial monitor, you can try to print it on the lcd.
it just freezes after printing out "lcdvalue=" it does not output the actual value
(outputs "test" on the LCD thou)
How about using separate commands for inputing a string to the LCD and printing it out?