Curs Ultim Ardruino
Curs Ultim Ardruino
void loop() {
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
Serial.println(buttonState);
delay(1);
}
4)
int led = 9;
int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}
5)
CITIREA UNEI INTRARI DE TENSIUNE CA VOLTAJ SI AFISARE
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(voltage);
}
CAPITOLUL II
21) CLIPIREA UNUI LED DUPA VALORILE UNEI FUNCTII
const int ledPin = LED_BUILTIN;// the number of the LED pin
int ledState = LOW;
unsigned long previousMillis = 0;
// constants won't change:
const long interval = 1000;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
22)
Buton de stingere si aprindere a unui led
const int buttonPin = 2;
const int ledPin = 13;
// variables will change:
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on: digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
25)
Numarare de cate ori apasam un buton
const int buttonPin = 2;
const int ledPin = 13;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
if (buttonPushCounter % 4 == 0) {
digitalWrite(
}
ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
26)
Folosirea unei claviaturi audio pentru un difuzor
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#include "G:\aldruino\arduinoSOFTULBUN\examples\02.Digital\toneKeyboard\pitches.h"
const int threshold = 10; // minimum reading of the sensors that generates a note
int notes[] = {
NOTE_A4, NOTE_B4, NOTE_C3
};
void setup() {
}
void loop() {
for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
int sensorReading = analogRead(thisSensor);
if (sensorReading > threshold) {
tone(8, notes[thisSensor], 20);
}
}
}
27) Folosind tonuri cu mai multe difuzoare folosind functia tone()
Fisierul pitches.h
/*************************************************
* Public Constants
*************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
Se precizeaza de unde se importa fisierul G:\aldruino\arduinoSOFTULBUN\examples\02.Digital\
toneMelody\pitches.h
#include "G:\aldruino\arduinoSOFTULBUN\examples\02.Digital\toneMelody\pitches.h"
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
}
void loop() {
}
27)
void setup() {
}
void loop() {
noTone(8);
tone(6, 440, 200);
delay(200);
noTone(6);
tone(7, 494, 500);
delay(500);
noTone(7);
tone(8, 523, 300);
delay(300);
}
28)
Modificarea tonurilor dupa cum pot sa modific analog semnalul
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorReading = analogRead(A0);
Serial.println(sensorReading);
int thisPitch = map(sensorReading, 400, 1000, 120, 1500);
tone(9, thisPitch, 10);
delay(1); // delay in between reads for stability
}
Salvarea fisierelor
CAPITOLUL III
29)
delay(2);
}
30)
int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}
32)
const int sensorPin = A0;
const int ledPin = 9;
int sensorValue = 0;
int sensorMin = 1023;
int sensorMax = 0;
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
digitalWrite(13, LOW);
}
void loop() {
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
sensorValue = constrain(sensorValue, 0, 255);
analogWrite(ledPin, sensorValue);
}
33)
Capitolul III
35)
Demonstrarea functiilor externe
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("ASCII Table ~ Character Map");
}
int thisByte = 33;
void loop() {
Serial.write(thisByte);
Serial.print(", dec: ");
Serial.print(thisByte)
Serial.print(", hex: ");
Serial.print(thisByte, HEX);
Serial.print(", oct: ");
Serial.print(thisByte, OCT);
Serial.print(", bin: ");
Serial.println(thisByte, BIN);
if (thisByte == 126) {
while (true) {
continue;
}
}
thisByte++;
}
36)
const int ledPin = 9; // the pin that the LED is attached to
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
byte brightness;
if (Serial.available()) {
brightness = Serial.read();
analogWrite(ledPin, brightness);
}
}
import processing.serial.*;
Serial port;
void setup() {
size(256, 150);
37)
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}
void loop() {
// send the value of analog input 0:
Serial.println(analogRead(A0));
delay(2);
}
import processing.serial.*;
Serial myPort;
int xPos = 1;
float inByte = 0;
void setup () {
size(400, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
:
myPort.bufferUntil('\n');
background(0);
}
void draw () {
);
line(xPos, height, xPos, height - inByte);
int sensorValue = 0;
int sensorMin = 1023;
int sensorMax = 0;
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);
digitalWrite(13, LOW);
}
void loop() {
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
sensorValue = constrain(sensorValue, 0, 255);
analogWrite(ledPin, sensorValue);
}
}
void loop() {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(ledPin, fadeValue);
delay(30);
}
}
40) multiple citiri de la o intrare analoaga
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
int inputPin = A0;
void setup() {
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
total = total - readings[readIndex];
readings[readIndex] = analogRead(inputPin);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
Serial.println(average);
delay(1);
}
CAPITOLUL IV
G:\aldruino\arduinoSOFTULBUN\examples\04.Communication
41) miscarea mouseului schimba lumina unui led
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
int inputPin = A0;
void setup() {
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
44)
Stingerea unui LED daca trimitem date spre placa Arduino si procesare cu Max/MSP.
PRIMUL PROGRAM DE RULAT
const int ledPin = 13;
int incomingByte;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
}
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
}
}
}
45x)
TRIMITEREA mai multor variabile care sa apleze o procedura
int firstSensor = 0;
int secondSensor = 0;
int thirdSensor = 0;
int inByte = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
pinMode(2, INPUT);
establishContact();
}
void loop() {
if (Serial.available() > 0) {
inByte = Serial.read();
firstSensor = analogRead(A0) / 4;
delay(10);
secondSensor = analogRead(1) / 4;
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
Serial.write(firstSensor);
Serial.write(secondSensor);
Serial.write(thirdSensor);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
46 x)
Trimiterea de mesaje ascii prin mai multe proceduri
int firstSensor = 0;
int secondSensor = 0;
int thirdSensor = 0;
int inByte = 0;
void setup() {
// start serial port at 9600 bps and wait for port to open:
Serial.begin(9600);
while (!Serial) {
;
}
pinMode(2, INPUT); // digital sensor is on digital pin 2
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
if (Serial.available() > 0) {
inByte = Serial.read();
firstSensor = analogRead(A0);
secondSensor = analogRead(A1);
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
Serial.print(firstSensor);
Serial.print(",");
Serial.print(secondSensor);
Serial.print(",");
Serial.println(thirdSensor);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("0,0,0"); // send an initial string
delay(300);
}
}
47x)
Trimiterea mai multor variabile catre Arduino si catre calculator dar procesate de Max/MSP.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(analogRead(redPin));
Serial.print(",");
Serial.print(analogRead(greenPin));
Serial.print(",");
Serial.println(analogRead(bluePin));
}
CAPITOLUL V
51)
const int ledPin = 9; // the pin that the LED is attached to
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
byte brightness;
if (Serial.available()) {
brightness = Serial.read();
analogWrite(ledPin, brightness);
}
}
52)
TRIMITERE de date si grafice pentru procesare
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(2);
}
53)
Mesaje MIDI trimise catre un port serial
void setup() {
Serial.begin(31250);
}
void loop() {
for (int note = 0x1E; note < 0x5A; note ++) {
delay(100);
noteOn(0x90, note, 0x00);
delay(100);
}
}
56)
Apelari multiple la proceduri
int firstSensor = 0;
int secondSensor = 0;
int thirdSensor = 0;
int inByte = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
pinMode(2, INPUT);
establishContact();
}
void loop() {
if (Serial.available() > 0) {
inByte = Serial.read();
firstSensor = analogRead(A0) / 4;
delay(10);
secondSensor = analogRead(1) / 4;
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
Serial.write(firstSensor);
Serial.write(secondSensor);
Serial.write(thirdSensor);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
57)
Transmitere de parametrii prin referinta(rezultat)
int firstSensor = 0;
int secondSensor = 0;
int thirdSensor = 0;
int inByte = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
pinMode(2, INPUT);
establishContact();
}
void loop() {
if (Serial.available() > 0) {
inByte = Serial.read();
firstSensor = analogRead(A0);
secondSensor = analogRead(A1);
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
Serial.print(firstSensor);
Serial.print(",");
Serial.print(secondSensor);
Serial.print(",");
Serial.println(thirdSensor);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("0,0,0");
delay(300);
}
}
Capitolul VI
61)
Read an ADXL3xx accelerometer.
const int groundpin = 18;
const int powerpin = 19;
const int xpin = A3;
const int ypin = A2;
const int zpin = A1;
void setup() {
Serial.begin(9600);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}
void loop() {
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print(analogRead(ypin));
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
delay(100);
}
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorReading = analogRead(knockSensor);
ledState = !ledState;
digitalWrite(ledPin, ledState);
Serial.println("Knock!");
}
delay(100);
}
64)
Two-axis accelerometer.
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
void setup() {
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}
void loop() {
Serial.print(accelerationX);
Serial.print("\t");
Serial.print(accelerationY);
Serial.println();
delay(100);
}
64)
Detecting objects with an ultrasonic range finder.
const int pingPin = 7;
void setup() {
Serial.begin(9600);
}
void loop() {
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
return microseconds / 29 / 2;
}
Capitolul VII
71)
How to make an LED bar graph.
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
void setup() {
void loop() {
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
72)
How to control an 8x8 matrix of LEDs.
const int row[8] = {
2, 7, 19, 5, 13, 18, 12, 16
};
int pixels[8][8];
int x = 5;
int y = 5;
void setup() {
digitalWrite(col[thisPin], HIGH);
}
void loop() {
readSensors();
refreshScreen();
}
void readSensors() {
pixels[x][y] = HIGH;
pixels[x][y] = LOW;
}
void refreshScreen() {
digitalWrite(row[thisRow], HIGH);
digitalWrite(col[thisCol], thisPixel);
if (thisPixel == LOW) {
digitalWrite(col[thisCol], HIGH);
}
}
digitalWrite(row[thisRow], LOW);
}
}
Capitolul VIII
81)// Add Strings together in a variety of ways.
Serial.begin(9600);
while (!Serial) {
;
}
stringOne = String("You added ");
stringTwo = String("this string");
stringThree = String();
void loop() {
while (true);
}
82)
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
void loop() {
if (Serial.available() > 0) {
int thisChar = Serial.read();
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 white-
space");
}
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)");
}
Serial.println();
Serial.println("Give me another byte:");
Serial.println();
}
}
83)
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("\n\nAppending to a String:");
Serial.println();
}
void loop() {
Serial.println(stringOne); // prints "Sensor "
stringOne += stringTwo;
Serial.println(stringOne); // prints "Sensor value"
stringOne += 'A';
Serial.println(stringOne); // prints "Sensor value for input A"
stringOne += 0;
Serial.println(stringOne); // prints "Sensor value for input A0"
stringOne += analogRead(A0);
Serial.println(stringOne); // prints "Sensor value for input A0: 456" or whatever analogRead(A0) is
stringOne += 123456789;
Serial.println(stringOne); // prints "A long integer: 123456789"
stringTwo.concat(millis());
Serial.println(stringTwo); // prints "The millis(): 43534" or whatever the value of the millis() is
while (true);
}
84)
// Change the case of a String.
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
void loop() {
85)
// Get/set the value of a specific character in a String.
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
void loop() {
String message = "Most significant digit of the sensor reading is: ";
Serial.println(message + mostSignificantDigit);
Serial.println();
reportString.setCharAt(13, '=');
Serial.println(reportString);
while (true);
}
86)
// Compare Strings alphabetically.
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
stringOne = String("this");
stringTwo = String("that");
Serial.println("\n\nComparing Strings:");
Serial.println();
void loop() {
if (stringOne == "this") {
Serial.println("StringOne == \"this\"");
}
if (stringOne != stringTwo) {
Serial.println(stringOne + " =! " + stringTwo);
}
stringOne = "This";
stringTwo = "this";
if (stringOne != stringTwo) {
Serial.println(stringOne + " =! " + stringTwo);
}
if (stringOne.equals(stringTwo)) {
Serial.println(stringOne + " equals " + stringTwo);
} else {
Serial.println(stringOne + " does not equal " + stringTwo);
}
if (stringOne.equalsIgnoreCase(stringTwo)) {
Serial.println(stringOne + " equals (ignoring case) " + stringTwo);
} else {
Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);
}
stringOne = "1";
int numberOne = 1;
if (stringOne.toInt() == numberOne) {
Serial.println(stringOne + " = " + numberOne);
}
stringOne = "2";
stringTwo = "1";
if (stringOne >= stringTwo) {
Serial.println(stringOne + " >= " + stringTwo);
}
stringOne = String("Brown");
if (stringOne < "Charles") {
Serial.println(stringOne + " < Charles");
}
stringOne = "Cucumber";
stringTwo = "Cucuracha";
if (stringOne.compareTo(stringTwo) < 0) {
Serial.println(stringOne + " comes before " + stringTwo);
} else {
Serial.println(stringOne + " comes after " + stringTwo);
}
delay(10000);
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);
}
}
}
-----------------------------------------
87)
//How to initialize String objects.
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("\n\nString Constructors:");
Serial.println();
}
void loop() {
stringOne = String('a');
Serial.println(stringOne);
Serial.println(stringOne);
stringOne = String(13);
Serial.println(stringOne);
Serial.println(stringOne);
Serial.println(stringOne);
Serial.println(stringOne);
while (true);
88)
//Look for the first/last instance of a character in a String.
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
void loop() {
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);
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("<LI>", firstListItem + 1);
Serial.println("The index of the second list tag in the string " + stringOne + " is " + secondListItem);
89)
// Examples of how to use length() in a String.
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("\n\nString length():");
Serial.println();
}
void loop() {
if (txtMsg.length() != lastStringLength) {
Serial.println(txtMsg);
Serial.println(txtMsg.length());
lastStringLength = txtMsg.length();
}
}
90)
// Get and trim the length of a String.
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
void loop() {
stringOne.trim();
Serial.print(stringOne);
Serial.print("<--- end of trimmed string. Length: ");
Serial.println(stringOne.length());
while (true);
}
91)
// Replace individual characters in a String.
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("\n\nString replace:\n");
Serial.println();
}
void loop() {
String stringOne = "<html><head><body>";
Serial.println(stringOne);
stringTwo.replace("<", "</");
while (true);
}
93)
// Check which characters/substrings a given String starts or ends with.
/*
String startWith() and endsWith()
http://www.arduino.cc/en/Tutorial/StringStartsWithEndsWith
*/
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// send an intro:
Serial.println("\n\nString startsWith() and endsWith():");
Serial.println();
}
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");
}
95)
//Look for "phrases" within a given String.
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
// send an intro:
Serial.println("\n\nString substring():");
Serial.println();
}
void loop() {
String stringOne = "Content-Type: text/html";
Serial.println(stringOne);
if (stringOne.substring(19) == "html") {
Serial.println("It's an html file");
}
while (true);
}
96)
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("\n\nString toInt():");
Serial.println();
}
void loop() {
inString += (char)inChar;
}
if (inChar == '\n') {
Serial.print("Value:");
Serial.println(inString.toInt());
Serial.print("String: ");
Serial.println(inString);
inString = "";
}
}
}
Capitolul 10)
101)
int switchstate = 0;
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop() {
switchstate = digitalRead(2);
if (switchstate == LOW) {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
else {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250);
}
}
102)
void setup() {
Serial.begin(9600);
void loop() {
103)
void setup() {
Serial.begin(9600);
void loop() {
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
int redSensorValue = 0;
int greenSensorValue = 0;
int blueSensorValue = 0;
void setup() {
Serial.begin(9600);
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}
void loop() {
redSensorValue = analogRead(redSensorPin);
delay(5);
greenSensorValue = analogRead(greenSensorPin);
delay(5);
blueSensorValue = analogRead(blueSensorPin);
redValue = redSensorValue / 4;
greenValue = greenSensorValue / 4;
blueValue = blueSensorValue / 4;
analogWrite(redLEDPin, redValue);
analogWrite(greenLEDPin, greenValue);
analogWrite(blueLEDPin, blueValue);
}
105)
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9);
Serial.begin(9600);
}
void loop() {
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
myServo.write(angle);
delay(15);
}
106)
int sensorValue;
int sensorHigh = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, LOW);
}
void loop() {
sensorValue = analogRead(A0);
delay(10);
}
107)
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(keyVal);
if (keyVal == 1023) {
tone(8, notes[0]);
} else if (keyVal >= 990 && keyVal <= 1010) {
tone(8, notes[1]);
} else if (keyVal >= 505 && keyVal <= 515) {
tone(8, notes[2]);
} else if (keyVal >= 5 && keyVal <= 10) {
tone(8, notes[3]);
} else {
noTone(8);
}
}
107)
unsigned long previousTime = 0; // store the last time an LED was updated
int switchState = 0; // the current switch state
int prevSwitchState = 0; // the previous switch state
int led = 2; // a variable to refer to the LEDs
void setup() {
pinMode(switchPin, INPUT);
}
void loop() {
unsigned long currentTime = millis();
previousTime = currentTime;
digitalWrite(led, HIGH);
led++;
if (led == 7) {
}
}
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState) {
led = 2;
previousTime = currentTime;
}
prevSwitchState = switchState;
}
108)
int switchState = 0;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
}
void loop() {
switchState = digitalRead(switchPin);
if (switchState == HIGH) {
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
}
}
109)
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;
void setup() {
// initialize the inputs and outputs
pinMode(directionSwitchPin, INPUT);
pinMode(onOffSwitchStateSwitchPin, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
}
void loop() {
onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
delay(1);
directionSwitchState = digitalRead(directionSwitchPin);
motorSpeed = analogRead(potPin) / 4;
if (onOffSwitchState != previousOnOffSwitchState) {
if (onOffSwitchState == HIGH) {
motorEnabled = !motorEnabled;
}
}
if (directionSwitchState != previousDirectionSwitchState) {
if (directionSwitchState == HIGH) {
motorDirection = !motorDirection;
}
}
if (motorDirection == 1) {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
} else {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
}
if (motorEnabled == 1) {
analogWrite(enablePin, motorSpeed);
} else {
analogWrite(enablePin, 0);
}
previousDirectionSwitchState = directionSwitchState;
previousOnOffSwitchState = onOffSwitchState;
}
#include <LiquidCrystal.h>
int switchState = 0;
int prevSwitchState = 0;
int reply;
void setup() {
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
lcd.print("Ask the");
lcd.setCursor(0, 1);
lcd.print("Crystal Ball!");
}
void loop() {
switchState = digitalRead(switchPin);
if (switchState != prevSwitchState) {
if (switchState == LOW) {
reply = random(8);
lcd.clear();
lcd.setCursor(0, 0);
lcd.setCursor(0, 1);
switch (reply) {
case 0:
lcd.print("Yes");
break;
case 1:
lcd.print("Most likely");
break;
case 2:
lcd.print("Certainly");
break;
case 3:
lcd.print("Outlook good");
break;
case 4:
lcd.print("Unsure");
break;
case 5:
lcd.print("Ask again");
break;
case 6:
lcd.print("Doubtful");
break;
case 7:
lcd.print("No");
break;
}
}
}
prevSwitchState = switchState;
}
111)
#include <Servo.h>
Servo myServo;
int knockVal;
int switchVal;
int numberOfKnocks = 0;
void setup() {
myServo.attach(9);
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600);
digitalWrite(greenLed, HIGH);
myServo.write(0);
void loop() {
if (locked == false) {
switchVal = digitalRead(switchPin);
if (switchVal == HIGH) {
locked = true;
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
myServo.write(90);
delay(1000);
}
}
if (locked == true) {
knockVal = analogRead(piezo);
if (checkForKnock(knockVal) == true) {
numberOfKnocks++;
}
Serial.print(3 - numberOfKnocks);
Serial.println(" more knocks to go");
}
if (numberOfKnocks >= 3) {
locked = false;
myServo.write(0);
delay(20);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
Serial.println("the box is unlocked!");
numberOfKnocks = 0;
}
}
}
digitalWrite(yellowLed, HIGH);
delay(50);
digitalWrite(yellowLed, LOW);
else {
return false;
}
}
112)
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write(analogRead(A0) / 4);
delay(1);
}
112)
void setup() {
pinMode(optoPin, OUTPUT);
}
void loop() {
digitalWrite(optoPin, HIGH);
delay(15);
digitalWrite(optoPin, LOW);
delay(21000);
}
SURSA ARDRUINO
#include "Arduino.h"
#undef SERIAL
#if defined(ARDUINO_ARCH_AVR)
#endif
#ifndef ARDUINO_HOODLOADER2
#ifdef USE_OLD_STYLE_WIRING
#define PIN_MOSI 11
#define PIN_MISO 12
#define PIN_SCK 13
#endif
#else
#define RESET 4
#define LED_HB 7
#define LED_ERR 6
#define LED_PMODE 5
#endif
#ifndef PIN_MOSI
#define PIN_MOSI MOSI
#endif
#ifndef PIN_MISO
#define PIN_MISO MISO
#endif
#ifndef PIN_SCK
#define PIN_SCK SCK
#endif
#ifdef SERIAL_PORT_USBVIRTUAL
#define SERIAL SERIAL_PORT_USBVIRTUAL
#else
#define SERIAL Serial
#endif
#define HWVER 2
#define SWMAJ 1
#define SWMIN 18
// STK Definitions
#define STK_OK 0x10
#define STK_FAILED 0x11
#define STK_UNKNOWN 0x12
#define STK_INSYNC 0x14
#define STK_NOSYNC 0x15
#define CRC_EOP 0x20 //ok it is a space...
#ifdef USE_HARDWARE_SPI
#include "SPI.h"
#else
class SPISettings {
public:
// clock is in Hz
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) : clock(clock) {
(void) bitOrder;
(void) dataMode;
};
private:
uint32_t clock;
class BitBangedSPI {
public:
void begin() {
digitalWrite(PIN_SCK, LOW);
digitalWrite(PIN_MOSI, LOW);
pinMode(PIN_SCK, OUTPUT);
pinMode(PIN_MOSI, OUTPUT);
pinMode(PIN_MISO, INPUT);
}
void end() {}
private:
unsigned long pulseWidth; // in microseconds
};
static BitBangedSPI SPI;
#endif
void setup() {
SERIAL.begin(BAUDRATE);
pinMode(LED_PMODE, OUTPUT);
pulse(LED_PMODE, 2);
pinMode(LED_ERR, OUTPUT);
pulse(LED_ERR, 2);
pinMode(LED_HB, OUTPUT);
pulse(LED_HB, 2);
int error = 0;
int pmode = 0;
// address for reading and writing, set by 'U' command
unsigned int here;
uint8_t buff[256]; // global block storage
parameter param;
void loop(void) {
if (pmode) {
digitalWrite(LED_PMODE, HIGH);
} else {
digitalWrite(LED_PMODE, LOW);
}
if (error) {
digitalWrite(LED_ERR, HIGH);
} else {
digitalWrite(LED_ERR, LOW);
}
heartbeat();
if (SERIAL.available()) {
avrisp();
}
}
uint8_t getch() {
while (!SERIAL.available());
return SERIAL.read();
}
void fill(int n) {
for (int x = 0; x < n; x++) {
buff[x] = getch();
}
}
#define PTIME 30
void pulse(int pin, int times) {
do {
digitalWrite(pin, HIGH);
delay(PTIME);
digitalWrite(pin, LOW);
delay(PTIME);
} while (times--);
}
void empty_reply() {
if (CRC_EOP == getch()) {
SERIAL.print((char)STK_INSYNC);
SERIAL.print((char)STK_OK);
} else {
error++;
SERIAL.print((char)STK_NOSYNC);
}
}
void breply(uint8_t b) {
if (CRC_EOP == getch()) {
SERIAL.print((char)STK_INSYNC);
SERIAL.print((char)b);
SERIAL.print((char)STK_OK);
} else {
error++;
SERIAL.print((char)STK_NOSYNC);
}
}
void get_version(uint8_t c) {
switch (c) {
case 0x80:
breply(HWVER);
break;
case 0x81:
breply(SWMAJ);
break;
case 0x82:
breply(SWMIN);
break;
case 0x93:
breply('S'); // serial programmer
break;
default:
breply(0);
}
}
void set_parameters() {
param.devicecode = buff[0];
param.revision = buff[1];
param.progtype = buff[2];
param.parmode = buff[3];
param.polling = buff[4];
param.selftimed = buff[5];
param.lockbytes = buff[6];
param.fusebytes = buff[7];
param.flashpoll = buff[8];
param.eeprompoll = beget16(&buff[10]);
param.pagesize = beget16(&buff[12]);
param.eepromsize = beget16(&buff[14]);
void start_pmode() {
reset_target(true);
pinMode(RESET, OUTPUT);
SPI.begin();
SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0));
digitalWrite(PIN_SCK, LOW);
delay(20); // discharge PIN_SCK, value arbitrarily chosen
reset_target(false);
delayMicroseconds(100);
reset_target(true);
delay(50); // datasheet: must be > 20 msec
spi_transaction(0xAC, 0x53, 0x00, 0x00);
pmode = 1;
}
void end_pmode() {
SPI.end();
// We're about to take the target out of reset so configure SPI pins as input
pinMode(PIN_MOSI, INPUT);
pinMode(PIN_SCK, INPUT);
reset_target(false);
pinMode(RESET, INPUT);
pmode = 0;
}
void universal() {
uint8_t ch;
fill(4);
ch = spi_transaction(buff[0], buff[1], buff[2], buff[3]);
breply(ch);
}
commit(page);
return STK_OK;
}
void program_page() {
char result = (char) STK_FAILED;
unsigned int length = 256 * getch();
length += getch();
char memtype = getch();
// flash memory @here, (length) bytes
if (memtype == 'F') {
write_flash(length);
return;
}
if (memtype == 'E') {
result = (char)write_eeprom(length);
if (CRC_EOP == getch()) {
SERIAL.print((char) STK_INSYNC);
SERIAL.print(result);
} else {
error++;
SERIAL.print((char) STK_NOSYNC);
}
return;
}
SERIAL.print((char)STK_FAILED);
return;
}
void read_page() {
char result = (char)STK_FAILED;
int length = 256 * getch();
length += getch();
char memtype = getch();
if (CRC_EOP != getch()) {
error++;
SERIAL.print((char) STK_NOSYNC);
return;
}
SERIAL.print((char) STK_INSYNC);
if (memtype == 'F') result = flash_read_page(length);
if (memtype == 'E') result = eeprom_read_page(length);
SERIAL.print(result);
}
void read_signature() {
if (CRC_EOP != getch()) {
error++;
SERIAL.print((char) STK_NOSYNC);
return;
}
SERIAL.print((char) STK_INSYNC);
uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00);
SERIAL.print((char) high);
uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00);
SERIAL.print((char) middle);
uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00);
SERIAL.print((char) low);
SERIAL.print((char) STK_OK);
}
void avrisp() {
uint8_t ch = getch();
switch (ch) {
case '0': // signon
error = 0;
empty_reply();
break;
case '1':
if (getch() == CRC_EOP) {
SERIAL.print((char) STK_INSYNC);
SERIAL.print("AVR ISP");
SERIAL.print((char) STK_OK);
}
else {
error++;
SERIAL.print((char) STK_NOSYNC);
}
break;
case 'A':
get_version(getch());
break;
case 'B':
fill(20);
set_parameters();
empty_reply();
break;
case 'E':
fill(5);
empty_reply();
break;
case 'P':
if (!pmode)
start_pmode();
empty_reply();
break;
case 'U':
here = getch();
here += 256 * getch();
empty_reply();
break;
case 0x60:
getch();
getch();
empty_reply();
break;
case 0x61:
getch();
empty_reply();
break;
case 0x64:
program_page();
break;
case 0x74:
read_page();
break;
case 'V':
universal();
break;
case 'Q': //0x51
error = 0;
end_pmode();
empty_reply();
break;
case CRC_EOP:
error++;
SERIAL.print((char) STK_NOSYNC);
break;
default:
error++;
if (CRC_EOP == getch())
SERIAL.print((char)STK_UNKNOWN);
else
SERIAL.print((char)STK_NOSYNC);
}
}