Digispark click code help

I have working code for my digispark clone, the attiny85. I would like for the code to make clicks act like a mouse where when the key I have soldered to the attiny85 is pressed, it acts like a mouse being pressed down and would hold the click and when released it would stop holding left click. I have the 2 pins of the button soldered to the pin 2 and ground pin of my board. I am using the DigiMouse.h library. This is the link to the schematic of the attiny85. Here is my code

#include <DigiMouse.h>

void setup() {
  DigiMouse.begin();
  pinMode(2, INPUT_PULLUP);
  DigiMouse.begin();
}

void loop() {

  if(digitalRead(2)==LOW){ 
    DigiMouse.setButtons(1<<0);
    DigiMouse.delay(500);
    DigiMouse.setButtons(0); //unclick all
    DigiMouse.delay(500);
  }
}

1<<0 ?

I found it on a separate code that has a click in it. Here is the link. That is the code for left click.

See the File->Examples->02.Digital->StateChangeDetection example. It shows how to use a global variable to keep track of the previous state of the button. That way you can tell when the button state changes.

You will end up with something like:

#include <DigiMouse.h>

const byte ButtonPin = 2;
bool ButtonWasPressed = false;     // previous state of the button

void setup()
{
  DigiMouse.begin();
  pinMode(ButtonPin, INPUT_PULLUP);
}

void loop()
{
  bool buttonIsPressed = digitalRead(ButtonPin) == LOW;

  if (buttinIsPressed != ButtonWasPressed)
  {
    // Button state has changed
    ButtonWasPressed = buttonIsPressed;

    if (buttonIsPressed)
      DigiMouse.setButtons(1 << 0);  // Click button 0
    else
      DigiMouse.setButtons(0); //unclick all
  }
}

Thank you for the reply! I did get an error when I ran this code though. It was on the line that reads

  if (buttinIsPressed != ButtonWasPressed)

The error reads "'buttonPressed' was not declared in this scope". I'm not great with code if you could try and help me again I would greatly appreciate it.

No, the error reads "'buttinIsPressed' was not declared". That means "I don't recognize this name. " If you compare the spelling with the declaration:
bool buttonIsPressed =
You will see that I typed "buttinIsPressed" one place where I should have typed "buttonIsPressed" to match the declaration. In this case, you can tell the "buttonIsPressed" is the right spelling because it is used three times where "buttinIsPressed" is used only once.

This code is not working. The only thing I can tell you that happens when I plug it in is I get a windows notification that says "USB device not recognized".

Sounds like something is wrong with the digispark clone (bad firmware?) Is there an example with the DigiMouse library that you could try?

I have 4 of them and they work with the first code I sent and others I've tried. I followed this. The only thing that is different is my programmer is micronucleus, There is no option to change that setting for me.

Did you follow this instruction on that page?

Install Arduino 1.8.5, not any of the newer versions as they have known connectivity bugs.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.