Help on changing LED lighting by servo meter positioning

I am trying to modify Project 5 in the Starter Kit by making the position of the servo meter change which of the three LEDs go off. I have a couple of questions on this.

  • Do I connect the LEDs to the same pin as the Servo Meter, or are they all connected to a different one?
  • Am I writing the code wrong? (Posted below)
  • Is this completely off and I’m just getting tired?

Here’s my current code:

#include <Servo.h>
Servo myServo;

const int redLEDPin = 9;
const int yellowLEDPin = 10;
const int greenLEDPin = 11;

int const redpotVal = A3;
int const yellowpotVal = A2;
int const greenpotVal = A1;

int const potPin = A0;

int potVal;
int angle;

void setup() {
  myServo.attach(6);
  
  Serial.begin(9600);
  
  pinMode(redLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  
}

void loop() {
  potVal = analogRead(potPin);
  Serial.print("potVal: ");
  Serial.print(potVal);
  
  angle = map(potVal, 0, 1023, 0, 179);
  Serial.print("angle: ");
  Serial.print(angle);
  
  myServo.write(angle);
  
  if(potVal <341){
    (redLEDPin, LOW);
    (yellowLEDPin, LOW);
    (greenLEDPin, HIGH);
    
  }else if(potVal >340 && potVal <640){
    (redLEDPin, LOW);
    (yellowLEDPin, HIGH);
    (greenLEDPin, LOW);
    
  }else if(potVal >640){
    (redLEDPin, HIGH);
    (yellowLEDPin, LOW);
    (greenLEDPin, LOW);
    
  }
  
  analogWrite(redLEDPin, redpotVal);
  analogWrite(yellowLEDPin, yellowpotVal);
  analogWrite(greenLEDPin, greenpotVal);
  delay(15);
}

I’ll post a pic of the diagram tomorrow morning.

Did you mean to say:

  if(potVal <341){
    digitalWrite(redLEDPin, LOW);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(greenLEDPin, HIGH);
    
  }else if(potVal >340 && potVal <640){
    digitalWrite(redLEDPin, LOW);
    digitalWrite(yellowLEDPin, HIGH);
    digitalWrite(greenLEDPin, LOW);
    
  }else if(potVal >640){
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(greenLEDPin, LOW);
  }

You should probably also get rid of this code which overrides the code above:

  analogWrite(redLEDPin, redpotVal);
  analogWrite(yellowLEDPin, yellowpotVal);
  analogWrite(greenLEDPin, greenpotVal);

Ok, yes that should work now.