Using piezo for sound input - resistance level to find using pot
Using pot to "tune" to now set piezo resistance level
3 lights - below range in range above range - of adjusting pot resist. vs piezo resist. PWM through functions
problems other than structure
cannot PWM lights based on the idea of % differences against resistances
// define counter integers
int i = 0;
int ii = 0;
int iii=0;
int fadeAway =0;
const int switchP = 13;
// colorMEASUREPINident
const int rMP1 = 11;
const int rMP2 = 10;
const int rMP3 = 9;
const int gMP = 5;
const int bMPa = 4;
const int yMP = 3;
const int bMPb = 2;
const int pizP = A1;
const int potP = A0;
// colorMEASUREPINident
int switchState = 0;
int prevswitchState = 0;
int knockVal = 0;
int knockVal1 = 0;
int potVal1 = 0;
int potVal = 0;
float knockAverage;
float potAverage;
float potMapVal, knockMapVal;
void setup() {
// monitor ready
Serial.begin(9600);
Serial.println("Serial Monitor Setup Done");
// input output pin declaration assignment
// colorMEASUREPINident
pinMode(rMP1,OUTPUT);
pinMode(rMP2,OUTPUT);
pinMode(rMP3,OUTPUT);
pinMode(gMP,OUTPUT);
pinMode(bMPa,OUTPUT);
pinMode(bMPb,OUTPUT);
pinMode(yMP,OUTPUT);
pinMode(pizP, INPUT);
pinMode(potP, INPUT);
pinMode(switchP,INPUT);
// colorMEASUREPINident
}
void loop() {
Serial.println("Please Press the Button to begin!");
Serial.println(analogRead(pizP));
delay(2000);
switchState = digitalRead(switchP);
// was button pressed
if (switchState != prevswitchState){
Serial.println("Thanks!!! The Game is receiving sound input for a reference number.");
// iterate to create noise values to average
for (i; i<3; i++){
knockVal1 = analogRead(pizP);
digitalWrite(rMP1, HIGH);
digitalWrite(rMP3, HIGH);
digitalWrite(bMPa, HIGH);
digitalWrite(bMPb, HIGH);
delay(500);
digitalWrite(rMP1, LOW);
digitalWrite(rMP3, LOW);
digitalWrite(bMPa, LOW);
digitalWrite(bMPb, LOW);
digitalWrite(rMP2, HIGH);
digitalWrite(gMP, HIGH);
digitalWrite(yMP, HIGH);
delay(500);
digitalWrite(rMP2, LOW);
digitalWrite(gMP, LOW);
digitalWrite(yMP, LOW);
knockVal += knockVal1;
}
delay(2000);
knockAverage = knockVal/i;
Serial.println("We have collected enough sound!");
Serial.println("Next, you will turn the knob to keep the middle red light illuminated!");
delay(2500);
Serial.print("all sound values average: ");
Serial.println(knockAverage);
delay(1500);
// all code above this line does what its required as is
Serial.println("Remember, turn the knob to keep the middle red light illuminated!");
for (ii; ii < 9999; ii++){
potVal1 = analogRead(potP);
potVal += potVal1;
Serial.println("potVal before addition: ");
Serial.println(potVal1);
Serial.println("potVal after addition: ");
Serial.println(potVal);
potAverage = potVal/ii;
// potMapVal = map(potAverage, 0, 1023, 0, 255);
// knockMapVal = map(knockAverage, 0, 1023, 0, 255);
// in range potentiometer average
while(potAverage >= knockAverage*.8 || potAverage <= knockAverage*1.2)
{
analogWrite(rMP1, 0);
analogWrite(rMP2, 255);
analogWrite(rMP3, 0);
}
// lower than knockAverage lighting code
while (potAverage >= knockAverage*.6 || potAverage <= knockAverage*.79){
analogWrite(rMP1, 130);
analogWrite(rMP2, 195);
analogWrite(rMP3, 0);
}
while(potAverage >= knockAverage*.4 || potAverage <= knockAverage*.59){
analogWrite(rMP1, 195);
analogWrite(rMP2, 130);
analogWrite(rMP3, 0);
}
while(potAverage >= knockAverage*.0 || potAverage <= knockAverage*.39){
analogWrite(rMP1, 255);
analogWrite(rMP2, 30);
analogWrite(rMP3, 0);
}
// greater than knockAverage lighting code
while (potAverage >= knockAverage*1.21 || potAverage <= knockAverage*1.4){
analogWrite(rMP1, 0);
analogWrite(rMP2, 195);
analogWrite(rMP3, 130);
}
while(potAverage>=knockAverage*1.41 || potAverage<=knockAverage*1.6){
analogWrite(rMP1, 0);
analogWrite(rMP2, 130);
analogWrite(rMP3, 195);
}
while(potAverage >= knockAverage*1.61 || potAverage <= 1023){
analogWrite(rMP1, 0);
analogWrite(rMP2, 30);
analogWrite(rMP3, 255);
}
}
Serial.println("all pot values average: ");
Serial.println(potAverage);
Serial.print("Pot average: ");
Serial.println(potMapVal);
Serial.print("Knock average: ");
Serial.println(knockMapVal);
// this curly brace is the push button occured
}
// this curly brace is the push button occured
switchState = 0;
}