Pages: [1] 2   Go Down
Author Topic: Help with shrinking program on to Tiny85  (Read 1452 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I need a little help.

I have written a simple little sketch involving 3 LED's and A Push Button.

It works fine on the Bread Board hooked up to the Arduino UNO. But when I put the code on a atTiny85 it wont run.  I know I am programming the chip right. I have tested it with the Blink sketch.

I think it has something to do with the way I have it set up. I noticed pin 2 on the Arduino when sent as Analog input give out about 2 vdc,    Pin 2(3) on the Tiny85 puts out 5.  Not sure if I am on the right track with that.  all so please keep in mind I am very new to all this, just over a week old.

Attached is my code.

Thanks

Code:
/*
 *  Night Lite, final version
 */

int switchPin = 3;              // switch is connected to pin 3 (pin 2 on Arduino)  
int led1Pin = 0;                // Blue LED on Pin 8 on Arduino
int led2Pin = 1;                // Green LED on Pin 9 on Arduino
int led3Pin = 2;               // Red LED on Pin 10  on Arduino

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

  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);


  Serial.begin(9600);           // Set up serial communication at 9600bps
  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));

    }
  }
}







Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Just to check your connections,
int switchPin = 3;              // leg 3 on the ATTiny85
int led1Pin = 0;                // leg 5
int led2Pin = 1;                // leg 6
int led3Pin = 2;               // leg 7 but it's NOT PWM capable, so your analogWrites won't work; suggest you move this one to leg 2 or 3 (D3 or D4 if you still need to analogWrite)

Cheers ! Geoff

Cheers ! Geoff
Logged

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The image I have shows pin 3 on leg two.

Leg 1 Reset
Leg 2 PIN 3( Analog input 3)
Leg 3 PIN 4( Analog input 2)
Leg 4 GND
Leg 5 PIN 0 (PWM)
Leg 6 PIN 3 (PWM)
Leg 7 PIN 2 (Analog Input 1)
Leg 8 VCC





int switchPin = 3;              // leg 3 on the ATTiny85



* 85 Sketch_schem.jpg (26.53 KB, 510x378 - viewed 22 times.)
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry that was a typo. You are correct.
Logged

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,

did you take a look at my lay out ?
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just now, yes.

So the PWM remark still stands. Suggest using the internal pull-up on your switch pin to simplify, and swap switch and LED pins so you get PWM for analog write on that 3rd LED.

In around an hour if you like I'll be able to load this up on an ATTiny85 if you don't find what's breaking it first.

Cheers! Geoff
Logged

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

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to lose this too...
Code:
  Serial.begin(9600);           // Set up serial communication at 9600bps

Cheers! Geoff
Logged

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That would be great if you could do that,


In around an hour if you like I'll be able to load this up on an ATTiny85 if you don't find what's breaking it first.

Cheers! Geoff
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure how to do this. To tell you the truth I am not even sure what it means  smiley-red



 Suggest using the internal pull-up on your switch pin to simplify,

Cheers! Geoff
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No need to do that to fix anything, but a suggestion.  Simply setMode to output as you would, but then digitalWrite that pin to high.  Wire the pin to yor switch then ground.  When not pressed the pin will digitalread high, and when pressed it will be low.  But it means your switch circuit will be much simplified.

Regardless it is working the way you have it so I think just move LED3 to a pin that supports PWM, and lose the initiation of Serial...and i think it should run as it did for you on the arduino.

Geoff
Logged

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



There are only 2 PWM pins  PIN 0 (5)
and PIN 1 (6)
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I removed this line, and reprogrammed the 85 and it working some what.

But if your still willing to take a shot at I would deeply appreciate it.

You need to lose this too...
Code:
  Serial.begin(9600);           // Set up serial communication at 9600bps

Cheers! Geoff
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



There are only 2 PWM pins  PIN 0 (5)
and PIN 1 (6)
This is a common misconception.  ATTiny85 has 4 pins capable of PWM, with two sharing the same clock source.  I routinely use this little uC to run RGB LEDs.

Sorry still stuck on this iPad or would post an example.

Also, it is possible to drive any of the pins by software PWM if the application is lighting or anything else where accuracy or high frequencies aren't a requirement.

Cheers! Geoff
« Last Edit: January 02, 2013, 12:10:46 am by strykeroz » Logged

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

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am  sorry if I am being a pain, but can you list the 4 PWM pins.

I am still learning and you are helping me a lot. I have a long way to tho...

Thanks


Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not a problem at all.  Sorry I type better on my PC too smiley

Checkout the pins on this diagram http://www.akafugu.jp/images/microcontroller-reference-sheet.png

It is only those two PWM pins on ATTiny13.

Cheers! Geoff
Logged

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

Pages: [1] 2   Go Up
Jump to: