Created
November 4, 2012 16:36
-
-
Save tonussi/4012525 to your computer and use it in GitHub Desktop.
Interface between Arduino and Pd~ (PureData)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#N canvas 108 219 593 441 10; | |
#X msg 19 103 devices; | |
#X obj 122 120 comport 0 9600; | |
#X msg 19 74 close; | |
#X msg 19 26 devicename /dev/ttyACM0; | |
#X obj 146 262 noteout; | |
#X floatatom 216 182 0 0 0 0 - - -; | |
#X floatatom 169 182 0 0 0 0 - - -; | |
#X obj 300 405 print x1; | |
#X obj 32 409 print x2; | |
#X floatatom 102 198 0 0 0 0 - - -; | |
#X obj 145 160 hsl 255 8 0 255 0 0 empty empty Comport_Output -2 -8 | |
0 14 -261682 -1 -1 5080 1; | |
#X obj 124 234 makenote \$1 \$2; | |
#X obj 89 173 int; | |
#X connect 0 0 1 0; | |
#X connect 1 0 10 0; | |
#X connect 2 0 1 0; | |
#X connect 3 0 1 0; | |
#X connect 5 0 11 2; | |
#X connect 6 0 11 1; | |
#X connect 9 0 11 0; | |
#X connect 10 0 12 0; | |
#X connect 11 0 4 0; | |
#X connect 11 0 8 0; | |
#X connect 11 1 4 2; | |
#X connect 11 1 7 0; | |
#X connect 12 0 9 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int sensorPino = A4; | |
int delPino = 9; //pwm | |
int sensorValor = 0; | |
int sensorMax = 2000; | |
int sensorMin = 0; | |
const int nroLeituras = 10; | |
int leituras[nroLeituras]; | |
int indice = 0; | |
int total = 0; | |
int media = 0; | |
void setup() { | |
Serial.begin(9600); | |
/*for (int essaLeitura = 0; essaLeitura < nroLeituras; essaLeitura++) { | |
leituras[essaLeitura] = 0; | |
}*/ | |
pinMode(delPino, OUTPUT); | |
while (millis() < 5000) { //calibration | |
sensorValor = analogRead(sensorPino); | |
if (sensorValor > sensorMax) { | |
sensorMax = sensorValor; | |
} | |
if (sensorValor < sensorMin) { | |
sensorMin = sensorValor; | |
} | |
} | |
} | |
void loop() { | |
sensorValor = analogRead(sensorPino); | |
sensorValor = map(sensorValor, sensorMin, sensorMax, 0, 255); | |
sensorValor = constrain(sensorValor, 0, 255); | |
analogWrite(delPino, sensorValor); | |
/* | |
total = total - leituras[indice]; | |
leituras[indice] = analogRead(sensorPino); | |
total = total + leituras[indice]; | |
indice = indice + 1; | |
if (indice >= nroLeituras) { | |
indice = 0; | |
} | |
media = total/nroLeituras; | |
*/ | |
Serial.write(sensorValor); | |
//delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment