Hello everyone!
I'm a new Arduino developer, so I'm trying to make the examples and understand what their code means to do!
Anyway, I tried to modify the Knob example (it uses a potentiometer to turn a micro servo) getting this final code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 6;
int led5 = 7;
int n = 180; // value to search
void setup()
{
myservo.attach(8); // attaches the servo on pin 9 to the servo object
pinMode(led1, OUTPUT); // Too High LED (red)
pinMode(led2, OUTPUT); // Near - High LED (yellow)
pinMode(led3, OUTPUT); // Right Position LED (green)
pinMode(led4, OUTPUT); // Near - Low LED (yellow)
pinMode(led5, OUTPUT); // Too Low LED (red)
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // 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
delay(15); // waits for the servo to get there
if (val < (n-5)) { // this If cicle finds what LED to light
digitalWrite(led5, HIGH); // the LEDs are inverted because of the positive and negative rotation are inverted on my servo
}else{
if ((val >= (n-5)) && (val < n)) {
digitalWrite(led4, HIGH);
}else{
if (val = n) {
digitalWrite(led3, HIGH);
}else{
if ((val > n) && (val <= (n+5))) {
digitalWrite(led2, HIGH);
}else{
digitalWrite(led1, HIGH);
}
}
}
}
digitalWrite(led1, LOW); // turns off all the LEDs
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
}
(code tags fixed by moderator: code, and /code
It's pretty easy, you set a number that you want to find as a corner measured in degrees and then you find it turning the potentiometer. The 5 LEDs help you finding the right position by saying you if the corner is bigger than the one you want to find, or near big etc (there are comments in the code, you can see all there), just as a guitar tuner would do.
Anyway, for some reason the LEDs n° 1 and 2 (Digital input n° 9 and 10) don't light up. They are all connected right, everyone the same as the others.
Basically it's just [digital n] - [220 Ohm resistor] - [LED's anode] - [LED's cathode] - [gnd]
Any explanations on why this simple thing doesn't work?
Thanks everyone! :D[/code]