Need help, Why is this code running much slower than Uno?

Nutsy
Tue Jul 26, 2016 4:42 pm
So heres the thing. Im doing a speedo gauge project for fun to put on my bike.

And at this stage im just getting some basic motor controls and LED updates before I get into doing the MPH and RPM reading calculations done.

I’ve modified the stepper.h library to strip out the other wire code and fixed its stepper sequence which was just wrong…

That side of things seem to work fine. Its used code is actually untouched apart from sequence.

Now the trouble is with my LED array.
Where before on the Uno the array would update fine and the motor would run at full speed. I have the LED array updating between each MPH step (23 or so motor steps…)

Can anyone please take a look at this code and tell me why its running so slow? The main reason im using the maple is because its 75mhz. Its meant to be faster than the Uno, should be able to get more done… And i read theres even some basic software multi threading as well…
Which is useful as its going to be calculating MPH and RPM and display those details on motorised dials leds and a lcd display :p

BTW I’m coding from PlatformIO and building and uploading from arduino IDE, cant seem to upload and build form PlatformIO

Also I apologies for my naming conventions and theres plenty of commented out code while Ive been trying to find the cause of the slow down.

The function where things dont seem to be running at proper speed is moveMPH, and slows down when i run the call updateMPHled…

I’ve never claimed to be a coding genius. Im a graphics artist by trade. But if anyone can help me please let me know how to fix this.

Is it really just a flag or setting I need to apply to the STM? Something to call to get it to run full speed?

#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <newStepper.h>
#include <math.h>
#include <FastLED.h>

#define ledPin 3
#define numLeds 24

// Define the array of leds
CRGB led_array[numLeds];

LiquidCrystal_I2C lcd(0x3F, 20, 4);

const int HomeBTN = 13; /// pin connected to button

int MPH = 0; // to be replaced by a MPH calculator in loop connected to bike
int OldMPH = 0;
int differ = 0;

// The step angle is 5.625/64 and the
// operating Frequency is 100pps. Current draw is 92mA.
////////////////////////////////////////////////
//4096
// 2.25 deg per MPH, 5.625/64 = 0.0879, 2.25/0.0879 = 25.6 steps per mph

///////////////////////////////////////////////////////////////////////////////
/////// Stepper Settings /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#define StepsPerRev 2080 //2080 seems to be cloest non float figure //37.9 //88641975 /// 2037.88641975 /// 2048 /// 4075.7728395
#define mphRange 80 // dial range for MPH
#define StepsPerMPH ((StepsPerRev) / 2 / (mphRange)) //13
#define speed 15 //500 at 5v 700-750 at 9v some skipping 850 at 12v but too hot i think
//////////Stepper Call
newStepper mph_stepper(StepsPerRev, 8, 9, 10, 11); /// 64 steps per motor 360 (not geared rev)
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
void updateMPHled (int cMPH)
{
int light = ((numLeds + .0f) / (mphRange + .0f) * cMPH); // (numLeds / mphRange * cMPH);
//Serial.print("Blah " + String(light));
//Serial.println();

for (int led = 0; led < numLeds; led++)
{
int cVal = 0;
if (led <= light)
{
cVal = 5;
}

led_array[led].setRGB(cVal,cVal,cVal);
FastLED.show();
}

}

//////////////////////////////////////////////////////////////////////////////
void moveMPH(int inMPH) ///function to move motor to the mph detected and displays information on the screen
{
// MPH = inMPH;

//lcd.clear();
//lcd.setCursor(0, 0);
//lcd.print (String("MPH= ") + inMPH);
//lcd.setCursor(0, 1);
//lcd.print (String("Old MPH= ") + OldMPH);
//lcd.setCursor(0, 2);
//lcd.print (String("Difference= ") + (inMPH - OldMPH));

//Serial.print(OldMPH - inMPH);
//Serial.println();

/// will need changing for interrupted calls for faster mph updating. Will need a seporate var holding current mph or step count possision

if (OldMPH < inMPH)
{
for (int i = 0; i < (inMPH - OldMPH); i++)
{
mph_stepper.step(-StepsPerMPH);
updateMPHled((inMPH)); // StepsPerMPH) + (OldMPH / StepsPerMPH) );
}
}
else
{
for (int i = 0; i < (OldMPH - inMPH); i++)
{
mph_stepper.step(StepsPerMPH);
updateMPHled((inMPH)); // StepsPerMPH) + (OldMPH / StepsPerMPH) );
}
}
OldMPH = inMPH;
}

/////////////////////////////////////////////////////////////////////////
void ZeroHome()
{
while (digitalRead(HomeBTN)== HIGH)
{
mph_stepper.step(11);
}
OldMPH = 0;
}

////////////////////////////////////////////////////////////////////////
void setup()
{
mph_stepper.setSpeed(speed);

FastLED.addLeds<NEOPIXEL,ledPin>(led_array,numLeds);
pinMode(HomeBTN, INPUT_PULLUP);

//Serial.begin(9600); /// for serial feedback

/// setting up LCD during testing
//lcd.begin ();
//lcd.backlight();
//lcd.clear();

/// Current starting demo/homing code, to be replaced with reed detection at 0 and led demos.

ZeroHome();

//for (int i = 0; i < (mphRange / 10) + 1; i++)
//{
// moveMPH(i * 10);
// //delay(200);
//}

//delay(1000);

//for (int i = (mphRange / 10) ; i >= 1; i--)
//{
// moveMPH(i * 10);
// //delay(200);
//}

// ZeroHome();
// delay(1000);
}

//////////////////////////////////////////////////////////////////////////////
void loop() {

//lcd.setCursor(0, 3);
//lcd.print (String(digitalRead(HomeBTN)));
//MPH = random(0, mphRange + 1); // to be replaced by code that calculates MPH

// if (MPH < 0)
// {
// ZeroHome();
// }
// else
// {
// moveMPH(MPH);
// }
moveMPH(0);
delay(100);
moveMPH(80);
delay(100);
}


Nutsy
Tue Jul 26, 2016 5:35 pm
Actually I’m maybe thinking its the fast LED library…

stevestrong
Tue Jul 26, 2016 6:05 pm
I don’t think that any of stepper and fast LED library was yet tested/ported to STM32duino, so it is your job now to test and debug :)

edogaldo
Tue Jul 26, 2016 6:44 pm
Nutsy wrote:Actually I’m maybe thinking its the fast LED library…

madias
Tue Jul 26, 2016 6:50 pm
The “fastled” is really a monster of a library. Wish you luck to find it out….
But: If it’s SPI driven, I’m pretty sure that it has something to do with a wrong clock divider.
Candidate is: https://github.com/FastLED/FastLED/tree … /arm/stm32 (I cannot access to github at the moment, I only see a pink(!) angry(!) unicorn…..wuahaa..)
You should compare every single code (module) with your UNO to find out the “slow down part”.

Nutsy
Tue Jul 26, 2016 6:51 pm
the 5V motor is running off a 6v adapter, thats running through a ULN2003APC… Its a hacked together car based multi adapter thats running off a spare 12v one :p Im currently short on cash so not buying a new adapter.

Dont worry its isolated form the maple mini voltage.

Oh and as for debugger…. LOL im really REALLY not that good. Before this my coding experience was VBA :p

I also dont know C++ that well tbh. I can work stuff out and google every single call and function of c++ but some of the basic syntax really is lost on me. For instance I still cant work out what a this-> is… even when googling about i and reading explanations I still dont get it.

So yeah writing my own LED controller library is pretty beyond my skill set.


Nutsy
Tue Jul 26, 2016 7:00 pm
madias wrote:The “fastled” is really a monster of a library. Wish you luck to find it out….
But: If it’s SPI driven, I’m pretty sure that it has something to do with a wrong clock divider.
Candidate is: https://github.com/FastLED/FastLED/tree … /arm/stm32 (I cannot access to github at the moment, I only see a pink(!) angry(!) unicorn…..wuahaa..)
You should compare every single code (module) with your UNO to find out the “slow down part”.

madias
Tue Jul 26, 2016 7:01 pm
You can just search for an alternative library. There are many neopixel (aka WS2812) libraries out there. (I think I successfully used the adafruit neo-pixel library, but I can’t remember).
Just for one test:
Can you please connect USB to your project and just keep the serial monitor open? (USB serial slow down the whole system under special circumstances, mostly a forgotten “Serial.print”) If it’s much faster, search for every Serial.xxx even in the libraries.

madias
Tue Jul 26, 2016 7:04 pm
SPI.setClockDivider(dividervalue)

dividervalue can be:
SPI_CLOCK_DIV2
SPI_CLOCK_DIV4
SPI_CLOCK_DIV8
SPI_CLOCK_DIV16
SPI_CLOCK_DIV32
SPI_CLOCK_DIV64
SPI_CLOCK_DIV128
SPI_CLOCK_DIV256


Nutsy
Tue Jul 26, 2016 10:46 pm
Hmm im getting spi was not declared in this scope error… Is it ina specific library?

ignore that found the library… and it doesnt appear to make any difference… I think ill have to try the other library


zoomx
Wed Jul 27, 2016 6:15 am
Nutsy wrote:Hmm im getting spi was not declared in this scope error… Is it ina specific library?

madias
Wed Jul 27, 2016 7:49 am
AS I wrote: You have to search WITHIN the libraries for every “SPI” related stuff, not in the sketch.
The fastwire library has an extra STM32 part of code, but I’m sure that it was not written for STM32duino, but MBED or something. I just wonder that the library compiles. Maybe in soft SPI mode – you have to look at the verbose output of the IDE if there are any “warning” messages.

cat_enabler
Tue Apr 17, 2018 2:28 pm
@Nutsy, Just wanted to say I’m experiencing the same issue with slow running LED animation. I see you’re using Neopixels, which are a clockless LED chipset. If I run a clocked LED chipset like APA102 (DotStar) then animation runs at the correct speed. When driving Neopixels, the exact same animation code will run half as fast on a Maple Mini as on Uno. Will keep you posted if I find a solution.

Leave a Reply

Your email address will not be published. Required fields are marked *