arduino-0022
This commit is contained in:
parent
4f99742f03
commit
a9ad0e80a0
803 changed files with 69785 additions and 33024 deletions
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
Character analysis operators
|
||||
|
||||
Examples using the character analysis operators.
|
||||
Send any byte and the sketch will tell you about it.
|
||||
|
||||
created 29 Nov 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
// Open serial communications:
|
||||
Serial.begin(9600);
|
||||
|
||||
// send an intro:
|
||||
Serial.println("send any byte and I'll tell you everything I can about it");
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// get any incoming bytes:
|
||||
if (Serial.available() > 0) {
|
||||
int thisChar = Serial.read();
|
||||
|
||||
// say what was sent:
|
||||
Serial.print("You sent me: \'");
|
||||
Serial.write(thisChar);
|
||||
Serial.print("\' ASCII Value: ");
|
||||
Serial.println(thisChar);
|
||||
|
||||
// analyze what was sent:
|
||||
if(isAlphaNumeric(thisChar)) {
|
||||
Serial.println("it's alphanumeric");
|
||||
}
|
||||
if(isAlpha(thisChar)) {
|
||||
Serial.println("it's alphabetic");
|
||||
}
|
||||
if(isAscii(thisChar)) {
|
||||
Serial.println("it's ASCII");
|
||||
}
|
||||
if(isWhitespace(thisChar)) {
|
||||
Serial.println("it's whitespace");
|
||||
}
|
||||
if(isControl(thisChar)) {
|
||||
Serial.println("it's a control character");
|
||||
}
|
||||
if(isDigit(thisChar)) {
|
||||
Serial.println("it's a numeric digit");
|
||||
}
|
||||
if(isGraph(thisChar)) {
|
||||
Serial.println("it's a printable character that's not whitespace");
|
||||
}
|
||||
if(isLowerCase(thisChar)) {
|
||||
Serial.println("it's lower case");
|
||||
}
|
||||
if(isPrintable(thisChar)) {
|
||||
Serial.println("it's printable");
|
||||
}
|
||||
if(isPunct(thisChar)) {
|
||||
Serial.println("it's punctuation");
|
||||
}
|
||||
if(isSpace(thisChar)) {
|
||||
Serial.println("it's a space character");
|
||||
}
|
||||
if(isUpperCase(thisChar)) {
|
||||
Serial.println("it's upper case");
|
||||
}
|
||||
if (isHexadecimalDigit(thisChar)) {
|
||||
Serial.println("it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)");
|
||||
}
|
||||
|
||||
// add some space and ask for another byte:
|
||||
Serial.println();
|
||||
Serial.println("Give me another byte:");
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
Adding Strings together
|
||||
|
||||
Examples of how to add strings together
|
||||
You can also add several different data types to string, as shown here:
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringAdditionOperator
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
// declare three strings:
|
||||
String stringOne, stringTwo, stringThree;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
stringOne = String("stringThree = ");
|
||||
stringTwo = String("this string");
|
||||
stringThree = String ();
|
||||
Serial.println("\n\nAdding strings together (concatenation):");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// adding a constant integer to a string:
|
||||
stringThree = stringOne + 123;
|
||||
Serial.println(stringThree); // prints "stringThree = 123"
|
||||
|
||||
// adding a constant long interger to a string:
|
||||
stringThree = stringOne + 123456789;
|
||||
Serial.println(stringThree); // prints " You added 123456789"
|
||||
|
||||
// adding a constant character to a string:
|
||||
stringThree = stringOne + 'A';
|
||||
Serial.println(stringThree); // prints "You added A"
|
||||
|
||||
// adding a constant string to a string:
|
||||
stringThree = stringOne + "abc";
|
||||
Serial.println(stringThree); // prints "You added abc"
|
||||
|
||||
stringThree = stringOne + stringTwo;
|
||||
Serial.println(stringThree); // prints "You added this string"
|
||||
|
||||
// adding a variable integer to a string:
|
||||
int sensorValue = analogRead(A0);
|
||||
stringOne = "Sensor value: ";
|
||||
stringThree = stringOne + sensorValue;
|
||||
Serial.println(stringThree); // prints "Sensor Value: 401" or whatever value analogRead(A0) has
|
||||
|
||||
// adding a variable long integer to a string:
|
||||
long currentTime = millis();
|
||||
stringOne="millis() value: ";
|
||||
stringThree = stringOne + millis();
|
||||
Serial.println(stringThree); // prints "The millis: 345345" or whatever value currentTime has
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
Appending to Strings using the += operator and concat()
|
||||
|
||||
Examples of how to append different data types to strings
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringAppendOperator
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
String stringOne, stringTwo;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
stringOne = String("Sensor ");
|
||||
stringTwo = String("value");
|
||||
Serial.println("\n\nAppending to a string:");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println(stringOne); // prints "Sensor "
|
||||
|
||||
// adding a string to a string:
|
||||
stringOne += stringTwo;
|
||||
Serial.println(stringOne); // prints "Sensor value"
|
||||
|
||||
// adding a constant string to a string:
|
||||
stringOne += " for input ";
|
||||
Serial.println(stringOne); // prints "Sensor value for input"
|
||||
|
||||
// adding a constant character to a string:
|
||||
stringOne += 'A';
|
||||
Serial.println(stringOne); // prints "Sensor value for input A"
|
||||
|
||||
// adding a constant integer to a string:
|
||||
stringOne += 0;
|
||||
Serial.println(stringOne); // prints "Sensor value for input A0"
|
||||
|
||||
// adding a constant string to a string:
|
||||
stringOne += ": ";
|
||||
Serial.println(stringOne); // prints "Sensor value for input"
|
||||
|
||||
// adding a variable integer to a string:
|
||||
stringOne += analogRead(A0);
|
||||
Serial.println(stringOne); // prints "Sensor value for input A0: 456" or whatever analogRead(A0) is
|
||||
|
||||
Serial.println("\n\nchanging the Strings' values");
|
||||
stringOne = "A long integer: ";
|
||||
stringTwo = "The millis(): ";
|
||||
|
||||
// adding a constant long integer to a string:
|
||||
stringOne += 123456789;
|
||||
Serial.println(stringOne); // prints "A long integer: 123456789"
|
||||
|
||||
// using concat() to add a long variable to a string:
|
||||
stringTwo.concat(millis());
|
||||
Serial.println(stringTwo); // prints "The millis(): 43534" or whatever the value of the millis() is
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
String Case changes
|
||||
|
||||
Examples of how to change the case of a string
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringCaseChanges
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString case changes:");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// toUpperCase() changes all letters to upper case:
|
||||
String stringOne = "<html><head><body>";
|
||||
Serial.println(stringOne);
|
||||
stringOne = (stringOne.toUpperCase());
|
||||
Serial.println(stringOne);
|
||||
|
||||
// toLowerCase() changes all letters to lower case:
|
||||
String stringTwo = "</BODY></HTML>";
|
||||
Serial.println(stringTwo);
|
||||
stringTwo = stringTwo.toLowerCase();
|
||||
Serial.println(stringTwo);
|
||||
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
String charAt() and setCharAt()
|
||||
|
||||
Examples of how to get and set characters of a String
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringCharacters
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString charAt() and setCharAt():");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// make a string to report a sensor reading:
|
||||
String reportString = "SensorReading: 456";
|
||||
Serial.println(reportString);
|
||||
|
||||
// the reading's most significant digit is at position 15 in the reportString:
|
||||
String mostSignificantDigit = reportString.charAt(15);
|
||||
Serial.println("Most significant digit of the sensor reading is: " + mostSignificantDigit);
|
||||
|
||||
// add blank space:
|
||||
Serial.println();
|
||||
|
||||
// you can alo set the character of a string. Change the : to a = character
|
||||
reportString.setCharAt(13, '=');
|
||||
Serial.println(reportString);
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
/*
|
||||
Comparing Strings
|
||||
|
||||
Examples of how to compare strings using the comparison operators
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringComparisonOperators
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
String stringOne, stringTwo;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
stringOne = String("this");
|
||||
stringTwo = String("that");
|
||||
Serial.println("\n\nComparing Strings:");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// two strings equal:
|
||||
if (stringOne == "this") {
|
||||
Serial.println("StringOne == \"this\"");
|
||||
}
|
||||
// two strings not equal:
|
||||
if (stringOne != stringTwo) {
|
||||
Serial.println(stringOne + " =! " + stringTwo);
|
||||
}
|
||||
|
||||
// two strings not equal (case sensitivity matters):
|
||||
stringOne = "This";
|
||||
stringTwo = "this";
|
||||
if (stringOne != stringTwo) {
|
||||
Serial.println(stringOne + " =! " + stringTwo);
|
||||
}
|
||||
// you can also use equals() to see if two strings are the same:
|
||||
if (stringOne.equals(stringTwo)) {
|
||||
Serial.println(stringOne + " equals " + stringTwo);
|
||||
}
|
||||
else {
|
||||
Serial.println(stringOne + " does not equal " + stringTwo);
|
||||
}
|
||||
|
||||
// or perhaps you want to ignore case:
|
||||
if (stringOne.equalsIgnoreCase(stringTwo)) {
|
||||
Serial.println(stringOne + " equals (ignoring case) " + stringTwo);
|
||||
}
|
||||
else {
|
||||
Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);
|
||||
}
|
||||
|
||||
// a numeric string compared to the number it represents:
|
||||
stringOne = "1";
|
||||
int numberOne = 1;
|
||||
if (stringOne == numberOne) {
|
||||
Serial.println(stringOne + " = " + numberOne);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// two numeric strings compared:
|
||||
stringOne = "2";
|
||||
stringTwo = "1";
|
||||
if (stringOne >= stringTwo) {
|
||||
Serial.println(stringOne + " >= " + stringTwo);
|
||||
}
|
||||
|
||||
// comparison operators can be used to compare strings for alphabetic sorting too:
|
||||
stringOne = String("Brown");
|
||||
if (stringOne < "Charles") {
|
||||
Serial.println(stringOne + " < Charles");
|
||||
}
|
||||
|
||||
if (stringOne > "Adams") {
|
||||
Serial.println(stringOne + " > Adams");
|
||||
}
|
||||
|
||||
if (stringOne <= "Browne") {
|
||||
Serial.println(stringOne + " <= Browne");
|
||||
}
|
||||
|
||||
|
||||
if (stringOne >= "Brow") {
|
||||
Serial.println(stringOne + " >= Brow");
|
||||
}
|
||||
|
||||
// the compareTo() operator also allows you to compare strings
|
||||
// it evaluates on the first character that's different.
|
||||
// if the first character of the string you're comparing to
|
||||
// comes first in alphanumeric order, then compareTo() is greater than 0:
|
||||
stringOne = "Cucumber";
|
||||
stringTwo = "Cucuracha";
|
||||
if (stringOne.compareTo(stringTwo) < 0 ) {
|
||||
Serial.println(stringOne + " comes before " + stringTwo);
|
||||
}
|
||||
else {
|
||||
Serial.println(stringOne + " comes after " + stringTwo);
|
||||
}
|
||||
|
||||
delay(10000); // because the next part is a loop:
|
||||
|
||||
// compareTo() is handy when you've got strings with numbers in them too:
|
||||
|
||||
while (true) {
|
||||
stringOne = "Sensor: ";
|
||||
stringTwo= "Sensor: ";
|
||||
|
||||
stringOne += analogRead(A0);
|
||||
stringTwo += analogRead(A5);
|
||||
|
||||
if (stringOne.compareTo(stringTwo) < 0 ) {
|
||||
Serial.println(stringOne + " comes before " + stringTwo);
|
||||
}
|
||||
else {
|
||||
Serial.println(stringOne + " comes after " + stringTwo);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
String constructors
|
||||
|
||||
Examples of how to create strings from other data types
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringConstructors
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// using a constant String:
|
||||
String stringOne = "Hello String";
|
||||
Serial.println(stringOne); // prints "Hello String"
|
||||
|
||||
// converting a constant char into a String:
|
||||
stringOne = String('a');
|
||||
Serial.println(stringOne); // prints "a"
|
||||
|
||||
// converting a constant string into a String object:
|
||||
String stringTwo = String("This is a string");
|
||||
Serial.println(stringTwo); // prints "This is a string"
|
||||
|
||||
// concatenating two strings:
|
||||
stringOne = String(stringTwo + " with more");
|
||||
// prints "This is a string with more":
|
||||
Serial.println(stringOne);
|
||||
|
||||
// using a constant integer:
|
||||
stringOne = String(13);
|
||||
Serial.println(stringOne); // prints "13"
|
||||
|
||||
// using an int and a base:
|
||||
stringOne = String(analogRead(A0), DEC);
|
||||
// prints "453" or whatever the value of analogRead(A0) is
|
||||
Serial.println(stringOne);
|
||||
|
||||
// using an int and a base (hexadecimal):
|
||||
stringOne = String(45, HEX);
|
||||
// prints "2d", which is the hexadecimal version of decimal 45:
|
||||
Serial.println(stringOne);
|
||||
|
||||
// using an int and a base (binary)
|
||||
stringOne = String(255, BIN);
|
||||
// prints "11111111" which is the binary value of 255
|
||||
Serial.println(stringOne);
|
||||
|
||||
// using a long and a base:
|
||||
stringOne = String(millis(), DEC);
|
||||
// prints "123456" or whatever the value of millis() is:
|
||||
Serial.println(stringOne);
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
String indexOf() and lastIndexOf() functions
|
||||
|
||||
Examples of how to evaluate, look for, and replace characters in a String
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringIndexOf
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString indexOf() and lastIndexOf() functions:");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// indexOf() returns the position (i.e. index) of a particular character
|
||||
// in a string. For example, if you were parsing HTML tags, you could use it:
|
||||
String stringOne = "<HTML><HEAD><BODY>";
|
||||
int firstClosingBracket = stringOne.indexOf('>');
|
||||
Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);
|
||||
|
||||
stringOne = "<HTML><HEAD><BODY>";
|
||||
int secondOpeningBracket = firstClosingBracket + 1;
|
||||
int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket );
|
||||
Serial.println("The index of the second > in the string " + stringOne + " is " + secondClosingBracket);
|
||||
|
||||
// you can also use indexOf() to search for Strings:
|
||||
stringOne = "<HTML><HEAD><BODY>";
|
||||
int bodyTag = stringOne.indexOf("<BODY>");
|
||||
Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);
|
||||
|
||||
stringOne = "<UL><LI>item<LI>item<LI>item</UL>";
|
||||
int firstListItem = stringOne.indexOf("<LI>");
|
||||
int secondListItem = stringOne.indexOf("item", firstListItem + 1 );
|
||||
Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);
|
||||
|
||||
// lastIndexOf() gives you the last occurrence of a character or string:
|
||||
int lastOpeningBracket = stringOne.lastIndexOf('<');
|
||||
Serial.println("The index of the last < in the string " + stringOne + " is " + lastOpeningBracket);
|
||||
|
||||
int lastListItem = stringOne.lastIndexOf("<LI>");
|
||||
Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem);
|
||||
|
||||
|
||||
// lastIndexOf() can also search for a string:
|
||||
stringOne = "<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";
|
||||
int lastParagraph = stringOne.lastIndexOf("<p");
|
||||
int secondLastGraf = stringOne.lastIndexOf("<p", lastParagraph - 1);
|
||||
Serial.println("The index of the second last paragraph tag " + stringOne + " is " + secondLastGraf);
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
String length()
|
||||
|
||||
Examples of how to use length() in a String.
|
||||
Open the Serial Monitor and start sending characters to see the results.
|
||||
|
||||
created 1 Aug 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringLengthTrim
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
String txtMsg = ""; // a string for incoming text
|
||||
int lastStringLength = txtMsg.length(); // previous length of the String
|
||||
|
||||
void setup() {
|
||||
// open the serial port:
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// add any incoming characters to the String:
|
||||
while (Serial.available() > 0) {
|
||||
char inChar = Serial.read();
|
||||
txtMsg += inChar;
|
||||
}
|
||||
|
||||
// print the message and a notice if it's changed:
|
||||
if (txtMsg.length() != lastStringLength) {
|
||||
Serial.println(txtMsg);
|
||||
Serial.println(txtMsg.length());
|
||||
// if the String's longer than 140 characters, complain:
|
||||
if (txtMsg.length() < 140) {
|
||||
Serial.println("That's a perfectly acceptable text message");
|
||||
}
|
||||
else {
|
||||
Serial.println("That's too long for a text message.");
|
||||
}
|
||||
// note the length for next time through the loop:
|
||||
lastStringLength = txtMsg.length();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
String length() and trim()
|
||||
|
||||
Examples of how to use length() and trim() in a String
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringLengthTrim
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString length() and trim():");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// here's a String with empty spaces at the end (called white space):
|
||||
String stringOne = "Hello! ";
|
||||
Serial.print(stringOne);
|
||||
Serial.print("<--- end of string. Length: ");
|
||||
Serial.println(stringOne.length());
|
||||
|
||||
// trim the white space off the string:
|
||||
stringOne = stringOne.trim();
|
||||
Serial.print(stringOne);
|
||||
Serial.print("<--- end of trimmed string. Length: ");
|
||||
Serial.println(stringOne.length());
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
String replace()
|
||||
|
||||
Examples of how to replace characters or substrings of a string
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringReplace
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString replace:");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
String stringOne = "<html><head><body>";
|
||||
Serial.println(stringOne);
|
||||
// replace() changes all instances of one substring with another:
|
||||
String stringTwo = stringOne.replace("<", "</");
|
||||
Serial.println(stringTwo);
|
||||
|
||||
// you can also use replace() on single characters:
|
||||
String normalString = "bookkeeper";
|
||||
Serial.println("normal: " + normalString);
|
||||
String leetString = normalString.replace('o', '0');
|
||||
leetString = leetString.replace('e', '3');
|
||||
Serial.println("l33tspeak: " + leetString);
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
String startWith() and endsWith()
|
||||
|
||||
Examples of how to use startsWith() and endsWith() in a String
|
||||
|
||||
created 27 July 2010
|
||||
modified 4 Sep 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringStartsWithEndsWith
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString startsWith() and endsWith():");
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// startsWith() checks to see if a String starts with a particular substring:
|
||||
String stringOne = "HTTP/1.1 200 OK";
|
||||
Serial.println(stringOne);
|
||||
if (stringOne.startsWith("HTTP/1.1")) {
|
||||
Serial.println("Server's using http version 1.1");
|
||||
}
|
||||
|
||||
// you can also look for startsWith() at an offset position in the string:
|
||||
stringOne = "HTTP/1.1 200 OK";
|
||||
if (stringOne.startsWith("200 OK", 9)) {
|
||||
Serial.println("Got an OK from the server");
|
||||
}
|
||||
|
||||
// endsWith() checks to see if a String ends with a particular character:
|
||||
String sensorReading = "sensor = ";
|
||||
sensorReading += analogRead(A0);
|
||||
Serial.print (sensorReading);
|
||||
if (sensorReading.endsWith(0)) {
|
||||
Serial.println(". This reading is divisible by ten");
|
||||
}
|
||||
else {
|
||||
Serial.println(". This reading is not divisible by ten");
|
||||
|
||||
}
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
String substring()
|
||||
|
||||
Examples of how to use substring in a String
|
||||
|
||||
created 27 July 2010
|
||||
by Tom Igoe
|
||||
|
||||
http://arduino.cc/en/Tutorial/StringSubstring
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("\n\nString substring():");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Set up a String:
|
||||
String stringOne = "Content-Type: text/html";
|
||||
Serial.println(stringOne);
|
||||
|
||||
// substring(index) looks for the substring from the index position to the end:
|
||||
if (stringOne.substring(19) == "html") {
|
||||
Serial.println("It's an html file");
|
||||
}
|
||||
// you can also look for a substring in the middle of a string:
|
||||
if (stringOne.substring(14,18) == "text") {
|
||||
Serial.println("It's a text-based file");
|
||||
}
|
||||
|
||||
// do nothing while true:
|
||||
while(true);
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
String to Integer conversion
|
||||
|
||||
Reads a serial input string until it sees a newline, then converts
|
||||
the string to a number if the characters are digits.
|
||||
|
||||
The circuit:
|
||||
No external components needed.
|
||||
|
||||
created 29 Nov 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
String inString = ""; // string to hold input
|
||||
|
||||
void setup() {
|
||||
// Initialize serial communications:
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Read serial input:
|
||||
while (Serial.available() > 0) {
|
||||
int inChar = Serial.read();
|
||||
if (isDigit(inChar)) {
|
||||
// convert the incoming byte to a char
|
||||
// and add it to the string:
|
||||
inString += (char)inChar;
|
||||
}
|
||||
// if you get a newline, print the string,
|
||||
// then the string's value:
|
||||
if (inChar == '\n') {
|
||||
Serial.print("Value:");
|
||||
Serial.println(inString.toInt());
|
||||
Serial.print("String: ");
|
||||
Serial.println(inString);
|
||||
// clear the string for new input:
|
||||
inString = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,230 @@
|
|||
/*
|
||||
Serial RGB controller
|
||||
|
||||
Reads a serial input string looking for three comma-separated
|
||||
integers with a newline at the end. Values should be between
|
||||
0 and 255. The sketch uses those values to set the color
|
||||
of an RGB LED attached to pins 9 - 11.
|
||||
|
||||
The circuit:
|
||||
* Common-anode RGB LED cathodes attached to pins 9 - 11
|
||||
* LED anode connected to pin 13
|
||||
|
||||
To turn on any given channel, set the pin LOW.
|
||||
To turn off, set the pin HIGH. The higher the analogWrite level,
|
||||
the lower the brightness.
|
||||
|
||||
created 29 Nov 2010
|
||||
by Tom Igoe
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
String inString = ""; // string to hold input
|
||||
int currentColor = 0;
|
||||
int red, green, blue = 0;
|
||||
|
||||
void setup() {
|
||||
// Initialize serial communications:
|
||||
Serial.begin(9600);
|
||||
// set LED cathode pins as outputs:
|
||||
pinMode(9, OUTPUT);
|
||||
pinMode(10, OUTPUT);
|
||||
pinMode(11, OUTPUT);
|
||||
// turn on pin 13 to power the LEDs:
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, HIGH);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int inChar;
|
||||
|
||||
// Read serial input:
|
||||
if (Serial.available() > 0) {
|
||||
inChar = Serial.read();
|
||||
}
|
||||
|
||||
if (isDigit(inChar)) {
|
||||
// convert the incoming byte to a char
|
||||
// and add it to the string:
|
||||
inString += (char)inChar;
|
||||
}
|
||||
|
||||
// if you get a comma, convert to a number,
|
||||
// set the appropriate color, and increment
|
||||
// the color counter:
|
||||
if (inChar == ',') {
|
||||
// do something different for each value of currentColor:
|
||||
switch (currentColor) {
|
||||
case 0: // 0 = red
|
||||
red = inString.toInt();
|
||||
// clear the string for new input:
|
||||
inString = "";
|
||||
break;
|
||||
case 1: // 1 = green:
|
||||
green = inString.toInt();
|
||||
// clear the string for new input:
|
||||
inString = "";
|
||||
break;
|
||||
}
|
||||
currentColor++;
|
||||
}
|
||||
// if you get a newline, you know you've got
|
||||
// the last color, i.e. blue:
|
||||
if (inChar == '\n') {
|
||||
blue = inString.toInt();
|
||||
|
||||
// set the levels of the LED.
|
||||
// subtract value from 255 because a higher
|
||||
// analogWrite level means a dimmer LED, since
|
||||
// you're raising the level on the anode:
|
||||
analogWrite(11, 255 - red);
|
||||
analogWrite(9, 255 - green);
|
||||
analogWrite(10, 255 - blue);
|
||||
|
||||
// print the colors:
|
||||
Serial.print("Red: ");
|
||||
Serial.print(red);
|
||||
Serial.print(", Green: ");
|
||||
Serial.print(green);
|
||||
Serial.print(", Blue: ");
|
||||
Serial.println(blue);
|
||||
|
||||
// clear the string for new input:
|
||||
inString = "";
|
||||
// reset the color counter:
|
||||
currentColor = 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Here's a Processing sketch that will draw a color wheel and send a serial
|
||||
string with the color you click on:
|
||||
|
||||
// Subtractive Color Wheel with Serial
|
||||
// Based on a Processing example by Ira Greenberg.
|
||||
// Serial output added by Tom Igoe
|
||||
//
|
||||
// The primaries are red, yellow, and blue. The secondaries are green,
|
||||
// purple, and orange. The tertiaries are yellow-orange, red-orange,
|
||||
// red-purple, blue-purple, blue-green, and yellow-green.
|
||||
//
|
||||
// Create a shade or tint of the subtractive color wheel using
|
||||
// SHADE or TINT parameters.
|
||||
|
||||
// Updated 29 November 2010.
|
||||
|
||||
|
||||
|
||||
import processing.serial.*;
|
||||
|
||||
int segs = 12;
|
||||
int steps = 6;
|
||||
float rotAdjust = TWO_PI / segs / 2;
|
||||
float radius;
|
||||
float segWidth;
|
||||
float interval = TWO_PI / segs;
|
||||
|
||||
Serial myPort;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(127);
|
||||
smooth();
|
||||
ellipseMode(RADIUS);
|
||||
noStroke();
|
||||
// make the diameter 90% of the sketch area
|
||||
radius = min(width, height) * 0.45;
|
||||
segWidth = radius / steps;
|
||||
|
||||
// swap which line is commented out to draw the other version
|
||||
// drawTintWheel();
|
||||
drawShadeWheel();
|
||||
// open the first serial port in your computer's list
|
||||
myPort = new Serial(this, Serial.list()[0], 9600);
|
||||
}
|
||||
|
||||
|
||||
void drawShadeWheel() {
|
||||
for (int j = 0; j < steps; j++) {
|
||||
color[] cols = {
|
||||
color(255-(255/steps)*j, 255-(255/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/1.5)-((255/1.5)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2)-((255/2)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j, 0),
|
||||
color(255-(255/steps)*j, 0, 0),
|
||||
color(255-(255/steps)*j, 0, (255/2)-((255/2)/steps)*j),
|
||||
color(255-(255/steps)*j, 0, 255-(255/steps)*j),
|
||||
color((255/2)-((255/2)/steps)*j, 0, 255-(255/steps)*j),
|
||||
color(0, 0, 255-(255/steps)*j),
|
||||
color(0, 255-(255/steps)*j, (255/2.5)-((255/2.5)/steps)*j),
|
||||
color(0, 255-(255/steps)*j, 0),
|
||||
color((255/2)-((255/2)/steps)*j, 255-(255/steps)*j, 0)
|
||||
};
|
||||
for (int i = 0; i < segs; i++) {
|
||||
fill(cols[i]);
|
||||
arc(width/2, height/2, radius, radius,
|
||||
interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void drawTintWheel() {
|
||||
for (int j = 0; j < steps; j++) {
|
||||
color[] cols = {
|
||||
color((255/steps)*j, (255/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/1.5)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2)/steps)*j, 0),
|
||||
color((255/steps)*j, ((255/2.5)/steps)*j, 0),
|
||||
color((255/steps)*j, 0, 0),
|
||||
color((255/steps)*j, 0, ((255/2)/steps)*j),
|
||||
color((255/steps)*j, 0, (255/steps)*j),
|
||||
color(((255/2)/steps)*j, 0, (255/steps)*j),
|
||||
color(0, 0, (255/steps)*j),
|
||||
color(0, (255/steps)*j, ((255/2.5)/steps)*j),
|
||||
color(0, (255/steps)*j, 0),
|
||||
color(((255/2)/steps)*j, (255/steps)*j, 0)
|
||||
};
|
||||
for (int i = 0; i < segs; i++) {
|
||||
fill(cols[i]);
|
||||
arc(width/2, height/2, radius, radius,
|
||||
interval*i+rotAdjust, interval*(i+1)+rotAdjust);
|
||||
}
|
||||
radius -= segWidth;
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// nothing happens here
|
||||
}
|
||||
|
||||
void mouseReleased() {
|
||||
// get the color of the mouse position's pixel:
|
||||
color targetColor = get(mouseX, mouseY);
|
||||
// get the component values:
|
||||
int r = int(red(targetColor));
|
||||
int g = int(green(targetColor));
|
||||
int b = int(blue(targetColor));
|
||||
// make a comma-separated string:
|
||||
String colorString = r + "," + g + "," + b + "\n";
|
||||
// send it out the serial port:
|
||||
myPort.write(colorString );
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in a new issue