*SOLVED* Simple code won't work in ATTINY85, please help.

All I'm doing is using the microcontroller to read a LDR and have 1 of 2 outputs (LEDs) high, while the other is low. It works fine on the Uno, but I put it on the ATTINY85 and it just sits there with pin 0 high, no matter how much light I give/take away from the LDR.

Blink works on the ATTINY, so my setup should be good.

Here is the original code from that is on the Uno, which works perfectly:

const int dooropen=13; // variable which stores pin number
const int doorclose=12; //variable which stores pin number

void setup()
{
pinMode(dooropen, OUTPUT); //configures pin 13 as OUTPUT
pinMode(doorclose, OUTPUT); //configures pin 12 as OUTPUT
Serial.begin(9600);
}

void loop()
{
int sensor_value = analogRead(A0);
if (sensor_value > 600)// the point at which the state of LEDs change
{
digitalWrite(doorclose, HIGH);
digitalWrite(dooropen, LOW);
}
if (sensor_value < 400)
{
digitalWrite(doorclose,LOW); //Sets LEDs OFF
digitalWrite(dooropen, HIGH);
}
Serial.println(sensor_value);
delay(500);

}

I tried the original code (changed pin numbers), no joy. I then simplified the code as best I could to this

void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
}

void loop()
{
int sensor_value = analogRead(4); //convenient placement on board
if (sensor_value > 600){

digitalWrite(0, LOW);
digitalWrite(1, HIGH);
}
if (sensor_value < 400){
digitalWrite(1,LOW);
digitalWrite(0, HIGH);
}

}

Still no joy, sits there with pin 0 LED on.

I am working on this with my 10 year old, we are learning Arduino together, but we take such long breaks inbetween touching it that I forget pretty much everything.

Are you sure you're using the right pins?

The physical pin number is not what matters - what matters is the arduino pin number, which you can find by looking at the pinout chart supplied with the core you're using. For my core (and I think other tiny85 cores) its:


(right click and open image in new tab - the forum limits the size of images)

Edit: Oh, no wonder. What are you doing trying to read ADC channel number 4? That doesn't correspond to a pin (depending on which core, it will either do nothing, or read one of the differential ADC channels). You must use an ADC channel number that corresponds to the pin that you have the thing you're measuring connected to; not all pins can be used for this - refer to the chart I posted above.

I was using physical pin 5 & 6 for the outputs, physical pin 3 for the analog input.

Is that not correct? How exactly do I define the analogRead?

So right now I'm using this:

const int dooropen=0; // variable which stores pin number
const int doorclose=1; //variable which stores pin number

void setup()
{
pinMode(dooropen, OUTPUT); //configures pin 13 as OUTPUT
pinMode(doorclose, OUTPUT); //configures pin 12 as OUTPUT
//Serial.begin(9600);
}

void loop()
{
int sensor_value = analogRead(A0);
if (sensor_value > 600)// the point at which the state of LEDs change
{
digitalWrite(doorclose, HIGH);
digitalWrite(dooropen, LOW);
}
if (sensor_value < 400)
{
digitalWrite(doorclose,LOW); //Sets LEDs OFF
digitalWrite(dooropen, HIGH);
}
//Serial.println(sensor_value);
delay(500);

}

and it sort of works. I get no output on pin 0 (physical pin 5). But when I put my hand over the LDR, pin 1 LED is on, remove hand it pin 1 LED goes off. Now I just need another output to be HIGH when the other output is LOW.

GOT IT!!!

Realized that A0 was also the reset.. oops...

This works:
const int dooropen=0; // variable which stores pin number
const int doorclose=1; //variable which stores pin number

void setup()
{
pinMode(dooropen, OUTPUT); //configures pin 13 as OUTPUT
pinMode(doorclose, OUTPUT); //configures pin 12 as OUTPUT
//Serial.begin(9600);
}

void loop()
{
int sensor_value = analogRead(A3);
if (sensor_value > 600)// the point at which the state of LEDs change
{
digitalWrite(dooropen, LOW);
digitalWrite(doorclose, HIGH);
}
if (sensor_value < 400)
{
digitalWrite(doorclose,LOW);
digitalWrite(dooropen, HIGH);
}
//Serial.println(sensor_value);
delay(500);

}