Imágenes
Códigos
Uso de PWM. Simulación de una salida analógica
void setup() { //No necesitamos realizar tareas de configuración en este caso } void loop() { //Escribimos cada valor analógico separado por intervalos //de 2 segundos analogWrite(9,180); delay(2000); analogWrite(9,220); delay(2000); analogWrite(9,255), delay(2000); }
Fading (programa original de David A. Mellis y Tom Igoe)
/*Fading This example shows how to fade an LED using the analogWrite() function. The circuit: LED attached from digital pin 9 to ground. Created 1 Nov 2008 By David A. Mellis modified 30 Aug 2011 By Tom Igoe http://www.arduino.cc/en/Tutorial/Fading This example code is in the public domain. */ int ledPin = 9; // LED connected to digital pin 9 void setup() { // nothing happens in setup } void loop() { // fade in from min to max in increments of 5 points: for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { // sets the value (range from 0 to 255): analogWrite(ledPin, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } // fade out from max to min in increments of 5 points: for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { // sets the value (range from 0 to 255): analogWrite(ledPin, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30); } }
Control del brillo en un diodo RGB mediante el comando map
//Declaramos las variables asociadas a los pines de salida int rojo=9; int verde=10; int azul=11; //Declaramos las variables que recogerán //la lectura de los potenciómetros int potRojo; int potVerde; int potAzul; //Declaramos las variables que se escribirán //en el pin de control de cada color int valRojo; int valVerde; int valAzul; void setup() { //declaramos los pines de salida pinMode(rojo,OUTPUT); pinMode(verde,OUTPUT); pinMode(azul,OUTPUT); } void loop() { //Leemos los potenciómetros potAzul=analogRead(A0); potVerde=analogRead(A1); potRojo=analogRead(A2); //Transformamos los valores del rango [0,1023] //al rango [0,255] con map() valAzul=map(potAzul,0,1023,0,255); valVerde=map(potVerde,0,1023,0,255); valRojo=map(potRojo,0,1023,0,255); //Por último, activamos cada color del RGB con la intensidad correspondiente analogWrite(azul,valAzul); analogWrite(verde,valVerde); analogWrite(rojo,valRojo); //Fin del bucle }
Código SIMPLIFICADO del control del brillo en un diodo RGB
//Ahora sólo usaremos las variables que se escribirán en el pin de control de cada color int valRojo; int valVerde; int valAzul; void setup() { //declaramos los pines de salida pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); } void loop() { //En un sólo paso hacemos la lectura //y hacemos la transformación con map() valAzul=map(analogRead(A0),0,1023,0,255); valVerde=map(analogRead(A1),0,1023,0,255); valRojo=map(analogRead(A2),0,1023,0,255); //Activamos cada color del RGB con la intensidad correspondiente analogWrite(11,valAzul); analogWrite(10,valVerde); analogWrite(9,valRojo); }
Órgano electrónico con ocho pulsadores usando el comando tone
void setup(){ pinMode(2,INPUT); pinMode(3,INPUT); pinMode(4,INPUT); pinMode(5,INPUT); pinMode(6,INPUT); pinMode(7,INPUT); pinMode(8,INPUT); pinMode(9,INPUT); } void loop(){ if (digitalRead(2)==HIGH){ tone(11,262,100); }else{ noTone(11); } if (digitalRead(3)==HIGH){ tone(11,294,100); }else{ noTone(11); } if (digitalRead(4)==HIGH){ tone(11,330,100); }else{ noTone(11); } if (digitalRead(5)==HIGH){ tone(11,349,100); }else{ noTone(11); } if (digitalRead(6)==HIGH){ tone(11,392,100); }else{ noTone(11); } if (digitalRead(7)==HIGH){ tone(11,440,100); }else{ noTone(11); } if (digitalRead(8)==HIGH){ tone(11,494,100); }else{ noTone(11); } if (digitalRead(9)==HIGH){ tone(11,523,100); }else{ noTone(11); } delay(5); }