0

ITSY BITSY 32U4 3.3V 8MHZ
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

ITSY BITSY 32U4 3.3V 8MHZ

by neilvdm on Mon Sep 09, 2019 2:29 am

Hello
Im busy with a project where Im using Neopixel strip, ITSY BITSY 32U4 and a mini Reed switch, and I need some help with the coding as I am still VERY new to this.
What I want to do is say for example:
if the switch is open, then LED 0 - 25 is white, if the switch is closed then LED 0 - 25 turns green.
I can figure out how the get the LED going, but what I have no idea about is how to do an IF, based on the status of a switch

What would the best way to code this be?
Thank you

neilvdm
 
Posts: 4
Joined: Tue Aug 20, 2019 5:09 am

Re: ITSY BITSY 32U4 3.3V 8MHZ

by adafruit_support_mike on Mon Sep 09, 2019 2:52 am

That kind of thing is covered in our Learn Arduino series of tutorials:

https://learn.adafruit.com/adafruit-ard ... tal-inputs

The general idea is to connect one of the microcontroller's input pins to a known voltage through a resistor, then use the switch to connect the pin to a different voltage. Then the microcontroller can tell whether the switch is open or closed by reading the voltage on that pin.

adafruit_support_mike
 
Posts: 58856
Joined: Thu Feb 11, 2010 2:51 pm

Re: ITSY BITSY 32U4 3.3V 8MHZ

by neilvdm on Wed Sep 11, 2019 2:24 am

Mike, based on your referral link, I played around with my design and coding and the closest i can get it to run is that if I press button A, the "else" statement kicks in, meaning 15 LEDs turn green.
What am I doing wrong?

PLEASE guys, I really need your help. Im completely new to coding and trying my best to understand the coding structure

This is the code:
Code: Select all | TOGGLE FULL SIZE
// A basic everyday NeoPixel strip test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>

// definitions
#define DATA_PIN 6
#define LED_COUNT 60


Adafruit_NeoPixel strip(LED_COUNT, DATA_PIN, NEO_GRB + NEO_KHZ800);

uint32_t green = strip.Color(0, 255, 0);
uint32_t white = strip.Color(255, 255, 255);
uint32_t red = strip.Color(255, 0, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t yellow = strip.Color(255, 255, 0);
           


int buttonApin = 9;
int buttonBpin = 8;

 
void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  strip.begin();
  strip.clear(); // Initialize all pixels to 'off'
  pinMode(buttonApin, INPUT_PULLUP); 
  pinMode(buttonBpin, INPUT_PULLUP); 

}

void  loop () {
if (digitalRead(buttonApin) == HIGH)
  //from here on wards I dont know how would I initiate strip.fill if the switch is HIGH

strip.fill(red, 0, 15);


  // and from here on wards I dont know how would I initiate strip.fill if the switch is LOW in the else statement
else {                                                                                                   
strip.fill(green, 0, 15);
strip.show();

}
}


2019-09-11_9-13-51.png
2019-09-11_9-13-51.png (139.76 KiB) Viewed 33 times

neilvdm
 
Posts: 4
Joined: Tue Aug 20, 2019 5:09 am

Re: ITSY BITSY 32U4 3.3V 8MHZ

by neilvdm on Wed Sep 11, 2019 2:48 am

I think I have it running :)
Now to get this working with reed switch. At least I have a better understanding of the coding structure

Code: Select all | TOGGLE FULL SIZE
// A basic everyday NeoPixel strip test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>

// definitions
#define DATA_PIN 6
#define LED_COUNT 60


Adafruit_NeoPixel strip(LED_COUNT, DATA_PIN, NEO_GRB + NEO_KHZ800);

uint32_t green = strip.Color(0, 255, 0);
uint32_t white = strip.Color(255, 255, 255);
uint32_t red = strip.Color(255, 0, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t yellow = strip.Color(255, 255, 0);
                // variable to store the value

int buttonApin = 9;
int buttonBpin = 8;

 
void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  strip.begin();
  strip.clear(); // Initialize all pixels to 'off'
 
  pinMode(buttonApin, INPUT_PULLUP); 
  pinMode(buttonBpin, INPUT_PULLUP); 

}

void  loop () {
if (digitalRead(buttonApin) == LOW)
  //from here onwards I dont know how would I initiate strip.fill if the switch is HIGH

strip.fill(green, 0, 15);

  // and from here onwards I dont know how would I initiate strip.fill if the switch is LOW in the else statement
//else {                                                                                                   
//strip.fill(green, 0, 15);
strip.show();

//}

  if (digitalRead(buttonApin) == HIGH)
  {
   strip.fill(red, 0, 15);
    strip.show();
  }}

neilvdm
 
Posts: 4
Joined: Tue Aug 20, 2019 5:09 am

Re: ITSY BITSY 32U4 3.3V 8MHZ

by adafruit_support_mike on Wed Sep 11, 2019 11:43 am

Glad to hear you have something working. Does it behave the way you want, or is there anything you want to change?

adafruit_support_mike
 
Posts: 58856
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.