Help with Toggle Code

I have purchased a Digispark for my project. I am wanting to use it to toggle a LED strip on and off with a camera command from a quadcopter. The LEDs are wrapped around my quad and I wanted a remote control for them.

The quadcopter outputs a 3.3V signal all the time on the camera command leg. When the transmitter button is pressed the quad will break this voltage go to zero volts then return to a constant 3.3.

I want the Digispark to toggle a transistor to turn the LED strip on and off but I'm having trouble getting a code to read the voltage and output the correct signal to the transistor.

I have the quad signal on P0 and the Transistor gate on P1. The Digispark is powered by a USB connector I soldered together on the bottom of the quadcopter.

Any thoughts? Thanks for any help!

What is a Digispark?

Post a link to its datasheet.


Here's a link to the manufacturer's page. Its a very small board.

Also a wiki link.

byte pinIn = x; // insert your pin here
byte pinOut = y; // insert your pin here
byte toggleState = 0;
void setup(){
pinMode (pinIn, INPUT_PULLUP);
pinMode (pinOut, OUTPUT);
void loop(){
  if (digitalRead(pinIn) == LOW){
  toggeState = 1 - toggleState; // 1-0-1-0-1-0-1 ...
  digitalWrite (pinOut, toggleState);
  delay (100); // give pinIn time to back  high

I’ll try that after work today. Thanks very much for the help!

Can also toggle an output by:

digitalWrite(myOutputPin, !digitalRead(myOutputPin);


Wow, that looks slow! Good enough for toggling an LED strip tho.

LarryD, how do I set my clock? I forgot what the Red & Black buttons do, and got the time hosed trying to set for the daylight savings time change. Which is now coming up again ....

PM instructions sent.

On the UNO:

digitalWrite(13, !digitalRead(13);
Takes ~8us <<<<<<<<<<<<<< :frowning:

PINB = 0x20; // Toggles pin D13
Takes ~70ns <<<<<<<<<<<<<< :slight_smile:

PINB |= _BV(PINB5); // Toggles pin D13
Takes ~130ns

PORTB ^= B00100000; // Toggles pin D13
Takes ~ 130ns

Thanks CrossRoads! Your code worked great. I get a blink between each toggle for some reason but that's fine for the application.

My solder joints were loose on my output pin yesterday. That was driving me crazy because it would flash on and off on its own sometimes. Bit its working now. I appreciate the help.

8 uS (128 clock cycles) vs 1 and 2 clock cycles. That's a big difference! The code is doing a lot more checking of the hardware state and stuff as part of that.

You get a blink between toggles? I wonder why.

I don't know. It blinks about 3 times coming on and going off. It's weird but I'm glad they do what I wanted!