0

FSR/Neopixel project running very slow
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

FSR/Neopixel project running very slow

by tazemt on Wed Sep 15, 2021 9:02 pm

Disclaimer: I am a complete and utter noob. In fact, just by saying that I probably insulted noobs. Also I'm now brain dead from trying to figure this out on my own. How brain dead? I rewrote this post at least 5 times trying to NOT sound like a lunatic and the following was the most coherent and concise of the bunch so, yeah,,,, pretty brain dead.

I am working on a project that involves an ItsyBitsy 5V, eight LED Neopixel stick, and a FSR. ItsyBitsy listens to A7 for the FSR which is hooked up to 3V pin. I have a 100K ohm resistor in between the FSR and A7 running to the ground pin as per the FSR tutorial increased ohm resistor not withstanding. The Neopixel stick is taking up the 5V, other ground, and #11 pins. The goal is as the FSR reads higher resistance the neopixel changes from colorWipe red to yellow, then ultimately to green.

(Background: It's for a task trainer to help teach new medics how tight a tourniquet has to be for it to effective stop an arterial haemorrhage: Red = too loose Yellow = getting there but not quite and Green = good to go)

When I run my code and check the serial readout, if there's no pressure on the FSR it's just flying along as expected. However when I put some pressure on the FSR suddenly serial goes from checking 4-5 times a second to checking once every 4 seconds. The LED's don't appear to be slowing down.

At the moment I am just running the microcontroller off an USB2 port on my laptop while I trying and figure out how everything is going to work, so no aux power running to the microcontroller. Could this be a reason for the slow down? I have a variable power supply but I'm hesitant to use it since I'm not really sure how many volts/amps the ItsyBitsy can handle. I'm sure that info is written out somewhere but it's either not anywhere that I've looked (slightly possible) or I'm too stupid to realize it's staring me right in the face (extremely possible).

If I just run the FSR without the LED's it works great. And the code for the LED's works, it just work really slowly when pressure is applied to the FSR. What am I screwing up here? Is there a protocol or etiquette for posting an .ino file that won't mess up the forum? Maybe the problem is in how I'm coding it (script kiddie style by cobbling together bits of code that people waaaay smarter than me came up with for different purposes).

Any insight anyone could offer would be much appreciated. I don't mind working to find answer myself, that's certainly fair, I just don't know where to look. Thank you for your time.

tazemt
 
Posts: 5
Joined: Tue Sep 07, 2021 4:18 pm

Re: FSR/Neopixel project running very slow

by mikeysklar on Thu Sep 16, 2021 4:53 pm

Cool project making a tourniquet trainer.

Please post your code inside CODE brackets. You'll see a CODE button above while replying.

You could try running the ItsyBitsy off a USB battery, but I don't think it will make a difference here. If possible please post a photos of your wiring / soldering. 800x600 works best you can attach photos to your replies.

mikeysklar
 
Posts: 5855
Joined: Mon Aug 01, 2016 8:10 pm

Re: FSR/Neopixel project running very slow

by tazemt on Tue Sep 21, 2021 4:29 pm

I must apologize for the delay in response. Getting the photos dialed in proved more tedious that I thought, then again my OCD-like tendencies certainly weren't helping matters. Again I will state: I have no idea what I'm doing.
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

#define LED_PIN    11
#define fsrpin A7

int fsrreading;

#define LED_COUNT 8

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN);


void setup() {
  Serial.begin(9600);
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(20); // Set BRIGHTNESS to about 1/5 (max = 255)
}

void colorWipe(uint32_t color, int wait) { // Taken from Adafruit neopixel guide
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
  for(int i=strip.numPixels()-1; i >= 0; i--) {
    strip.setPixelColor(i,0, 0, 0);
    strip.show();
    delay(wait);
  }
}

void loop() {
   
  fsrreading=analogRead(fsrpin);
  Serial.println(fsrreading);
  if (fsrreading>50) {
    colorWipe(strip.Color(255,   0,   0), 40);
   }
  else strip.show();
  if (fsrreading>100) {
    colorWipe(strip.Color(204,   170, 2), 40);
    } // Yellow
  else strip.show();
  if (fsrreading>200) {
    colorWipe(strip.Color(  0, 255, 0), 40);
    }// Green
   else strip.show();
  }

I used (stole, ripped off, plagiarized the hell out of, and probably broke) the code written in the Adafruit NeoPixel guide for the strip test and attempted (failed miserably) to edit it to fit my specific needs. Below are the photos. The resistor (if it can be seen at all) is a 100K ohm resistor, provided I'm not going color blind.
Attachments
allInOne.jpg
No, the spaghetti wiring wasn't done deliberately, I just didn't have any short jumpers handy, sorry.
allInOne.jpg (110.21 KiB) Viewed 377 times

