Hi guys! I am trying programming from arduino to attiny85 via arduino as ISP.
I want to read analog values with a potentiometer but unfortunately my chip reads be 0 be the maximum value.
Thank you for your answers!
(Sorry for my english I'm french)
here is the code:
int analogValue;
void setup() {
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(PB4,INPUT);
}
void loop() {
analogValue = analogRead(PB4); // the position of the potentiometer is recover
analogWrite(1,analogValue); // I transcribe its position on an LED
if(analogValue < 200){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
}
}
if i remember the adc's are 10 bits so you can read up to 1023 counts on pin pb4, then you
are writing to a pwm channel which can only handle up to 255. So you may want to use
the map function to scale the values.
serial print out analogValue so you know if your potentiometer is working correctly.
So I put a map function in my code but it still doesn't work.
The LED wich is connected to pin 2 continues to be constantly on and the other stops flashing at a certain time by going out
Here is the new code :
int analogValue;
int analogValueUse;
void setup() {
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(PB4,INPUT);
}
void loop() {
analogValue = analogRead(PB4); // the position of the potentiometer is recover
analogValueUse = map(analogValue,0,255,0,1023);// I transcribe its position on an LED
analogWrite(1,analogValueUse);
if(analogValueUse < 200){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
}
}
The map function is backwards, should be 0,1023,0,255
I would forget about the led until you are able to get 0 -1023 counts back from
the potentiometer. Confirm that the pot works.
It would be a good idea to get into the habit of checking the Arduino reference pages for the functions you use, before writing the code. The above call doesn't do what you think, or anything useful, actually.
Sorry I made a mistake sending you my code.
Here is my real code:
int analogValue;
int analogValueUse;
void setup() {
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(PB4,INPUT);
}
void loop() {
analogValue = analogRead(PB4); // the position of the potentiometer is recover
analogValueUse = map(analogValue,0,1023,0,255);
analogWrite(1,analogValueUse); // I transcribe its position on an LED
if(analogValueUse < 200){
digitalWrite(2,HIGH);
}
else{
digitalWrite(2,LOW);
}
}
Somethings not making sense. If your pot is working the led should turn off and on, not just stay on
Try toggling the led pin on and off in code does it blink like it should or not.
For the LED, you probably should be testing analogValue.
Post a wiring diagram.
Put in a Serial.print() to see what analog values are being returned as you vary the pot. It is probably best to write a separate program to test that part of the operation.
So I put a serial.print in my code and I observed that the value was always equal to zero moreover I changed the led of pin 1 and now it is always off.
Sorry but I don't know how to make wiring diagram
Pencil and paper work fine. If you take the hobby seriously, intend to continue, and want to learn the fundamental skill of reading and drawing schematics, this tutorial is excellent.