Sunteți pe pagina 1din 81

LUPULEȚ ION LUPULEȚ MARINELA DENISA

BOGDAN CONSTANTIN

APLICAȚII ALE MATEMATICII SI INFORMATICII IN


ELECTRONICĂ SI AUTOMATICĂ

Aplicatii în
LIMBAJUL ARDUINO

2019

1
PREFATA
Cartea prezinta PRINCIPIILE MATEMATICE SI INFORMATICE IN PROGRAMAREA automatelor si
dispozitivelor electronice de la centrale de apartament , sisteme de securitate si roboti
industriali . Rezolvarea tipurilor de matematice folosind acest mediu de programare nou este
o prioritate de la listare si pana la functionare dar si in industria ultramoderna robotizata .
Este o continuare a primului volum introductiv de arduino pentru a completa toate tipurile
de probleme cerute la examen .
Autorii
Prof . LUPULEȚ ION , C.T NR 2 TG JIU
Prof . LUPULEȚ MARINELA DENISA , C.N TUDOR ARGHEZI , TG CARBUNESTI
Prof . Dr. BOGDAN CONSTANTIN , DIRECTOR ADJUNCT C.T ION MINCU , TG JIU

Referenti stiintifici
Prof.Dr Patrascoiu Nicolae , Universitatea din Petrosani

G:\aldruino\arduinoSOFTULBUN\examples\
1)citirea de pe un potentiometru si afisare pe ecran

2
void setup() {
Serial.begin(9600);
}

3
void loop() {

// CITESC DE PE INTRAREA analog pin 0:


int sensorValue = analogRead(A0);
// AFISEZ VALOAREA DE PE PIN ZERO :
Serial.println(sensorValue);
delay(1);
}

2) stingrea si aprinderea unui led

4
void setup() {

5
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

//se repeta stingerea si aprinderea unui led


void loop() {
digitalWrite(LED_BUILTIN, HIGH); // se aprinde ledul
delay(1000); // sta aprinns 1000 de milisecunde
digitalWrite(LED_BUILTIN, LOW); // se stinge
delay(1000); // sta stins 1000 de milisecunde
}

3) citirea unui distribuitor de semnal

6
int pushButton = 2;

void setup() {

Serial.begin(9600);

pinMode(pushButton, INPUT);
}

void loop() {

int buttonState = digitalRead(pushButton);

Serial.println(buttonState);
delay(1);
}

4)

7
8
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
9
void setup() {

Serial.begin(9600);
}

10
void loop() {

int sensorValue = analogRead(A0);

float voltage = sensorValue * (5.0 / 1023.0);

Serial.println(voltage);
}

CAPITOLUL II
21) CLIPIREA UNUI LED DUPA VALORILE UNEI FUNCTII

11
const int ledPin = LED_BUILTIN;// the number of the LED pin

12
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

13
14
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:
15
digitalWrite(ledPin, LOW);
}
}

23)filtrarea stingerii uni led de la un buton

16
const int buttonPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
17
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
}

24) Demonstram ca folosim INPUT_PULLUP cu functia pinMode().

18
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
19
pinMode(13, OUTPUT);
}
void loop() {
int sensorVal = digitalRead(2);
Serial.println(sensorVal);
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
}

25)
Numarare de cate ori apasam un buton

20
const int buttonPin = 2;
const int ledPin = 13;

21
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);

22
26)
Folosirea unei claviaturi audio pentru un difuzor

Fisierul de se importa numit pitches.h

Se va mentiona in sursa intre apostrosfe calea ctre acest fisier


/*************************************************
* Public Constants

23
*************************************************/

#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

24
#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

25
#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

26
#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
27) Folosind tonuri cu mai multe difuzoare folosind functia tone()

Fisierul pitches.h
/*************************************************
* Public Constants
*************************************************/
28
#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

29
#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

30
#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

31
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() {
32
}

27)

33
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
34
35
36
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

37
CAPITOLUL III
29)

const int analogInPin = A0;


const int analogOutPin = 9;
int sensorValue = 0;
int outputValue = 0;

38
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

delay(2);
}

30)

39
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);
}

31) Pornire si oprire leduri

40
const int lowestPin = 2;
const int highestPin = 13;
void setup() {
for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
pinMode(thisPin, OUTPUT);

41
}
}
void loop() {
for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
for (int brightness = 0; brightness < 255; brightness++) {
analogWrite(thisPin, brightness);
delay(2);
}
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(thisPin, brightness);
delay(2);
}
delay(100);
}
}

