ATTINY85 and button press using if/then

I’m trying to figure out how to make an if/then statement react to a button press on an digispark attiny85, and don’t know what I’m doing wrong.

Why will the following code not do what I want?
I have LED and a resistor from P0 to 5v and a button from 5v to P3 (A3 input).

Thanks!

int LEDPin = 0;          //assigns LED pin to 0 (P0)
const int buttonPin = A3; // assigns pushbutton to pin A3 (P3)
int buttonState = 0;     // variable for reading the pushbutton status

void setup()
{
  pinMode(LEDPin, OUTPUT);  //sets LEDPin to output
  pinMode(buttonPin, INPUT);  //sets buttonPin to input
}
void loop()
{
  buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
  if (buttonState == HIGH) {
    digitalWrite(LEDPin, HIGH);
  }  else {
    digitalWrite(LEDPin, LOW);  // turn LED output off:
  }
}

I’ve even tried using just a jumper from gnd or 5v (instead of a pushbutton) with no success.

 pinMode (buttonPin, INPUT);  //sets buttonPin to input

should be

 pinMode (buttonPin, INPUT_PULLUP);  //sets buttonPin to input

I copied the code from a sketch I had running on a nano, and it worked fine there.

Why should it be INPUT_PULLUP and why would it run on a nano but not an attiny85?

Thank you!

Why should it be INPUT_PULLUP and why would it run on a nano but not an attiny85?

Because floating pins behave erratically, there is no way of saying if it works on anyone's nano, just yours that one time with your setup.
See Inputs

With a pullup you'll want to change the switch wiring to take the pin to ground, not 5V.

And then in the logic, pressed will be low, unpressed high.

Thank you, again.

But, it didn’t work. Now, after 5seconds of boot/wait, the LED comes on. The button does nothing, even if I tie it to gnd instead of 5v. I’ve attached a pic of the setup, just in case I’m missing some detail.

EDIT
Yes, I don’t rightly care if it’s high or low for now, just to get it working one way. I can mod it for what I need after. This bit of code is going into a PWM controller i"m trying to build, and I have pullups on the breadboard, as I’m not proficient with coding that sort of thing just yet. I was going to code in the pullups after I had it working in the breadboard, first.

(and, yes, I *now know that digispark didn’t release a ver3. this is a clone, apparently)

An attiny85 and a digispark aren’t the same thing.

Stick a serial print in there to print out buttonState, let's see if it's even being read.

I've never used one of those processors: you sure the pin designation is correct?

I've attached a pic of the setup, just in case I'm missing some detail.

Yep. You seem to have connected the button between 5V and the input pin. That way it will always read HIGH, you need to connect it between the input pin and ground.

Now, after 5seconds of boot/wait, the LED comes on.

That suggests that the pin you are looking at is still floating.

Stick a serial print in there to print out buttonState,

No UART in the ATTINY85 so serial print does not work.

%99 sure about the pin assignments. The pin assignments, output, and input are kinda weird. there is a page with some info here

No, the attiny is not digispark, necessarily, but digispark did put out attiny85 boards with a micronucleus (?) bootloader. I have one setup with a pot and mosfet now, and that is working well. I just need to figure out what I’m missing about programming it some more.

I don’t know how to do serial on this because it doesn’t have uart, but it looks like I’m gonna learn.

I’ll report back in a little bit.

Grumpy_Mike:
No UART in the ATTINY85 so serial print does not work.

Ah- bummer

Well, the only option for serial debug is a 1 wire interface....which I just happen to have laying around for my blheli esc's (atmel based brushless motor speed control for RC hobby).

But when I flash the tiny and hook up the interface all I get in the term window is a nonstop flow of repeating jibberish. I think I had the settings correct, but that's ok, I give up *for now. I have a couple nano's here and I'll just use one of them for my PWM. I wanted to add an IIC LCD, current and voltage sense, and pushbutton up/down control to replace the pot anyway, and I won't be able to do that with the attiny.

For the moment, I'll just put the pushbutton on the mosfet gate and be done with it. At least I'll be able to move forward with the rest of what this is for: heat sealing/wlding ripstop nylon together for a hovercraft skirt! ;D

Thank you, again for all the help. I still have learned much from the (momentary) failure.

Grumpy_Mike:
No UART in the ATTINY85 so serial print does not work.

You can get the functional equivalent...

http://www.ernstc.dk/arduino/tinycom.html

I just programmed a tiny85 and your code (I cleaned it up a bit) works:

#define LEDPin 0  // pin 0 --> LED --> 330ohm --> gnd
#define buttonPin 4  // pin 4 --> button --> gnd
// I used pin 4 instead of 3 so I could put the switch right on the breadboard for clarity

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);
  }

void loop() {
  boolean buttonState = digitalRead(buttonPin);
  digitalWrite(LEDPin, buttonState);
  }

If it doesn’t, then perhaps your “digispark” is broken?

Note that you MUST use INPUT_PULLUP if you wire it this way:

Thank you!

Your code worked, so I looked at mine again. Calling out pin 3 as "A3" is what was crippling my sketch!

now, to incorporate that into my pwm bit...

I'm so confused.

Why do I need to call out pin 2 as "A3" for the potPin but the buttonPin is only "3". They are both inputs, and I had read that inputs on an attiny85 should be called out as "Ax".

(Also, I am now looking into why you used "#define" instead of "int".)

reset a0 5 |o  | Vcc
xtal1 a3 3 |   | 2 a1 scl
xtal2 a2 4 |   | 1
       gnd |   | 0 sda

Use a0, a1, a2 and a3 with analogRead(). Otherwise, use 0, 1, 2, 3 and 4 as shown in the diagram above. Think of the "a" in the pin name to mean "analog". The non-symmetric naming of the pins on the tiny85 is irritating, but since there are so few pins, you get used to it quickly.

Note that pin a0 will only work for analog values from ~300 to 1023. Otherwise the pin is LOW and the processor resets.

BTW, I learned that for analogue input pins, you have to call it out as Ax and for digital input pins you can just call it "X" (where x is a number).

However, the ADC pins and the board pins and the processor pins are not all the same, so it gets confusing as hell.

you have to call it out as Ax and for digital input pins you can just call it "X"

No.

A3 and Pin 3 are two different pins. In fact A3 and Pin 17 are the same pin on a Uno.

So if you want to use the pin A0 as a digital input / output you can use A0 or 14 to set up that one pin.

Grumpy_Mike:
No.

A3 and Pin 3 are two different pins. In fact A3 and Pin 17 are the same pin on a Uno.

So if you want to use the pin A0 as a digital input / output you can use A0 or 14 to set up that one pin.

https://www.arduino.cc/en/Tutorial/AnalogInputPins

In this case though (tiny85) pin 3 and pin A3 are both physical pin 2.