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

Post your cool example code here.
Nutsy
Posts: 214
Joined: Sun Jul 24, 2016 4:26 pm

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

Postby 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?

Code: Select all

#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
Posts: 214
Joined: Sun Jul 24, 2016 4:26 pm

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

Postby Nutsy » Tue Jul 26, 2016 5:35 pm

Actually I'm maybe thinking its the fast LED library...

stevestrong
Posts: 1011
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

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

Postby 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
Posts: 247
Joined: Fri Jun 03, 2016 8:19 am

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

Postby edogaldo » Tue Jul 26, 2016 6:44 pm

Nutsy wrote:Actually I'm maybe thinking its the fast LED library...

Hi @Nutsy, just a tip: as I can see the stepper speed depends on the voltage applied, which voltage are you using?
How are you powering the motor?

Best, E.

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

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

Postby 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
Posts: 214
Joined: Sun Jul 24, 2016 4:26 pm

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

Postby 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
Posts: 214
Joined: Sun Jul 24, 2016 4:26 pm

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

Postby 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".


Clock divider?

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

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

Postby 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
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

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

Postby madias » Tue Jul 26, 2016 7:04 pm

SPI.setClockDivider(dividervalue)

dividervalue can be:

Code: Select all

SPI_CLOCK_DIV2
SPI_CLOCK_DIV4
SPI_CLOCK_DIV8
SPI_CLOCK_DIV16
SPI_CLOCK_DIV32
SPI_CLOCK_DIV64
SPI_CLOCK_DIV128
SPI_CLOCK_DIV256

start with something like SPI_CLOCK_DIV16 and go up to SPI_CLOCK_DIV2 (fastest, but maybe incompatible with your additional SPI hardware)

Nutsy
Posts: 214
Joined: Sun Jul 24, 2016 4:26 pm

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

Postby 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


Return to “Code snipplets”

Who is online

Users browsing this forum: No registered users and 1 guest