Analogread() on attiny85 (arduino tiny core) problem

Hi guys! I'm pretty new to arduino (I've had since Christmas), but since I have previous programming experience (software for pc and for fun only), I have always been able to cope with all problems pretty soon. Until now. I was porting my project (universal super duper thing for visually impaired-meter, clock, stopwatch, kitchen timer and water level detector all in one btw.) from arduino to attiny85 via arduino as ISP using newest version of arduino tiny core (http://code.google.com/p/arduino-tiny/) when I ran into following problem: When I start it, it beeps to show it works ok (code in setup() routine), and then attemps to read analog value from connected button. (Reason for this is that I connect 2 buttons via one analog pin-each button has different resistor attached to it, allowing me to know which button is pressed by reading analog value. So I have to do initial calibration, because I don't know how high exactly is the voltage of batteries.) BUT! When my Attiny attemps to read the value, it fails miserably! The value (which I obtained by beeping it out using the algorithm that would normally show minutes, centimeters, etc.) is either 1020 (if I use pin 3), or 7 (if I use pin 4), but doesn't seem to be affected by pressing buttons. Plus, if I use pin 3, voltmeter shows battery voltage (=cca 3v) between it and ground! On pin 4, idle voltage is 0v and it changes when I press buttons, but attiny doesn't seem to care. I have set pins as input pins correctly; on big Arduino Duemillanove it all works out of the box. I am using external pulldown resistors (not relying on built in pullups). Any idea what it might be? It's driving me crazy. Thank you very much. :)

I have more questions than answers, goodhen

For analogRead on IC pin2 you have val = analogRead(3); ? For analogRead on IC pin3 you have val = analogRead(2); ? For analogRead on IC pin7 you have val = analogRead(1); ? Is IC pin5 also supposed to be analog input 0?

IC pin5 is also supposed to be ARef. If a sketch involves any analogReads does that preclude pin5 "ARef" being used for digitalRead/Write or is there a setup instruction?

And your code is where?

runaway: I have more questions than answers, goodhen

For analogRead on IC pin2 you have val = analogRead(3); ? For analogRead on IC pin3 you have val = analogRead(2); ? For analogRead on IC pin7 you have val = analogRead(1); ? Is IC pin5 also supposed to be analog input 0?

THX DUDE! THAT'S WHAT I WAS LOOKING FOR! I wrongly assumed that arduino tiny core uses the same pin setup as hi low tech groups solution for porting arduino code to attiny chips. But it didn't. Works now. You're the boss. 8)

edit: That analog reference thing is so evil! I didn't know it exists and added my own code for gathering analog reference values! GRR!

I'm having the same issue but the actual solution is not readily apparent from the previous posts.

From what I can gather there are multiple specifications for the ports (which seems stupid) but how do I figure out what I need to be using?

My code is this

int ledPin = 0;    // LED connected to PB0 (pin 5)
//int analogPin = 3;    // Pin PB3 (pin 2) Works for Analog input
//int analogPin = 4;    // Pin PB4 (pin 3) Does not work for Analog input
int analogPin = 2;    // Pin PB2 (pin 7) Does not work for Analog input

void setup() {

  pinMode(analogPin, INPUT);

  pinMode(ledPin, OUTPUT);


  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(100);
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(100);
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(1000);

}
void loop() {

  int sensorValue = analogRead(analogPin);    


  digitalWrite(ledPin, HIGH);
  delay(sensorValue);

  digitalWrite(ledPin, LOW);
  delay(sensorValue);
}

Actually while typing this post I tried switching boards to the non arduino tiny core and it worked for pin 7/ADC1/pb2 when I put in 1 for the analog pin.

I would like to get it working for the arduino tiny core but cannot see where the pins are mapped in the hardware core libraries.

Actually playing again has me thinking that there is a bigger issue with the arduino tiny core. I guess I shouldn't be trying to make it work in the 0022 version.

I found one of the issues. The arduino tiny core seems to have the wrong fuse definitions in boards.txt for the 8mhz internal oscillator.

it has high_fuses=0xD7 when it should be high_fuses=0xdf

edit: if you're new to attiny ignore this. More info below :)

it has high_fuses=0xD7 when it should be high_fuses=0xdf

Turns OFF "Preserve EEPROM memory through the Chip Erase cycle; [EESAVE=0]". You are certainly welcome make that change for your applications. The majority of Arduino users seem to prefer the option on. It does not qualify as "wrong fuse definitions" for using the 8 MHz internal oscillator.

//int analogPin = 3;    // Pin PB3 (pin 2) Works for Analog input

PB3 is coincidentally ADC3.

//int analogPin = 4;    // Pin PB4 (pin 3) Does not work for Analog input

PB4 is ADC2. (analog pin 2)

int analogPin = 2;    // Pin PB2 (pin 7) Does not work for Analog input

PB2 is ADC1. (analog pin 1)

[quote author=Coding Badly link=topic=90297.msg689539#msg689539 date=1329164873]

it has high_fuses=0xD7 when it should be high_fuses=0xdf

Turns OFF "Preserve EEPROM memory through the Chip Erase cycle; [EESAVE=0]". You are certainly welcome make that change for your applications. The majority of Arduino users seem to prefer the option on. It does not qualify as "wrong fuse definitions" for using the 8 MHz internal oscillator. [/quote] I do believe that you are correct.

What I was seeing though was a significant difference in performance based upon changing that fuse which didn't make sense at the time and still doesn't make sense. This morning though it was all bad and no matter what I do the attiny85 runs slow so I did something in the files that has caused an issue.

So my learning task for today is the boards.txt file and the boot loader process.

One thing the attiny programming has taught me is that the arduino ide has some real limitations. Like needing to restart the whole application to pick up a boards.txt change and it does not associate the board with the sketch so if you have two sketches open and switch boards in one it will change it in the other. Such things just add to the already inevitable frustration =(

[quote author=Coding Badly link=topic=90297.msg689539#msg689539 date=1329164873]

//int analogPin = 3;    // Pin PB3 (pin 2) Works for Analog input

PB3 is coincidentally ADC3.

//int analogPin = 4;    // Pin PB4 (pin 3) Does not work for Analog input

PB4 is ADC2. (analog pin 2)

int analogPin = 2;    // Pin PB2 (pin 7) Does not work for Analog input

PB2 is ADC1. (analog pin 1)

[/quote] I found this thread http://arduino.cc/forum/index.php/topic,82951.0.html so now I know where to go hunting in the source for the pins designations or work out how to make up my own that are pin compatible with the arduino so sketches will run the same on either device.

Thank you Mr Coding or is that Mr Badly ;) for all your effort in getting the attiny mc's working through the arduino ide.

I followed your various steps as they appear around the place and I now have the bootloaders and the sketch upload working properly in both 0022 and V1.0

I still have a lot to look through to understand it all though.

I am having the problems while using analogread function.. I have to give an output by taking just an analog reading .... I used Attiny85 pin no. 2 for reading input and in code i write val=analogRead(3); and using ic pin no. 5 for output. What should i do?? help me.. Thanks

Read this carefully so you know how to ask a question in a way that we can actually give help, then based on what you just read open your own thread.