button = (analogRead(A0)*5+512)/1024;which returns a number between 0 and 5. The advantage of the above design is that it uses five identical resistors, so it's easy to construct. The disadvantage is that it draws current even when no button is pressed; in this case 1mA. Here's an alternative design that solves this problem:
button = (analogRead(A0)*5+512)/1024;
int led = 3; void setup() { digitalWrite(led, LOW); pinMode(led, INPUT); } void loop() { // Read input if (digitalRead(led) == LOW) { // Turn LED on pinMode(led, OUTPUT); delay(1000); // Turn LED off pinMode(led, INPUT); } }
/* Using the Reset pin as ADC0 */ const int Speaker = 3; // Beep out a 10-bit binary number, LSB first void debug (unsigned int thing) { for (unsigned int b=1; b!=1024; b = b<<1) { for (unsigned int i=0; i<1250; i++) { if (thing &b) digitalWrite(Speaker, (i &16)); else digitalWrite(Speaker, (i &32)); } for (unsigned int i=0; i<1250; i++) digitalWrite(Speaker, 0); } } void setup() { pinMode(Speaker, OUTPUT); } void loop() { debug(analogRead(0)); delay(2000); }To reveal the ADC reading I've connected a piezo speaker between PB3 and ground, and I'm using a simple debug routine which beeps out the ten-bit ADC value as a series of low and high beeps, LSB first, with a low beep representing a 0 and a high beep representing a 1. I tested it by connecting a potentiometer across the supply, with the wiper connected to the reset pin. Here are the results: ●5v - reading 0b11111111 or 1023 ●2.2V - reading 0b0111000011 or 451 ●Below 2.2V - ATtiny85 reset This corresponds to the datasheet, and gives us a range of 2.8V over which we can use the reset pin for analogue measurements. One simple application is to sense a pushbutton that switches ADC0 between 3v and 5v:
/* Blink Reset pin */ int led = 5; // Reset pin void setup() { DDRB = 1<<led; } void loop() { PINB = 1<<led; delay(1000); }To program the ATtiny85 I used Sparkfun's Tiny AVR Programmer [1]. To use this with the Arduino IDE install the ATtiny microcontroller support for the Arduino IDE. You now need to reprogram the High Fuses to 0x5f rather than 0xdf, to set the RSTDISBL fuse. The simplest way is to use a high-voltage programmer, which lets you set the fuses to anything you want. You can use an Arduino Uno as a high-voltage programmer [2]. Alternatively Proto-Pic in the UK used to sell a high-voltage programmer shield which used a 12V external power supply. Unfortunately this is no longer available.
// Desired fuse configuration #define HFUSE 0x5F #define LFUSE 0x62and run the program to set the RSTDISBL fuse. Now if you put the ATtiny85 back in the circuit the LED connected to pin 1, the reset pin, should flash as required. Note that the reset pin doesn't have such a high drive capability as the other pins, so the LED will be dimmer than an LED on pins 0 to 4.
avrdude: initialization failed, rc=-1You will need to reset the fuses to their default values using the High-Voltage Programmer again before proceeding.