Hi mem,
The sketch I am using to check the problem is this one here:
#include <Servo.h>
int potPin = 0; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the sensor
Servo servo1;
void setup()
{
servo1.attach(14);
servo1.setMaximumPulse(2500);
Serial.begin(19200);
}
void loop()
{
{
val = analogRead(potPin); // read the value from the sensor
servo1.write(val/4); // send value /4 to servo
Serial.print(val, DEC); // print pot value
Serial.println(); // print line return
}
Servo::refresh();
}
I am only using it to check the value from the pot. As there is only one pot in this simple code I can also display the correct value of val/4 in serial monitor. I do not have the servo or any other pots connected at the moment. Everything is connected correctly and the pot seems ok on resistance test.
The problem with this code even, is that the first say 5 degrees of pot angle gives the full range of values (1023 down to about 20) then the values drop right off to less than 10 and stay there until the end of the pot wiper travel. :-?
Strangely, using this other piece of code the pot numbers are correct for the whole range?
int potPin1 = 0; // select the input pin for the potentiometer
int potPin2 = 1;
int ledPin = 13; // select the pin for the LED
float val1 = 0; // variable to store the value coming from the sensor
float val2 = 0;
float val3 = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(19200);
}
void loop() {
val2 = analogRead(potPin2); // read the value from the sensor
val1 = analogRead(potPin1);
val3 = val1/val2;
Serial.print(val1, DEC);
Serial.print(" ");
Serial.print(val2, DEC);
Serial.print(" ");
Serial.print(val3, DEC);
Serial.println();
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val1); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val1); // stop the program for some time
}
Is it the delay function that makes the difference?
Also, forgive my ignorance, but how do I get the serial monitor to display the fraction (or decimal place data)? The serial monitor displays the nearest whole number, even though I have used floats instead of ints in this code?