[solved] ATtiny my night light, can't find the bug.

Hello folks,
I am new to programming and the arduino, as my first project I wanted to build a small night light, which turned out fine using a Duemilanove, I now want to shrink the night light, using a ATtiny 85. I am using the arduino as a programmer and sketches like blink and fade work just fine, my project however shows nothing, not even the LED turns on and I really don’t know why, I got a CR 2032 and the Duemilanove as a power source, both work with blink and fade.

The code:

 int LDR = 2;  // I also tryed 1 and 3
 int LEDO = 0;
 float fade = 255;
 int off = 0;
 
 
 void setup() {
   pinMode(LDR, INPUT);
   pinMode(LEDO, OUTPUT);
 }
  void loop() {
   int v = analogRead(LDR);

// turns the LED off when their is light  
   if (v > 100) {
     fade = LOW;
     analogWrite(LEDO,fade);
     off = 0;
  
// resets the dimming when the light turns on and off  
   }
   else {
     if (off == 0) {
     fade = 255 ;
     off = 1;

     }

// stops the dimming going into - 
     else {
            if ( fade <= 0.5){
       off = 1;
       fade = 0;
       analogWrite(LEDO,fade);

   // slowly fades the LED
   }
       else {
       delay(100);
     analogWrite(LEDO,fade);
     fade = fade-0.1;

   }
   }
   }
  }

I hope this makes some sense, like I said the code works just fine with a real arduino, but I will gladly take any advise !
Can somone help me to port this onto the ATtiny, or at leat tell me what I am doing wrong ?
I added a picture of my setup !

Thank You !

Depends, what clock speed are the fuse bits set to? Which attiny software core are you using?

I burned the bootloader, and selected 8 MHz clock, using the "https://github.com/damellis/attiny" files, or did you mean something else with software core ?

Thank you for the help !

The LDR is connected to pin PB2 on the ATtiny85. This corresponds to analog input A1.

You should try:

int LDR = A1;

link=topic=269800.msg1901815#msg1901815:
The LDR is connected to pin PB2 on the ATtiny85. This corresponds to analog input A1.

No wonder I was not getting anywhere, Thank You !

Unfortunately my trouble is not over, I had some trouble with the reading of the Photoresistor, so I changed the wiring.
I also used a smaller sketch, to just test basic functionality, I will move back to the full one when everything else is working.

Now the Photoresistor works, the threshold is really weird but, it turn the LED on and off, hoever the LED itself is really dim, barley visible at all, I had to use a lower power LED, to even see that it is working, without darkening my complete room.
This did not happen with the fade sketch, which worked quite nicely, can anyone tell me why it is doing this now ?

New sketch:

 int LDR = A1;
 int LEDO = 0;
   void setup() {
   pinMode(LDR, INPUT);
   pinMode(LEDO, OUTPUT);

 }
 
 void loop() 
 {
  
  if (analogRead(LDR) > 50) 
        {
    
     analogWrite(LEDO,LOW);
        }
   else       {
      
     analogWrite(LEDO,HIGH);
        
              }
 }

analogWrite(LEDO,HIGH);

"HIGH" is defined as "1".

fungus: analogWrite(LEDO,HIGH);

"HIGH" is defined as "1".

Thank You ! This makes me feel really stupid now ! The LED works like a charm, could be a little brighter with 3 Volts, but that's something I can worry about later. I also changed the resistor from 10 to 22 K Ohm, which fixes the weird threshold. I hope I will get the full program working now!

A big Thank You to fungus and hiduino, everything is working now !

I managed to stuff everything in a Kinder Surprise.

The 22k Ohm resistor and a threshold of 25 work great, even inside the egg, I did no further changes to the sketch.

This was my first project and I am really happy that it worked out so well, the next one is sure to follow !

Rivet:
A big Thank You to fungus and hiduino, everything is working now !

I managed to stuff everything in a Kinder Surprise.

That’s a good idea.

It would be cool if you put in an RGB led and a shake sensor.

Shake it to get 10 minutes of RGB patterns, gradually fading.

(I might have to do that…)

Dunno what the battery life would be. I guess it depends mostly on LED brightness.

I was considering a shake sensor to reset the fade value, so you can grab the light at night and turn it on. I also considered RGB LEDs but this would be more suitable for an ambient light, which you could build a little bigger and with a diffuser. Like a small box, with a couple of buttons to change color behavior and fade on off/time and different sensors, maybe one that reacts to noise, I will probably build one, but now I have to go flat hunting, I am moving to a new city to study.

Please share your project, if you decide to build a night light as well !

You could maybe use something like this connected to the Tiny85's "wake up" pin...

http://www.ebay.com/itm/221561270799

Now you can send the Tiny85 to deep sleep and not use any battery until somebody shakes it.

I have not considered any power consumption at all till now and I have really no idea how long my current project is going to last, but a deepsleep option sounds good and the little shake switch looks simple and cheap enough, will probably order some after I have moved.

Rivet: I have not considered any power consumption at all till now and I have really no idea how long my current project is going to last

Without a sleep mode? You'll probably need a new battery every night...

A dammit, never thought of it while building. I will have to read into this fast, my first prototype will probably not last really long now :( I just thought small script, small mc, only lights up for a couple of minutes a day, will probably not eat much power. Can you recommend any good power consumption guides ?

Without sleep mode, with minimal features enabled, a Tiny85 will probably use about a milliamp when there's no LEDs lit up.

Wikipedia says a CR2032 has a life of 225mA hours, I didn't know it was that much.

So ... in theory it could last about a week if the LEDs are only on for a few minutes a day.

Could I have just used the photoresistor to wake up the arduino ?

A Week would still be bad, I want to use them as small presents, so the longer lasting they are, the better, unfortunately I this will have to wait, no use having a working night light but no roof over my head.

Rivet: Could I have just used the photoresistor to wake up the arduino ?

Photoresistors use power as well.

You could maybe put the Tiny85 to sleep then wake up once in a while to check the photoresistor. Power the photoresistor off a Tiny85 pin so you can switch it on/off.

1 pin - analog read of light level. 1 pin - LED 1 pin - Photoresistor power

It will need some fiddly programming but it can be done.

I was just reading through this: https://www.adafruit.com/datasheets/maxell_cr2032_datasheet.pdf

If you look at "Relationship between Discharge Current and Discharge Capacity" you'll see a problem - CR2032s don't have much current capacity. (I knew there was a reason I wasn't using them in my projects)

The maximum is about 3mA (and if you run it a 3mA continuously you severely reduce the battery life).

If the Tiny85 needs about 1mA that only leaves 2mA for your LED. Still plenty of light in a dark place but you have to plan for that.

It really is a little dim, looks way better over 5 Volt USB Power, I also tried it with two CR2032, but for some reason I don't get any dimming or turning off, so it somehow messes up my program, 6 Volts may be to much.

Maybe 3 AAA are a better solution, but I wont get that into my little egg anymore :(

You can try this lithium Ion coin cell, LIR2450/CR2450. This should pack a little more juice.

Coin cell: https://www.adafruit.com/products/1572 Charger: https://www.adafruit.com/products/1573

Cell Holder: https://www.sparkfun.com/products/8863 BO Board: https://www.sparkfun.com/products/10495