Simple code runs correctly on Arduino Uno, but not attiny85

My code is running properly on an Uno, and I am able to upload it successfully to an attiny85 at 8MHz using arduino as ISP. I get strange behavior with the attiny, however. Normally, when the input goes from low to high, some code should run to turn various pins on/off. Then, when the input goes from high to low, different code should run to do a similar task. Those tasks are within the functions turnon() and turnoff(), respectively.

On the attiny, the code seems to work when going from low to high, but it never reaches the high to low state (turnoff() function). I thought maybe this was a coding issue, but everything works properly with NO changes whatsoever on an Arduino Uno. The input is a toggle switch with a pulldown resistor. I’ve tried the basic troubleshooting things, such as using another attiny, swapping out breadboards/wires/etc, to no avail.

Any suggestions for things to try? Maybe the code needs to be reworked to handle the attiny better, but I thought this would be a simple task for it to do. I’m using an 2 channel oscilloscope to view the output pins.

const int switchPin = 4;    // switch input              (pin 3 on attiny)
const int motor1Pin = 0;   // H-bridge leg 1 (pin 2, 1A) (pin 5 on attiny)
const int motor2Pin = 1;   // H-bridge leg 2 (pin 7, 2A) (pin 6 on attiny)
const int enablePin = 2;   // H-bridge enable pin        (pin 7 on attiny)

int currentState = 0;      //state change detector
int lastState = 0;         //state change detector

// Variables will change for Debouncing:
int SwitchState;             // the current reading from the input pin
int lastSwitchState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers 

void setup() {
  // set the switch as an input:
    pinMode(switchPin, INPUT); 
    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);

    //set enablePin low so that it's off by default:
    digitalWrite(enablePin, LOW);

void loop() {
  int reading = digitalRead(switchPin);
  // If the switch changed, due to noise or pressing:
  if (reading != lastSwitchState) {
    // reset the debouncing timer
    lastDebounceTime = millis();

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
        currentState = digitalRead(switchPin);
        if (currentState!=lastState) { //if the current state is different than the previous
          // if the change is high, valve will toggle in one direction:
          if (currentState == HIGH) { 
            //digitalWrite(enablePin, LOW);
          //if the change is low, valve will toggle in the other direction:
          else {
            //digitalWrite(enablePin, HIGH);
  lastState=currentState; //state detection
  lastSwitchState = reading; //debounce detection

void turnon() {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      digitalWrite(enablePin, HIGH);  //send power
      digitalWrite(enablePin, LOW);   //stop sending power

void turnoff() {
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      digitalWrite(enablePin, HIGH);  //send power
      digitalWrite(enablePin, LOW);  //stop sending power

hbridge_attiny.ino (2.65 KB)

Are you sure it’s not resetting?

Post your schematic. Did you include the 0.1uf decoupling cap between Vcc and Gnd? Are the connections to it nice and short (I’ve had to trashcan a batch of boards -with the tiny85 as a matter of fact - because I screwed up and put the decoupling cap too far away…). On breadboard, you want to have the decoupling cap straddling the chip, not off in the corner somewhere where it won’t do any good.

I'm not sure if it's resetting or not, that seems like a possibility. I tried using a cap between Vcc and Gnd, but when I do, the attiny doesn't do anything at all. I didn't have a .1uf cap handy at the time, so I was using a 1uf instead, which might be the problem.

EDIT: I tried using the proper .1uf size and it seemed to make things worse. The attiny wouldn't even run the code properly in the direction (low to high) that was working previously.