Trinket MO: How to code rocker switch in Arduino

This is a special forum devoted to educators using Adafruit and Arduino products for teaching.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
zackboston
 
Posts: 129
Joined: Mon Feb 25, 2013 12:47 pm

Trinket MO: How to code rocker switch in Arduino

Post by zackboston »

I am doing a project with edge lit acrylic designs with my junior exploring computer science students at Boston's Madison Park Technical Vocational High School. I usually use a regular trinket for this project, but this time, I decided to try a Trinket MO.

Well, I got the blinkie sketch on the Trinket MO working and I got a simple one color neopixel strip code to upload just fine.

But now I am stuck on adding the rocker switch code.
The Adafruit tutorial on using Arduino says I have to use pinMode (RockerSwitchPIN, INPUT_PULLUP) in the void setup (). So I did that.

I used Switch = digitalRead(RockerSwitchPIN) & an if statement with if (Switch = 1) {}

This doesn't work. I checked that the switch is on pin 5 and grounded just fine.

What am I doing wrong? I've attached a copy of the code I am using.

Thank you for any help you can give me. . .
Attachments
RockerSwitchCode.png
RockerSwitchCode.png (233.11 KiB) Viewed 1621 times

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Trinket MO: How to code rocker switch in Arduino

Post by adafruit_support_bill »

With the Switch pin mode set to INPUT_PULLUP, the input will be pulled HIGH (1) by default when the switch is open.

The rocker switch should be connected between your Switch pin and GND. When you close the switch, the pin will be pulled LOW (0). So you should change your code to:

Code: Select all

if (Switch == 0)
Also note that I used '==', not '='. '=' is the assignment operator. '==' is the equality operator.

User avatar
zackboston
 
Posts: 129
Joined: Mon Feb 25, 2013 12:47 pm

Re: Trinket MO: How to code rocker switch in Arduino

Post by zackboston »

Thank you. . . I will try when I get to school!!!

I did try the == but I think I might have mis-typed the rocker switch pin number on that iteration of trying the code! . . .

User avatar
zackboston
 
Posts: 129
Joined: Mon Feb 25, 2013 12:47 pm

Re: Trinket MO: How to code rocker switch in Arduino

Post by zackboston »

OK. It still doesn't work.

I tried using two if statements Switch == 0 and Switch ==1 to see what is going on.

I attached code. It must be some stupid simple bug. . .
Attachments
Screen Shot 2022-02-18 at 6.06.28 AM.png
Screen Shot 2022-02-18 at 6.06.28 AM.png (280.01 KiB) Viewed 1619 times

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Trinket MO: How to code rocker switch in Arduino

Post by Franklin97355 »

You might get more info if you print the value of Switch before each if statement to see if it is what you expect.

User avatar
zackboston
 
Posts: 129
Joined: Mon Feb 25, 2013 12:47 pm

Re: Trinket MO: How to code rocker switch in Arduino

Post by zackboston »

Oh Franklin, thank YOU!

Putting in a Serial.print was SOOOO HELPFUL and solved the problem!!!!

Success!!! I don't know why the Trinket MO confounded me so much. . . I've been using regular trinkets for years!!!

Locked
Please be positive and constructive with your questions and comments.

Return to “For Educators”