aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Fissore <f.fissore@arduino.cc>2013-06-13 17:29:23 +0200
committerFederico Fissore <f.fissore@arduino.cc>2013-06-13 17:29:23 +0200
commit5a965943a538fc7213b8bf603d0cd97e31155107 (patch)
tree4b9d71907b1f54f9d290c8d1e727c8a3aa78ce3e
parent76f75eee51b28814ed46458dd2de6955bbdc656b (diff)
adding ConsoleRead example
-rw-r--r--libraries/Bridge/examples/ConsoleRead/ConsoleRead.ino43
1 files changed, 43 insertions, 0 deletions
diff --git a/libraries/Bridge/examples/ConsoleRead/ConsoleRead.ino b/libraries/Bridge/examples/ConsoleRead/ConsoleRead.ino
new file mode 100644
index 0000000..98f3b4d
--- /dev/null
+++ b/libraries/Bridge/examples/ConsoleRead/ConsoleRead.ino
@@ -0,0 +1,43 @@
+/*
+ Console.read() example:
+ read data coming from bridge using the Console.read() function
+ and store it in a string.
+
+ created 13 Jun 2013
+ by Angelo Scialabba
+
+ This example code is in the public domain.
+ */
+
+#include <Console.h>
+
+String name;
+int current_char;
+
+void setup() {
+ //Initialize Console and wait for port to open:
+ Bridge.begin();
+ Console.begin();
+
+ while (!Console) {
+ ; // wait for Console port to connect.
+ }
+ Console.println("Hi, who are you?");
+}
+
+void loop() {
+ current_char = Console.read(); //read the next char received
+ //look for the newline character, this is the last character in the string
+ if (current_char == '\n') {
+ //print text with the name received
+ Console.print("Hi ");
+ Console.print(name);
+ Console.println("! Nice to meet you!");
+ Console.println();
+ //Ask again for name and clear the old name
+ Console.println("Hi, who are you?");
+ name = "";
+ } else if (current_char != -1) { //if the buffer is empty Cosole.read returns -1
+ name = name + (char)current_char; //current_char is int, treat him as char and add it to the name string
+ }
+}