Hi Guys, i am NEW to Arduino and just started 3 days ago. So much fun.
Now i am learning together with my 10years old girl
Its a "Naughty Meter" , using servo to be the dial and make a meter. Led should be light analogy along with the dial.
Here is my problem.
I am doing the servo example and successfully control the servo moving from 0 - 180 with my potentiometer.
Also successfully make my LED as analogWrite with my potentiometer.
HOWEVER, when i put 2 codes together. its NOT working. the led only light up when the MAX value as 255 of the mapping.
Here is the code.
Did i missed something?
I try to put delay in-between 2 analogRead. Still not working.....
#include <Servo.h>
Servo myservo;
int led=10;
int potpin = A0;
int val;
int ledval=0;
int ledmap=0;
#include <Servo.h>
Servo myservo;
int led= 10;
int potpin = A0;
int val;
int ledval=0;
int ledmap=0;
void setup() {
Serial.begin(9600);
myservo.attach(9);
pinMode(led,OUTPUT);
pinMode(potpin,INPUT);
}
void loop() {
Serial.println(ledval);
analogWrite(led,ledval);
val = analogRead(potpin);
delay(15);
val = map(val, 0, 1023, 180, 0);
myservo.write(val);
delay(15);
ledmap=analogRead(potpin);
delay(15);
ledval = map(ledmap, 0, 1023, 0, 255);
delay(15);
}
But have a look at how changed to code and read my comments
#include <Servo.h>
Servo myservo;
const byte Led = 3; //Use 3, 10 can't work with servo ;)
const byte PotPin = A0;
const byte ServoPin = 9; //Why name led an pot, but not poor old servo?
void setup() {
Serial.begin(9600);
myservo.attach(ServoPin);
pinMode(Led,OUTPUT);
pinMode(PotPin,INPUT);
}
void loop() {
//let's start with reading the pot shall we?
int val = analogRead(PotPin);
Serial.print("Pot val: ");
Serial.println(val); //print it
//Map the value for the led and print it
int ledVal = map(val, 0, 1023, 0, 255);
Serial.print("Led val ");
Serial.println(ledVal);
analogWrite(Led, ledVal); //Set the led
//we already read the pot, no need to do it again
//map the value for the servo
int servoVal = map(val, 0, 1023, 180, 0);
Serial.print("Servo val ");
Serial.println(servoVal);
myservo.write(servoVal);
//Not need to use delays...
}
thank you all for your fast reply. And Good that internet can make us learn something New everyday.
I try to upload your Code Septillion, all looks fine , except the led still have problem.
My understanding , as the dail closer to value 255, the brighter the led should be . but the led only light up at the beginning . once i turn the potentiometer, it just turn off.
Thank you Both AWOL and Septillion Again!
After all i find out its NOT software problem but hardware, its the wiring problem. and it light up because of the surrounding NOISE.