tazemt
 
Posts: 5
Joined: Tue Sep 07, 2021 4:18 pm

Re: FSR/Neopixel project running very slow

by tazemt on Tue Sep 21, 2021 4:31 pm

This is as clear a shot as I could get of my soldering. And yes, believe it or not I really do have full function in both my hands, and was sober at the time; I'm just this bad at soldering.
Attachments
pinout.jpg
pinout.jpg (71.65 KiB) Viewed 377 times

tazemt
 
Posts: 5
Joined: Tue Sep 07, 2021 4:18 pm

Re: FSR/Neopixel project running very slow

by tazemt on Tue Sep 21, 2021 4:35 pm

Wiring "diagram" (I use that term VERY loosely) I drew up to keep track of what goes where.
Attachments
diagram.jpg
diagram.jpg (46.35 KiB) Viewed 377 times

tazemt
 
Posts: 5
Joined: Tue Sep 07, 2021 4:18 pm

Re: FSR/Neopixel project running very slow

by mikeysklar on Tue Sep 21, 2021 7:22 pm

This is a pretty funny self deprecating writeup you have done. Maybe you have a future in comedy instead of savings lives through proper tourniquet technique.

One thing to try is a flag to save the previous FSR value and if it has not changed enough to warrant an LED change that you do nothing "continue" and let your code continue to scan at full speed rather than update stuff. Sort of any example here using a case statement and saving the last value.

https://wokwi.com/arduino/libraries/Ada ... ttoncycler

mikeysklar
 
Posts: 5855
Joined: Mon Aug 01, 2016 8:10 pm

Re: FSR/Neopixel project running very slow

by moonie223 on Wed Sep 22, 2021 4:10 pm

I think the native usb always uses a fast serial data rate, but you could try bumping serial baudrate from 9600 to 115200.

Your loop code could use some checking. It looks like it's going to show LED colors you don't want it to show.

Assume fsrreading is 220. As you progress through the loop code this variable is high enough to trigger all of your color updates. First fsrreading is higher than 50, so it gets wiped red, then it's skipping over the first neopixel update. Next its still higher than 100, so you'll update the LEDs in ram again. Finally it's still higher than 200, so they'll be updated once again.

You've got delays in there too, at least in the colorWipe function you call from the main loop. I believe neopixel code is blocking. There's no need, you won't get to the delay before the LEDs are done updating.

And you call updates to the LEDs twice in the same function (colorWipe) not very efficient. You only need to call strip.show once per loop.

It's unrelated to power. If you were to actually run out of power the CPU will brownout reset. It won't derate it's clock speed and start processing slower, that's not possible.

moonie223
 
Posts: 47
Joined: Sun Feb 05, 2017 2:44 pm

Re: FSR/Neopixel project running very slow

by tazemt on Sun Sep 26, 2021 11:29 am

Thank you both very much for your advice (and compliments, mikeysklar). I am digging into the code again to find places where I can streamline it/make it more efficient. Mikeysklar, the Wokwi site is awesome. I have to admit, when I first opened it my ADD kicked in and a co-worker and I sat and played with it for about 45 mins before we remembered "Oh yeah, work, that's a thing we're supposed to do here." Does anyone have any advice on software similar to that found on Wokwi for simulating micro-controllers (not just Arduino's but Adafruit, etc boards as well)? I am running Pop_OS! on one laptop and Arch (ok who am I trying to kid, most times Arch is running me, not the other way around) on the other rig. Anyone have any favorite prototyping software they'd like to recommend? Preferably with simple instructions, words of one syllable or less work best in my case. Huukd own fonix werked fer me.

tazemt
 
Posts: 5
Joined: Tue Sep 07, 2021 4:18 pm

Re: FSR/Neopixel project running very slow

by mikeysklar on Mon Sep 27, 2021 12:20 pm

Nice summary of simulators here.

https://tutorial45.com/arduino-simulator-emulator/

Code: Select all | TOGGLE FULL SIZE
 PaulWare’s Arduino Simulator
SimDuino
ArduinoSim
Arduino Simulator for PC
Emulare Arduino Simulator
Simulator for Arduino
Yenka for Arduino
Tinkercad circuit
LTSpice Arduino Simulator
PSpice for Arduino
Circuit Lab Arduino Simulator
EasyEDA simulator
Circuits-cloud Simulator
Systemvision Simulator
Proteus by Labcenter
Arduino IO Simulator 1.5


Personally I find that too much time gets lost getting the simulator going and it does not reflect reality enough to justify. Sending data out the serial port and blinking lights is often faster if the code is small enough.

mikeysklar
 
Posts: 5855
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.