Voici deux beaux schémas réaliser par Gérard à l'aide de Fritzing : à tester au plus vite
Et le petit code qui va avec :
------------------------------------------------------------------------------------------------------------------------------
//Squishy RGB LED Controller - Code written by Matthew Schmidtbauer for the Squishy Circuits Project with AnnMarie Thomas
// permet de definir l'usage de la carte (relier une ou plusieurs de ces broches à la masse permet de choisir un programme particulier.)
// C'est un bon moyen d'avoir un seul sketch pour tous les bidouillages. Avec 3 broches, 8 montages différents sont possibles
// Une broche non reliée à la masse = 1 = défaut
// SW3 SW2 SW1 montage Valeur décimale Valeur du registre PIND <= VALEUR A PRENDRE EN COMPTE
// 1 1 1 Led RVB 7 14
// 1 1 0 Speaker 6 10
// 1 0 1 Moteur 5 12 (ralentir / accélerer un moteur DC/DC)
//
// 2 8 4
#define SW1 0 //4
#define SW2 1 //8
#define SW3 2 //2
const boolean debug = false;
int borne_min, borne_max;
byte mode = 0;
int speaker = 5;
int vRefPin = 4; // pin (digitale OUT) pour activer le vRef seulement quand on en a besoin
// permet d'éviter une perte de charge dans la pâte si on reste sur le +5v en permanence (la pâte semble être une charge capacitive)
int RED_LED = 9;
int GREEN_LED= 10;
int BLUE_LED = 11;
int squishy = 0; // squichy sur A0 --> RED
int squishy2 = 1; // squichy sur A1 --> GREEN
int squishy3 = 2; // squichy sur A2 --> BLUE
void setup()
{
noTone(speaker);
pinMode(vRefPin, OUTPUT);
digitalWrite(vRefPin, LOW);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(speaker, OUTPUT);
pinMode(squishy, INPUT);
pinMode(squishy2, INPUT);
pinMode(squishy3, INPUT);
// Les valeurs min et max de la resistance du squishy
borne_min = 300;
borne_max = 1023;
if (!debug) {
Serial.begin(9600); //Uncomment all Serial Commands to allow Serial Communications for Debugging
while(!Serial) {} // Leonardo seulement
delay(500);
} else {
digitalWrite(SW1, HIGH);
digitalWrite(SW2, HIGH);
digitalWrite(SW3, HIGH);
}
}
void loop()
{
if (!debug) {
mode = 0;
mode |= (B00001110 & PIND);
}
// a remplir
switch (mode) {
case 14: rgbLed(); break;
case 10: sound(); break;
default: break;
}
}
void rgbLed() {
// pour le squishy RGB
int analog_RED = 0; // common resistor connected to analog pin 0 outside leads to ground and +5V
int raw_RED = 0; // variable to store the raw input value
int BRIGHTNESS_RED = 0; //Variable to store Brightness Value (0-255)
int analog_GREEN = 1; // common resistor connected to analog pin 1 // outside leads to ground and +5V
int raw_GREEN = 0; // variable to store the raw input value
int BRIGHTNESS_GREEN = 0; //Variable to store Brightness Value (0-255)
int analog_BLUE = 2; // common resistor connected to analog pin 2 // outside leads to ground and +5V
int raw_BLUE = 0; // variable to store the raw input value
int BRIGHTNESS_BLUE = 0; //Variable to store Brightness Value (0-255)
digitalWrite(vRefPin, HIGH);
delay(2);
raw_RED = analogRead(squishy);
delay(2);
raw_GREEN = analogRead(squishy2);
delay(2);
raw_BLUE = analogRead(squishy3);
digitalWrite(vRefPin, LOW);
//Generate Brightness Value (PWM Values = 0-255) from Raw Voltages (0-1023)
BRIGHTNESS_RED = map(constrain(raw_RED, borne_min, borne_max), borne_min, borne_max, 0, 255);
BRIGHTNESS_GREEN = map(constrain(raw_GREEN, borne_min, borne_max), borne_min, borne_max, 0, 255);
BRIGHTNESS_BLUE = map(constrain(raw_BLUE, borne_min, borne_max), borne_min, borne_max, 0, 255);
if (raw_RED == 1023) { BRIGHTNESS_RED = 0; }
if (raw_GREEN == 1023) { BRIGHTNESS_GREEN = 0; }
if (raw_BLUE == 1023) { BRIGHTNESS_BLUE = 0; }
analogWrite(RED_LED, BRIGHTNESS_RED); //Output PWM Value to Light
analogWrite(GREEN_LED, BRIGHTNESS_GREEN); //Output PWM Value to Light
analogWrite(BLUE_LED, BRIGHTNESS_BLUE);
if (debug) {
Serial.print("RED: ");
Serial.print(raw_RED);
Serial.print("/");
Serial.print(BRIGHTNESS_RED);
Serial.print(" Green: ");
Serial.print(raw_GREEN);
Serial.print("/");
Serial.print(BRIGHTNESS_GREEN);
Serial.print(" Blue: ");
Serial.print(raw_BLUE);
Serial.print("/");
Serial.println(BRIGHTNESS_BLUE);
delay(10);
}
}
void sound() {
int raw_data = 0; // donnée brute (utilisée comme fréquence)
int duration = 50; // durée de chaque tone fixée à une seconde
digitalWrite(vRefPin, HIGH);
delay(2);
raw_data = analogRead(squishy); // (plage de fréquence plus étendue que 0 - 1023 Hz ^_^ là ça monte jusqu'à 10khz)
if (raw_data == 1023) { raw_data = 0; } // si la pate est coupée, on coupe le son... Meilleur pour les nerfs ^^
digitalWrite(vRefPin, LOW);
if (debug) {
Serial.print("Setting tone to ");
Serial.print(raw_data*5);
Serial.print(" Hz / ");
Serial.println(raw_data);
}
tone(speaker, raw_data*5, duration);
}
------------------------------------------------------------------------------


