0

NeoPixelRing code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: NeoPixelRing code

by adafruit_support_bill on Thu Feb 08, 2018 8:07 am

The Simon Monk books are good project-based introductory texts.
https://www.adafruit.com/category/250

But most of the Arduino-specific books are not very comprehensive with respect to the programming language. The "Arduino Language" is based on C++. And the Bjarne Stroustrup book is the definitive text on that - although I'll warn you that it is not an easy read.

https://www.amazon.com/C-Programming-La ... B%2B+books

There are also a number of very good on-line tutorials and references for the C++ language:
https://www.tutorialspoint.com/cplusplus/index.htm
http://www.cplusplus.com/doc/tutorial/
https://www.cprogramming.com/tutorial/c++-tutorial.html

adafruit_support_bill
 
Posts: 69769
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by mqdean on Thu Feb 08, 2018 9:11 am

Thanks alot! You guys are super responsive.

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by mqdean on Fri Feb 09, 2018 10:45 am

Managed to get the sketch working.

Thanks alot!

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Fri Feb 09, 2018 11:02 am

Good to hear. Thanks for the follow-up.

adafruit_support_bill
 
Posts: 69769
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by mqdean on Fri Mar 23, 2018 6:28 am

Hi there again.

I am trying to auto calibrate my fsr and below is the code. I want the pixel to light up when the value drops by 5 (serial monitor value) and return back to normal within 10 miliseconds. Hope you could assist me on this. I want to do this because the neutral reading changes when the fsr is worn as a vest by different persons.

Code: Select all | TOGGLE FULL SIZE
// These constants won't change:
const int sensorPin = 9;    // pin that the sensor is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value

#include <EasyNeoPixels.h>

#include <EasyNeoPixels.h>

#include <Adafruit_NeoPixel.h>

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);


void setup() {
  // calibrate during the first five seconds
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }
 
  pinMode(9, INPUT_PULLUP);
  strip.setBrightness(10);
  strip.begin();
  strip.show();
  delay (30);
}

int pixelCount = 0;
long impactTime = 0;

void loop() {
 
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);
 
Serial.println(analogRead(sensorPin));
   
    if (sensorValue < ((analogRead(sensorPin) - 5)))
    {
        // record the time when it goes below sensorValue drops by 5.
        impactTime = millis();
    }

    while (sensorValue < ((analogRead(sensorPin) - 5))) {}
   
    if (sensorValue) // check for return to 'normal'
    {
        if ((millis() - impactTime) < 10)  // If less than 100 ms, record as a hit.
        {
            strip.setPixelColor(pixelCount++, 0, 255, 0);
            strip.show();
            delay (100);
        }
    }
  }

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Fri Mar 23, 2018 7:19 am

Not sure I understand the logic in your loop. You take one reading that is re-mapped to your calibrated range. And you compare that to a raw, unmapped reading. I don't think you will get a meaningful result from that.

What happens during your calibration phase? Is it just to establish a baseline value, or do you do anything to the FSR?

adafruit_support_bill
 
Posts: 69769
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by mqdean on Fri Mar 23, 2018 9:12 am

Below is the sketch I did. When I wear the fsr as a vest, the reading in the serial monitor is between 40 and 50. But when my friend wears the fsr as a vest, the reading is between 20 and 30 without any pressure applied. Hence, the below sketch does not work because the threshold is according to the readings when I am wearing. Therefore, I would like the pixel to light up when the serial monitor value drops by 5.
For example, if the reading is between 20 and 30 for my friend, I need the pixel to light up when the value goes below 15 and returns to 20 within 100millis


Code: Select all | TOGGLE FULL SIZE
int force;
int pixelCount = 0;
long impactTime = 0;
int currentMillis;

void loop()
{
    force = analogRead(9);
    Serial.println(force);
    currentMillis = millis();
   
    if (force < 30)
    {
        // record the time when it goes below 30.
        impactTime = currentMillis;
        while(force < 30) { } // do nothing.
    }
    if (force > 40) // check for return to 'normal'
    {
        if ((currentMillis - impactTime) < 100)  // If less than 100 ms, record as a hit.
        {
            strip.setPixelColor(pixelCount++, 0, 255, 0);
            strip.show();
        }
    }
}

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Fri Mar 23, 2018 9:30 am

So the calibration phase is just to establish the baseline for whomever is wearing the vest.

So instead of comparing against a fixed value like this:
Code: Select all | TOGGLE FULL SIZE
if (force < 30)


You want to compare against the minimum baseline value from your calibration - minus 5:
Code: Select all | TOGGLE FULL SIZE
if (force < sensorMin - 5)

adafruit_support_bill
 
Posts: 69769
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by mqdean on Sat Mar 24, 2018 10:30 am

Hi

Below is what I have done. It does not seem to work. It seems like the baseline is not getting established. Kindly let me know where I have made an error.

Thank you!

Code: Select all | TOGGLE FULL SIZE
/*
  Calibration

  Demonstrates one technique for calibrating sensor input. The sensor readings
  during the first five seconds of the sketch execution define the minimum and
  maximum of expected values attached to the sensor pin.

  The sensor minimum and maximum initial values may seem backwards. Initially,
  you set the minimum high and listen for anything lower, saving it as the new
  minimum. Likewise, you set the maximum low and listen for anything higher as
  the new maximum.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0
  - LED attached from digital pin 9 to ground

  created 29 Oct 2008
  by David A Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Calibration
*/

// These constants won't change:
const int sensorPin = 9;    // pin that the sensor is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value

#include <EasyNeoPixels.h>

#include <EasyNeoPixels.h>

#include <Adafruit_NeoPixel.h>

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);


void setup() {

  // calibrate during the first five seconds
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  pinMode(9, INPUT_PULLUP);
  strip.setBrightness(10);
  strip.begin();
  strip.show();
  delay (15);
}

int pixelCount = 0;
long impactTime = 0;

void loop() {
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);

      Serial.println(analogRead(sensorPin));
   
    if (analogRead(sensorPin) < sensorMin-5)
    {
        // record the time when it goes below 50.
        impactTime = millis();
    }

     while (analogRead(sensorPin) < sensorMin-5) {}
   
    if (analogRead(sensorPin) > sensorMin) // check for return to 'normal'
    {
        if ((millis() - impactTime) < 5)  // If less than 100 ms, record as a hit.
        {
            strip.setPixelColor(pixelCount++, 0, 255, 0);
            strip.show();
            delay (100);
        }
    }
}

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Sat Mar 24, 2018 12:45 pm

This code is not serving any purpose. You can just delete it.

Code: Select all | TOGGLE FULL SIZE
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);


If you add some serial output to the loop showing the analog values and millisecond timings, it might shed some light on why it is not working as expected.

adafruit_support_bill
 
Posts: 69769
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.