Help with shrinking program on to Tiny85
Moderators: adafruit_support_bill, adafruit

Help with shrinking program on to Tiny85

by kculm on Tue Jan 01, 2013 11:54 pm

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 and layout

Thanks
Code: Select all | TOGGLE FULL SIZE
/*
 *  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));

    }
  }
}







Attachments
85 Sketch_schem.jpg
85 Sketch_schem.jpg (26.53 KiB) Viewed 611 times
kculm
 
Posts: 35
Joined: Sun Dec 23, 2012 7:34 pm

Re: Help with shrinking program on to Tiny85

by kewakl on Wed Jan 02, 2013 7:36 am

Is pin 1(PB5/RESET) floating?
Try a pullup. Maybe 10K.

We are unable to confirm that this post is not spam - please complete the captcha below and re-submit your post.
Just lovely. Even when LOGGED IN!
kewakl
 
Posts: 11
Joined: Fri Jul 20, 2012 6:49 pm

Re: Help with shrinking program on to Tiny85

by adafruit_support_bill on Wed Jan 02, 2013 7:44 am

lovely. Even when LOGGED IN!

Unfortunately, even spam-bots know how to log in. :(
User avatar
adafruit_support_bill
 
Posts: 30675
Joined: Sat Feb 07, 2009 10:11 am

Re: Help with shrinking program on to Tiny85

by adafruit on Wed Jan 02, 2013 8:17 am

kewakl wrote:We are unable to confirm that this post is not spam - please complete the captcha below and re-submit your post.[/color] Just lovely. Even when LOGGED IN!


if you're an adafruit customer and your email is associated with your forum account you will not see the captcha.

thanks,
adafruit support, phil
User avatar
adafruit
 
Posts: 11704
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Help with shrinking program on to Tiny85

by tromo on Sat Jan 05, 2013 8:10 pm

I'm suspicious of the Serial.begin() statement. The tiny85 has a USI, not a USART, and I don't think arduino supports hardware serial on it because of that. I'd try removing it and see if that makes a difference.

One thing I find helpful is to flash the LEDs during setup so I know the code is running (or I know what phase of startup I'm in if it's complex). You could do a quick ramp up of intensity as a power-on test to verify the LEDs are working as is PWM on those pins...
tromo
 
Posts: 23
Joined: Fri Jul 08, 2011 11:29 pm
Location: Upstate NY

Re: Help with shrinking program on to Tiny85

by jigsawnz on Sun Jan 06, 2013 5:20 am

What about this. I changed the code from if/else statements into a "switch case". It works the same but is easier to work with in your case I think. The pin change uses interrupt so it will register a button change while it is in the middle of processing something else. The random function now has a seed from a potentiometer on pin 4. check http://arduino.cc/en/Reference/random for more details.

hope it helps,

George

Code: Select all | TOGGLE FULL SIZE
// this constant won't change:
const int led1Pin = 0;   // Blue LED on Pin 8 on Arduino
const int led2Pin = 1;   // Green LED on Pin 9 on Arduino
const int led3Pin = 2;   // Red LED on Pin 10  on Arduino
const int buttonPin = 3; // the pin that the pushbutton is attached to
const int potPin = 4;    // the pin that the potentiometer is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
 
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, LOW);
 
  randomSeed(analogRead(potPin));
 
  attachInterrupt(buttonPin, stateChange, CHANGE);
}

void loop() { 
  // do something different depending on the
  // buttonPushCounter:
  switch (buttonPushCounter) {
  case 0:   
    sequence0();
    break;
  case 1:   
    sequence1();
    break;
  case 2:   
    sequence2();
    break;
  case 3:   
    sequence3();
    break;
  case 4:   
    sequence4();
    break;
  }
}
//---------------------------------------------------------------------------
void stateChange()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH)
      buttonPushCounter++;
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
   
  if (buttonPushCounter > 4)
    buttonPushCounter = 0;
}
//---------------------------------------------------------------------------
void sequence0()
{
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, LOW);
}

void sequence1()
{
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led1Pin, LOW);
}

void sequence2()
{
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led1Pin, LOW);
}

void sequence3()
{
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, HIGH);
}

void sequence4()
{
  analogWrite(led1Pin, random(135, 255));
  analogWrite(led2Pin, random(135, 255));
  analogWrite(led3Pin, random(135, 255));
  delay(random(100));
}
A person only needs two things in life. Some CRC/WD40 to make things go and some duct tape to make things stop.
User avatar
jigsawnz
 
Posts: 108
Joined: Mon Mar 12, 2012 10:17 pm
Location: New Zealand