Mapping 2 analog output with only 1 analog input?

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 :slight_smile:

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;

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);
}

chuchudiary:
Did i missed something?

Yep, code tags!

#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 :slight_smile:

#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...
}
int led=10;

Read this carefully.

Please use code tags when posting code.

Thanks AWOL, almost forgot to change that part :o

septillion:
Thanks AWOL, almost forgot to change that part :o

You almost forgot to change this part too int servoVal = map(val, 0, 1023, 0, 255);

:-[ :-[ :-[ :-[

Can I blame the fact my lunch was waiting?

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.

I try to upload your Code Septillion, all looks fine , except the led still have problem.

Try it again, but read it carefully and note the differences between it and your original code.

If you're still having problems, post your code inside [code][/code]

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.

The Code from Septillion is working perfectlly.

Cool to learn from you 2 . cheers.

No, it's because PWM doesn't work on pins 9 and 10 when the Servo library is in use.

WoW... thats explain why the pin 10 is only on and off, becuse its become digital output rather than PWM.

i c.

But why my Pin 9 still connnect to the servo.? and its still working????

Because the PWM driving the servo is created in software by the Servo library, not by the hardware timers.

Wo... thats beyond my knowledge about arduino so far . i will do more google and study on it . Thx again. have a Great Day.