I am trying to loop through the colors of the Neopixel by using the Force Sensitive Resistor if the random number is odd.
So by pressing harder on the FSR, the function will loop through the entire range of colors of the Neopixel. So when you push a little bit, the color will be green and if you pushing harder the color will be pink.
Below is my Arduino for this. Random numbers works. Only the neopixel function should loop through the range of the colors if the random number is odd. But when the FSR is not pressed, but there is a random number odd, it should set the neopixel on, like the color white or anything else.
Code: Select all
#include <Adafruit_NeoPixel.h> // Neopixel
#define PIN 6 // Which pin on the Arduino is connected to the NeoPixels?
#define NUMPIXELS 1 // How many NeoPixels are attached to the Arduino?
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second
int ledStart = 8; // Start at pin 8
int fsrAnalogPin = A0; // FSR connected to analog A0
int fsrReading; // the analog reading from the FSR resistor divider
long neoColor;
void setup() {
for(int i = 8; i<=13; i++) {
pinMode(i, OUTPUT);
}
pixels.begin();
pixels.show();
pixels.setBrightness(40);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
randNumber();
}
void randNumber() {
int randNumber;
randNumber = random(1, 5);
Serial.println(randNumber);
if (randNumber == 1 || randNumber == 3) {
for(int i = 8; i<= randNumber +5; i++) {
digitalWrite(i, HIGH);
delay(80);
}
neopixels();
}
if (randNumber == 4 || randNumber == 2) {
for(int i = 8; i<= randNumber +5; i++) {
digitalWrite(i, HIGH);
delay(80);
}
neopixelsOff();
}
delay(0);
}
void neopixels() {
fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
neoColor = map(fsrReading, 0, 100, 0, 16581375);
strip.setPixelColor(0, neoColor);
delay(400);
strip.show();
}
void neopixelsOff() {
pixels.setPixelColor(0,0,0,0);
}