So I am currently working on an Arduino project. I have built it and coded it. The potentiometer is working fine and the readings I am expecting to get I get. But whenever these values are displayed in the serial monitor the response I am expecting does not happen. I was hoping for some advice/tips.
I am putting the code below, and a picture of my build. The goal is that after the button is pressed, based of potentiometer readings different lights and tones will be on.
my code:
const int piezo = 8;
const int pot = A0;
const int switchPin = 2;
const int redLED = 3;
const int yellowLED = 4;
const int greenLED = 5;
int potVal;
int switchState;
int notes[] = {262,294,330};
//labeled all parts with corresponding pin
void setup() {
// put your setup code here, to run once
Serial.begin(9600);
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(switchPin, INPUT);
pinMode(pot, INPUT);
pinMode(piezo, OUTPUT);
//set each part as an input or an output
}
void loop() {
// put your main code here, to run repeatedly:
int potVal = analogRead(A0);
switchState = digitalRead(2);
//reading the value of the switch on or off to then let the rest of the circuit function of this
if(switchState == LOW)
{
//button not pressed
potVal = 0;
}
else if(switchState == HIGH)
//button is pressed
{
potVal = analogRead(A0);
}
Serial.print("potVal: ");
Serial.println(potVal);
//let me see value of potentiometer i hope
// after this comes the lighting and tone code but right now its not working
if (potVal = 0)
{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
noTone(8);
}
else if(potVal <= 330 && potVal >= 1)
{
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
tone(8, notes[1]);
}
else if(potVal <= 660 && potVal >= 331)
{
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
tone(8, notes[2]);
}
else if(potVal <= 1023 && potVal >= 661)
{
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
tone(8, notes[3]);
}
}