32)

42
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);
}

43
void loop() {
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
sensorValue = constrain(sensorValue, 0, 255);
analogWrite(ledPin, sensorValue);
}

33)

44
int ledPin = 9; // LED connected to digital pin 9
void setup() {
}
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);
}
}

34) Citirea mai multor intrari

45
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
int inputPin = A0;

46
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 III

35)
Demonstrarea functiilor externe
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("ASCII Table ~ Character Map");
}
int thisByte = 33;

47
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)

48
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);

println("Available serial ports:");

49
println(Serial.list());
select the port
}
void draw() {
for (int i = 0; i < 256; i++) {
stroke(i);
line(i, 0, i, 150);
}
as
port.write(mouseX);
}

37)

50
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;
51
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);

if (xPos >= width) {


xPos = 0;
background(0);
} else {
// increment the horizontal position:
xPos++;
}
}
void serialEvent (Serial myPort) {

String inString = myPort.readStringUntil('\n');


if (inString != null) {
inString = trim(inString);
inByte = float(inString);
println(inByte);
inByte = map(inByte, 0, 1023, 0, height);
}
}

52
38) maxim/minim pentru valorile unui senzor

const int sensorPin = A0;


const int ledPin = 9;

53
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);
}

39) iesire (PWM pin) cu LED.

54
int ledPin = 9; // LED connected to digital pin 9
void setup() {

}
void loop() {

55
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

56
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) {

57
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

58
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;
}
}

const int ledPin = 9; // the pin that the LED is attached to


void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);

59
}
void loop() {
byte brightness;
if (Serial.available()) {
brightness = Serial.read();
analogWrite(ledPin, brightness);
}
}

43) Trimiterea unui mesaj prin MIDI ul unui instrument muzical

60
void setup() {
Serial.begin(31250);
}
void loop() {
for (int note = 0x1E; note < 0x5A; note ++) {
noteOn(0x90, note, 0x45);
delay(100);
noteOn(0x90, note, 0x00);
delay(100);
}
}

void noteOn(int cmd, int pitch, int velocity) {


Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}

61
44)
Stingerea unui LED daca trimitem date spre placa Arduino si procesare cu Max/MSP.

62
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

63
int firstSensor = 0;
int secondSensor = 0;
int thirdSensor = 0;
int inByte = 0;

64
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

65
int firstSensor = 0;
int secondSensor = 0;
int thirdSensor = 0;
int inByte = 0;

66
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.

67
const int redPin = A0;
const int greenPin = A1;
const int bluePin = A2;

68
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)

69
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

70
void setup() {
Serial.begin(9600);
}

71
void loop() {
Serial.println(analogRead(A0));
delay(2);
}

53)
Mesaje MIDI trimise catre un port serial

72
void setup() {
Serial.begin(31250);
}
void loop() {
for (int note = 0x1E; note < 0x5A; note ++) {
delay(100);
noteOn(0x90, note, 0x00);
delay(100);
}
}

void noteOn(int cmd, int pitch, int velocity) {


Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}

73
55)
Oprirea unui led LED pornirea si oprirea de catre Arduino si procesarea de catre Max/MSP.

74
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);
}
}
}

56)
Apelari multiple la proceduri

75
int firstSensor = 0;
int secondSensor = 0;
int thirdSensor = 0;

76
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)

77
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);
78
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.

79
/*
ADXL3xx

Reads an Analog Devices ADXL3xx accelerometer and communicates the


acceleration to the computer. The pins used are designed to be
easily
compatible with the breakout boards from SparkFun, available from:
http://www.sparkfun.com/commerce/categories.php?c=80

The circuit:
- analog 0: accelerometer self test
- analog 1: z-axis
- analog 2: y-axis
- analog 3: x-axis
- analog 4: ground
- analog 5: vcc

created 2 Jul 2008


by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/ADXL3xx
*/

// these constants describe the pins. They won't change:


const int groundpin = 18; // analog input pin 4 --
ground
const int powerpin = 19; // analog input pin 5 -- volt-
age
80
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis mod-
els)

void setup() {
// initialize the serial communications:
Serial.begin(9600);

// Provide ground and power by using the analog inputs as normal


digital pins.
// This makes it possible to directly connect the breakout board
to the
// Arduino. If you use the normal 5V and GND pins on the Arduino,
// you can remove these lines.
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}

void loop() {
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
// delay before next reading:
delay(100);
}

81

S-ar putea să vă placă și