So i got the button working in the serial. But now i have a neopixel library for a ledstrip that i would like to trigger with the click of a button, but i have no clue how i would go about this.
Here's the 2 separate codes
int ledPin = D5; // choose the pin for the LED
int inPin = D7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
Serial.println("HOOOOOG");
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.println("laag");
}
}
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN D5
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 12
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second
void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
//#if defined (__AVR_ATtiny85__)
// if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
//#endif
// End of trinket special code
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
for(int i=0;i<NUMPIXELS;i++){
// pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
pixels.setPixelColor(i, pixels.Color(150,0,0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
Put the NeoPixel for loop in a function and call it when the button becomes pressed.
Thanks for the reply. I've been trying to get it working but no success yet
Im not quite sure how to call it with the button as well.
Here's the code i've been trying with
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN D5
#define NUMPIXELS 12
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 700; // delay for half a second
int inPin = D7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
Serial.println("HOOOOOG");
digitalWrite(ledPin, LOW);
} /*else {
digitalWrite(ledPin, HIGH);
Serial.println("laag");
}*/
}
function pixels(){
for(int i=0;i<NUMPIXELS;i++){
// pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
pixels.setPixelColor(i, pixels.Color(150,0,0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
That doesn't compile. Here's a version that does. I don't have the hardware to test it (yet).
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
const byte ledPin=5;
#define NUMPIXELS 12
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
int delayval = 700; // delay for half a second
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop()
{
val = digitalRead(inPin); // read input value
if (val == HIGH)
{ // check if the input is HIGH (button released)
Serial.println("HOOOOOG");
SetPixels();
digitalWrite(ledPin, LOW);
} /*else {
digitalWrite(ledPin, HIGH);
Serial.println("laag");
}*/
}
void SetPixels()
{
for (int i = 0; i < NUMPIXELS; i++)
{
// pixels.Color takes RGB values, from 255,0,0 up to 0,255,255
pixels.setPixelColor(i, pixels.Color(150, 0, 0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
Dude, you're a legend. the button works. I have an arduino with D5 and D7 and such only had to change that. Many thanks!
NO NEED DECLARE LEDPIN AS OUTPUT AGAIN