Help With My First Ever Project

Hi, This is my first ever project. I purchased the Arduino a few months ago but never got further than the blink example :-[ Poor I know.

Anyway I got home today from work after talking to a dude at work about a paintball sentry and it got my Arduino juices flowing.

Ive been using the examples and managed to get a Pot, Servo, Serial and LED thing going on. But I cant get my LED to light if (val > 500)

I was wondering if anyone could point me in the right direction :smiley:

// Pot-Servo-Serial-LED Mod By Telly!
//   :D

#include <Servo.h> 

Servo myservo;         // create servo object to control a servo 
int potpin = 7;        // analog pin used to connect the potentiometer
int val;               // variable to read the value from the analog pin 
int ledPin = 3;        // LED connected to digital pin 3

void setup() 
{ 
  myservo.attach(2);         // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);        // setup serial connection
  pinMode(ledPin, OUTPUT);   // initialize the digital pin as an output:
} 

void loop() 
{ 
    val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  int analogValue = analogRead(7);     // reads the value of the potentiomete
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  Serial.println(analogValue);         // sends analogValue to serial
  delay(1);                            // waits for the servo to get there 

  if (val > 500) 
  {
  digitalWrite(ledPin, HIGH); 
  }
}

Any "light" would be great!

Thank you all Telly.

Hardly surprising that doesn't work. Consider: val will never exceed 500 because you just map()ed it to a range between 0 and 179

Here you scale val to be between 0 and 179...

  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)

...which means here...

  if (val > 500)

...val cannot ever be greater than 500.

Move the "if (val > 500)" statement right below the "val = analogRead(potpin);" line and you should be good to go.

Noob is not the word ;D Thanks!

I've also changed "Serial.println(analogValue);" to "Serial.println(val);".

Thanks Again..