Go Down

Topic: Help with shrinking program on to Tiny85 (Read 2 times) previous topic - next topic

strykeroz

#20
Jan 02, 2013, 07:29 am Last Edit: Jan 02, 2013, 07:34 am by strykeroz Reason: 1
That's excellent news.

Confirming the changes my side were removing the Serial.begin() and these pin assignments
Code: [Select]
int switchPin = 3;              // IC leg 2 (PB3)
int led1Pin = 0;                // IC leg 5 (PB0)
int led2Pin = 1;                // IC leg 6 (PB1)
int led3Pin = 4;                // IC leg 3 (PB4)


Also, here's how it would look modified to use the internal pull-up resistor on the switch (as mentioned earlier).  For this circuit you'd not have any additional resistors on the switch circuit, just wire to the switch and from there to ground.
Code: [Select]
/*
*  Night Lite, final version
*/

int switchPin = 3;              // IC leg 2 (PB3)
int led1Pin = 0;                // IC leg 5 (PB0)
int led2Pin = 1;                // IC leg 6 (PB1)
int led3Pin = 4;                // IC leg 3 (PB4)

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state

int lightMode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  digitalWrite(switchPin, HIGH);  // set internal pull-up resistor
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);


  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        }
        else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          }
          else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            }
            else {
              if (lightMode == 3) { //  if its waving,
                lightMode = 4;
              }
              else{
                if (lightMode == 4){
                  lightMode =0;
                } // turn light off!
              }
            }
          }
        }
      }
      buttonState = val;                 // save the new state in our variable
    }

    // Now do whatever the lightMode indicates
    if (lightMode == 0) { // all-off
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, LOW);
      digitalWrite(led3Pin, LOW);

    }

    if (lightMode == 1) { // Blue On
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, LOW);
      digitalWrite(led3Pin, LOW);
    }

    if (lightMode == 2) { // Blue off, Green On
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, HIGH);
      digitalWrite(led1Pin, LOW);

    }
    if (lightMode == 3)  { // Red On Green Off
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, LOW);
      digitalWrite(led3Pin, HIGH);
    }   
    if (lightMode == 4) { // Flame
      analogWrite(led1Pin, random(120)+135);
      analogWrite(led2Pin, random(120)+135);
      analogWrite(led3Pin, random(120)+135);
      delay(random(100));

    }
  }
}


Cheers  ! Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

kculm

When I removed the Serial.begin() that's when thing started to work. Can you explain why.

Again sorry, but this is how I learn.

Thanks.

strykeroz

#22
Jan 02, 2013, 07:42 am Last Edit: Jan 02, 2013, 07:45 am by strykeroz Reason: 1
No need for any apology.  

It's simply that Serial is a set of functions that talk to the RX and TX pins via the inbuilt UART on the ATMega.  There is no hardware serial support on the ATTiny85 so that function cannot work.  

There is a software serial available for the Arduino-Tiny library if I recall correctly so the functionality can be achieved another way if you need it.

Cheers ! Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

kculm

Thanks to all your help,  I got it working and leaned a lot in the process.

Now one more thing I would like to ask. 

Because this is going to be a Night Lite I don't want to use batters. I don't know how log they would last.

That being said what would be the right size power supply.

Thanks

strykeroz

Hi

Glad to hear it's all working now your side.

Depending on the particular ATTiny85 you're using there's a broad range of voltages which will be suitable.
Quote from: ATTiny85 datasheet http://www.atmel.com/Images/doc2586.pdf
Operating Voltage
- 1.8 - 5.5V for ATtiny25V/45V/85V
- 2.7 - 5.5V for ATtiny25/45/85
Any wall wart in the 3 to 5 Volt range will be a safe bet (and somewhat cheap, if you don't have one laying about from an old mobile phone or similar to repurpose).

Just remember if you use something lower than 5V you will need to adjust the size of the current limiting resistors you use for your LEDs down or they'll be dimmer (maybe not a bad thing for a night light, but best to test first).  LEDcalc.com can help with the maths.

All the best,
Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Go Up