Étape 3: Le Code
Troisième partie
Le Code
Remarque : Ce code est composé de 6 « si instructions else "qui racontent chaque note individuelle à sonner lorsque son photorésistance correspondante atteint une valeur qui est inférieure au seuil fixé. Le code prend une lecture de la lumière ambiante pendant cinq secondes lorsqu’il est tourné tout d’abord, qui à son tour, définit son propre seuil. Broche D3 (première note) se fait entendre lorsque l’instrument est prêt à jouer.
COPIER LE CODE ET UPLOAD À ARDUINO :
/*
*/
Configuration des broches analogiques (résistance de la Photo)
const int photofor1 = A0 ;
const int photofor2 = A1 ;
const int photofor3 = A2 ;
const int photofor4 = A3 ;
const int photofor5 = A4 ;
const int photofor6 = A5 ;
le programme d’installation pour broches numériques (Obtient le son de la puce 555)
sound1 int = 3 ;
sound2 int = 5 ;
int solide3 = 6 ;
int sound4 = 9 ;
int sound5 = 10 ;
int sound6 = 11 ;
mise en place d’une boucle for pour auto set capteur valeurs
int sensorPin [6] = {A0, A1, A2, A3, A4, A5} ; broches que les capteurs sont fixés à
int sensorValue = 0 ;
int sensorMin [] = {1023,1023,1023,1023,1023,1023} ;
int sensorMax [] = {0,0,0,0,0,0} ;
void setup() {}
pinMode (sound1, sortie) ;
pinMode (sound2, sortie) ;
pinMode (solide3, sortie) ;
pinMode (sound4, sortie) ;
pinMode (sound5, sortie) ;
pinMode (sound6, sortie) ;
digitalWrite(sound1,LOW) ;
digitalWrite(sound2,LOW) ;
digitalWrite(sound3,LOW) ;
digitalWrite(sound4,LOW) ;
digitalWrite(sound5,LOW) ;
digitalWrite(sound6,LOW) ;
Serial.Begin(9600) ;
Étalonner pendant les cinq premières secondes
alors que {(millis() < 5000)
pour (int i = 0; i < = 5; i ++)
{
sensorValue = analogRead(sensorPin[i]) ;
reporter la valeur maximale de capteur (pour tous)
Si (sensorValue > sensorMax[i]) {}
sensorMax [i] = sensorValue ;
}
reporter la valeur minimale de capteur (pour tous)
Si (sensorValue < sensorMin[i]) {}
sensorMin [i] = sensorValue ;
}
}
}
Imprime tous les numéros pour chaque résistance photo (haute et basse)
Serial.Print("\n\n") ;
pour (int i = 0; i < = 5; i ++)
{
Serial.Print(sensorMin[i]) ;
Serial.Print('\t') ;
}
Serial.Print('\n') ;
pour (int i = 0; i < = 5; i ++)
{
Serial.Print(sensorMax[i]) ;
Serial.Print('\t') ;
}
Serial.Print("\n\n") ;
Fait avec calibration automatique
met une note sur lorsqu’elle est prête à jouer
digitalWrite (sound1, HIGH) ;
Delay(500) ;
}
void loop() {}
int analogValue1 = analogRead(photofor1) ;
Delay(1) ;
int analogValue2 = analogRead(photofor2) ;
Delay(1) ;
int analogValue3 = analogRead(photofor3) ;
Delay(1) ;
int analogValue4 = analogRead(photofor4) ;
Delay(1) ;
int analogValue5 = analogRead(photofor5) ;
Delay(1) ;
int analogValue6 = analogRead(photofor6) ;
Delay(1) ;
Note 1
Si (analogValue1 < (sensorMin [0] - 50)) {}
digitalWrite (sound1, HIGH) ;
}
else {}
digitalWrite(sound1,LOW) ;
}
Note 2
Si (analogValue2 < (sensorMin [1] - 50)) {}
Serial.println ("note 2") ;
digitalWrite (sound2, HIGH) ;
}
else {}
digitalWrite(sound2,LOW) ;
}
Note 3
Si (analogValue3 < (sensorMin [2] - 50)) {}
digitalWrite (solide3, HIGH) ;
}
else {}
digitalWrite(sound3,LOW) ;
}
Note 4
Si (analogValue4 < (sensorMin [3] - 50)) {}
digitalWrite (sound4, HIGH) ;
}
else {}
digitalWrite(sound4,LOW) ;
}
Note 5
Si (analogValue5 < (sensorMin [4] - 50)) {}
digitalWrite (sound5, HIGH) ;
}
else {}
digitalWrite(sound5,LOW) ;
}
Note 6
Si (analogValue6 < (sensorMin [5] - 50)) {}
digitalWrite (sound6, HIGH) ;
}
else {}
digitalWrite(sound6,LOW) ;
}
}