How to loop through the Neopixel with the Force Sensitive Re

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
JasperH
 
Posts: 2
Joined: Fri Oct 17, 2014 11:10 am

How to loop through the Neopixel with the Force Sensitive Re

Post by JasperH »

Good afternoon,

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);
}

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

Re: How to loop through the Neopixel with the Force Sensitiv

Post by adafruit_support_bill »

the neopixel function should loop through the range of the colors if the random number is odd.
You do not have a loop in your neopixel function. You only do one reading. Then you wait 400 milliseconds before calling strip.show(). After that you return to the loop and generate another random number.

User avatar
JasperH
 
Posts: 2
Joined: Fri Oct 17, 2014 11:10 am

Re: How to loop through the Neopixel with the Force Sensitiv

Post by JasperH »

Okay, but how can I create a loop, so when the FSR is pushed harder it will change the color?

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

Re: How to loop through the Neopixel with the Force Sensitiv

Post by adafruit_support_bill »

If you move your random number generator into the setup, your neopixel function will get called repeatedly.

I'd also either shorten or eliminate the delay so that it will be more responsive.

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

Return to “Arduino”