0

NeoPixelRing code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: NeoPixelRing code

by mqdean on Sun Feb 04, 2018 8:19 pm

Thank you for the help. Edited the sketch and uploaded, all the pixels light up without any force on the fsr.

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 < 50)
    {
        // record the time when it goes below 50.
        impactTime = currentMillis;
        while(force < 50) { } // do nothing.
    }
    if (force > 60) // 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: 24
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Mon Feb 05, 2018 7:07 am

Print out the actual force readings. You may need to adjust the thresholds to match the range that your sensor is reading.

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

Re: NeoPixelRing code

by mqdean on Mon Feb 05, 2018 7:52 am

The readings are between 125 and 135. So it should not light up by right. Was trying to play around with the brackets but did not work.

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

Re: NeoPixelRing code

by adafruit_support_bill on Mon Feb 05, 2018 7:59 am

Try adding a delay(100); to your setup().

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

Re: NeoPixelRing code

by mqdean on Mon Feb 05, 2018 10:22 am

The delay did not work but I modified the code a little by changing the "<" and ">" with "==" for the "if" logic. The force is working now but the time code is not working. Below is the modified sketch.
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 50.
        impactTime = currentMillis;
        while(force < 30) { } // do nothing.
    }
    if (force == 60) // 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: 24
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by mqdean on Mon Feb 05, 2018 10:23 am

What I understand is that the below code is not working.

Code: Select all | TOGGLE FULL SIZE
if ((currentMillis - impactTime) < 100)

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

Re: NeoPixelRing code

by mqdean on Mon Feb 05, 2018 10:25 am

When I apply pressure on the fsr for more than 5000 mills, the pixel still lights up when I release.

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

Re: NeoPixelRing code

by adafruit_support_bill on Mon Feb 05, 2018 2:34 pm

I modified the code a little by changing the "<" and ">" with "==" for the "if" logic.

That is not likely to work well. The sensor is not guaranteed to ever generate that specific value. With analog sensors, you generally need to check for a range of values.

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

Re: NeoPixelRing code

by mqdean on Mon Feb 05, 2018 3:20 pm

Is the below code meant to record the duration when the value is below 50 or is it meant to capture the time when it goes below 50.

Code: Select all | TOGGLE FULL SIZE
if (force < 50)
    {
        // record the time when it goes below 50.
        impactTime = currentMillis;
        while(force < 50) { } // do nothing.

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

Re: NeoPixelRing code

by adafruit_support_bill on Mon Feb 05, 2018 3:28 pm

It records the time when the value first goes below 50. Then it waits until the value goes back above 50.

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

Re: NeoPixelRing code

by mqdean on Mon Feb 05, 2018 11:02 pm

Why do we have to use the below code
Code: Select all | TOGGLE FULL SIZE
long impactTime = 0;


instead of

Code: Select all | TOGGLE FULL SIZE
int impactTime

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

Re: NeoPixelRing code

by adafruit_support_bill on Tue Feb 06, 2018 8:35 am

Because an 'int' is a 16 bit signed integer. it can store values ranging from -32768 to +32767.

In milliseconds, that is a little over 32 seconds before it wraps around to be a negative number.

A 'long' is a 32 bit integer. That will run for about 25 days worth of milliseconds before wrapping around.

Technically, a better type to use would be uint32_t which is an unsigned 32 bit integer. That run for 49 days worth of milliseconds before wrapping back around to zero.

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

Re: NeoPixelRing code

by mqdean on Tue Feb 06, 2018 9:22 am

Thank you for the explanation. Very useful.
In your recommended sketch, below, I have edited the "currentMillis()" to "currentMillis" thinking that it might have been a typo. If it was not a typo, please let me know.

Do I have to change the "int currentMillis;" to "long currentMillis as well;"?

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

void loop()
{
    force = analogRead(9);
    Serial.println(force);
    currentMillis = millis();
   
    if (force < 50)
    {
        // record the time when it goes below 50.
        impactTime = currentMillis();
        while(analogRead(9) < 50) { } // do nothing.
    }
    if (force > 60) // 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: 24
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Tue Feb 06, 2018 9:28 am

Yes, "currentMillis()" was a typo. It should not have the parenthesis.

And all variables holding millisecond values should be of type 'long' or 'uint32_t'.

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

Re: NeoPixelRing code

by mqdean on Thu Feb 08, 2018 5:44 am

Any recommendation of books on arduino programming, something comprehensive.

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

Please be positive and constructive with your questions and comments.