Go Down

Topic: problem with build project (Read 391 times) previous topic - next topic


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
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: ");
 //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);
 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]);




READ THIS POST to help you get the best out of the forum.


These steps may also save you some unnecessary time and effort in the forum.

  • Always provide as much detail as possible to your issue it may be important.
  • If applicable provide your SKETCH inside CODE TAGS ( </> ) or a CREATE share link.
  • If applicable provide a schematic even if hand drawn.
  • How to insert a picture into your post
  • If applicable provide a proper LINK to the hardware / sensors etc. you are using.
    It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

    Go Up