Lights and music - mcworkshop

MC        WORKSHOP
Vai ai contenuti

Menu principale:

Lights and music

Projects
INDICE
Flip Flop Project # 1
RGB Project # 2
VU Meter Project # 3
Audio test
Fade LED up and down Project # 4


Flip Flop
Project # 1
n°2 resistor 220 ohm
n°2 led


clic to enlarge

// Flip Flop
//This code is in the public domain
void setup()
{
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
delay(500);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
delay(500);
}
RGB
Project # 2
n°2 resistor 220 ohm
n°1 resistor 420 ohm
n° 1 Led RGB (negative common)


clic to enlarge



First Sketch
//RGB LED with potentiometer
//This example code is in the public domain.
//created 03/2019 by M.C. rev 24/08
const int x = 3;
const int y = 6;
const int z = 5;
const int pinrvar = A0;
int valrvar;
int valr;
int valg;
int valb;
void setup()
{
pinMode(x, OUTPUT);
pinMode(y, OUTPUT);
pinMode(z, OUTPUT);
pinMode (A0, INPUT);
}
void loop()
{
valrvar = analogRead(pinrvar);
if (valrvar > 511) {
  valg = map (valrvar, 0, 510, 0, 255);
  analogWrite (y, valg);
  analogWrite (z, 255);
  valr = map (valrvar, 0, 510, 255, 0);
  analogWrite (x, valr);
  delay(20);
}
if (valrvar <= 511) {
  valg = map (valrvar, 0, 511, 255, 0);
  analogWrite (y, valg);
  analogWrite (x, 255);
  valb = map (valrvar, 0, 511, 0, 255);
  analogWrite (z, valb);
  delay(20);
}

-----------------------
Second Sketch
//Potentiometer - Visible light - 380nm to 750nm + white and black
//This example code is in the public domain.
//created 03/2019 by M.C.
const int r = 3; //pin 3
const int g = 6; // pin 6
const int b = 5; // pin 5
int x = 0;
int val = 0;
void setup()
{
Serial.begin(9600);
 pinMode(r, OUTPUT);
pinMode(b, OUTPUT);
pinMode(g, OUTPUT);
}
void loop()
{
val = analogRead(x);
Serial.println(val);
delay(500);

if (val >= 0 && val <= 150){
analogWrite (r,128);//viola - violet
 analogWrite (g, 0);
  analogWrite (b, 128);
}
if (val >= 150 && val <= 300){
analogWrite (r, 0);//blu - blue
 analogWrite (g, 0);
  analogWrite (b, 255);
}
 if (val >= 300 && val <= 450){
analogWrite (r, 0);//verde - green
 analogWrite (g, 255);
  analogWrite (b, 0);  
}
if (val >=450 && val <= 600){
analogWrite (r, 255);//giallo - yellow
 analogWrite (g, 255);
  analogWrite (b, 0);  
}
if (val >=600 && val <= 750){
analogWrite (r, 255);//rosso - red
 analogWrite (g, 0);
  analogWrite (b, 0);
}
if (val >=750 && val <= 900){
analogWrite (r, 255);//bianco - white
 analogWrite (g, 255);
  analogWrite (b, 255);
}
if (val >=900 && val <= 1023){
analogWrite (r, 0);//nero - black
 analogWrite (g, 0);
  analogWrite (b, 0);
}
}
VU Meter
Project # 3
n°4 to 12 resistor 220 ohm
n° 4 to 12 led

clic to enlarge
SKetch download

//Wu Meter
//This example code is in the public domain.
// by Marco C. Marzo 2019
const int nleds = 4; //mettere il numero di led corrispondenti a quanti ne montate sul primo canale
const int nleds2 = 4;//mettere il numero di led corrispondenti a quanti ne montate sul secondo canale
const int ledPins [] = {4, 5, 6, 7}; //dichiarate su quali pin sono collegati i led del primo canale
const int ledPins2 [] = {12, 11, 10, 9};//dichiarate su quali pin sono collegati i led del secondo canale
const int analogInPin = A0; //l'ingresso del segnale DX proveniente dalle cuffie su A0
const int analogInPin2 = A1; //l'ingresso del segnale SN proveniente dalle cuffie su A1
const boolean LED_ON = HIGH;
const boolean LED_OFF = LOW;
int sensorValue = A0;
int sensorValue2 = A1;
int ledLevel = A0;
int ledLevel2 = A1;
void setup() {
 for (int led = 0; led < nleds; led++)
   for (int led = 0; led < nleds2; led++)
   {
     pinMode (ledPins[led], OUTPUT);
     pinMode (ledPins2[led], OUTPUT);
   }
}
void loop() {
 sensorValue = analogRead (analogInPin);
 sensorValue2 = analogRead (analogInPin2);
 ledLevel = map(sensorValue, 0, 10, 0 , nleds);
 ledLevel2 = map(sensorValue2, 0, 10, 0 , nleds); // la mappatura va cambiata in base al segnale delle cuffie 0, 10, 0 , NbrLEDs
 for (int led = 0; led < nleds; led++)
   for (int led = 0; led < nleds2; led++)
   {
     if (led < ledLevel) {
       digitalWrite(ledPins[led], LED_ON);
     }
     else {
       digitalWrite(ledPins[led], LED_OFF);
     }
     if (led < ledLevel2) {
       digitalWrite(ledPins2[led], LED_ON);
     }
     else {
       digitalWrite(ledPins2[led], LED_OFF);
     }
   }
}
Audio test
CAUTION DO NOT LISTEN INTO YOUR HEADPHONE WITH HIGH VOLUME!
NON ASCOLTARE IN CUFFIA A VOLUME ALTO!
DURATA 1'02'' / FORMATO mp3 / 256kbps
The audio test lasts about one minute and contains the following:
classic break
dlhome
electroloop
frequency in square wave at 30Hz
frequency in square wave at 400Hz
frequency in square wave at 1KHz
frequency in square wave at 2KHz
frequency in sinusoidal wave at 30Hz right channel
sine wave frequency at 30Hz left channel
Fade LED up and down
Project # 4
n°1 resistor 220 ohm
n° 1 led

//Fade LED up and down
//This example code is in the public domain.
// 03-2019
int pin = 3;
void setup() {
}
void loop() {
 int x = 1;
 for (int i = 0; i > -1; i = i + x)
 {
   analogWrite(pin, i);
   if (i == 255)
   {
     x = -1;
   }
   delay(20);
 }
}
 
Copyright 2015. All rights reserved.
Torna ai contenuti | Torna al menu