Using a photo resistor with an ATtiny85

Hello!!!

Glad to join this forum!

I just need help with the following:

I am currently creating a project that will use an ATtiny85 to set an output after a delay (determined in the setup by a potentiometer) ONLY WHEN the lights are OFF (this part using a photo resistor)

For some reason, the chip is behaving as if the photo resistor is not even present, activating the output right after the delay. It works on the arduino, but not with the Attiny85. My code is below. Any suggestions?

int potpin = A3;
int photoresistor = A2;
//Label outputs:
int Rledone = 2;
int Rledtwo = 1;
int relay = 0; //NC CONTACTS
//Label variables:
int val;
int delaytime;

void setup() {
//Define inputs:
  pinMode(potpin, INPUT);
  pinMode(photoresistor, INPUT);
//Define outputs: 
  pinMode(Rledone, OUTPUT);
  pinMode(Rledtwo, OUTPUT);
  pinMode(relay, OUTPUT);
  while(millis() < 15000) {
  RoomSetting();
  }
}

void loop() {
  if (analogRead(photoresistor) <= 90) {
  delay(delaytime);
  digitalWrite(relay, HIGH); 
  }
  if (analogRead(photoresistor) >= 91) {
    delay(5000);
    digitalWrite(relay, LOW);
  }
}


void RoomSetting() {
  val = analogRead(potpin);
  
  if(val == 0) {
    delaytime = 5000;
  }
  
  if(val > 0 && val <= 341) {
    digitalWrite(Rledone, HIGH); 
    digitalWrite(Rledone, LOW);
    delaytime = 10000;
  }
  
  if(val > 682 && val <= 1023) {
    digitalWrite(Rledone, HIGH);
    digitalWrite(Rledtwo, HIGH);
    digitalWrite(Rledone, LOW);
    digitalWrite(Rledtwo, LOW);
    delaytime = 20000;
  } 

}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Which core are you using?

In your RoomSetting() function you have a big gap for when val >= 342 and <= 681.

alright... that problem sorted itself out. Photocell is working fine. NOW i'm having a bigger issue....

For some reason, the LEDs hooked up to pins 2 and 1 are turning "on" before the chip is even powered (I have the chip's V+ unplugged)
Here's the strange part:

this only occurs when the POT or the photocell is hooked up to its pin... changing the POT changes the brightness of them too. After this some time, they shut off and the logic will work if I hook up V+.

No schematic = no help. Schematic = help.

We can't read your mind (well, I can't). If you want help, post a schematic.

Sorry... That would help...

Ill post one ASAP (within the next 2 hours hopefully)

Side note, I'm 99% sure it's some form of feedback

Power noise could cause strange analog readings. I would recommend adding a 0.1 uF ceramic decoupling capacitor between VCC and GND, if you haven't already.

hmmm... perhaps.

Here is the schematic (sorry for the delay, I just got home).

What seems to be happening is that some internal part of the chip is storing a charge and thus carrying out the program when VCC is removed... I'm going to try reprogramming them for the heck of it... but it's definitely something with the chip.

roomcontrol.pdf (20.4 KB)

You have power leaking in via the A2 and A3 pins. Your power switch sw1 should be located before the LDR and POT at your power source.

Yes, but wouldn't that just prevent those inputs from working? The program begins running when the chip is powered on via VCC

I'm sorry, as a complete newbie at this I don't know what you mean...

The specs are:

ATtiny85-20PU

Package:

PDIP-8

The items that are on the battery side of the power switch are causing the problem. You will have more that enough tome for the Arduino to wake up.. If Not then rewire those inputs so that they pull down a processor pin that is powered up when the Arduino is. The bias path for the Arduino IS the stuff that isn't switched off when the Arduino is.

Doc

But im just using the arduino as a power source. Just the +-5v (it's not doing any programming)

mrroboto20:

[quote author=Coding Badly link=topic=162245.msg1212685#msg1212685 date=1366695633]
Which core are you using?

I'm sorry, as a complete newbie at this I don't know what you mean...[/quote]

It no longer matters. You have the solution to the problem.

So its because of using the arduino as a power source? or because of pins A2 and A3? I'm sorry, I'm not seeing something obviously...

Docedison:
The items that are on the battery side of the power switch are causing the problem. You will have more that enough tome for the Arduino to wake up.. If Not then rewire those inputs so that they pull down a processor pin that is powered up when the Arduino is. The bias path for the Arduino IS the stuff that isn't switched off when the Arduino is.

Doc

so, if I understand you right, the arduino is discharging through my chips when disconnected?

When the processor is not correctly powered, electricity can "leak through". In addition to causing the problem you are having, there is a high probability that it will damage the processor. Move SW1.

Move SW1 (see attached.)

OH MY GOODNESS

that is as plain as day. duh. big duh. I will test ASAP (i'm swamped with other homework at the moment)

but thank-you everyone. I cannot believe I didn't catch that.

will post back tomorrow!

The items that are on the battery side of the power switch are causing the problem.

The simple solution is to move them to the other switch connection point so they are also operated by the power switch.