cancel
Showing results for 
Search instead for 
Did you mean: 

I would like to run an arduino nano sketch on STM 32, Only 4 pins are need to define.... please help me to do this.... The code is working perfectly on my circuit, due to arduino nano memory is less, so i am unable to add more leds (ws2812B). Thank you.

SDamo.1
Associate II

/*

  Code for Arduino WS2812b-MSGEQ7 Spectrum Analizer.

  https://www.youtube.com/watch?v=mGton_zqrS0

  

*/

#include <FastLED.h>

#define NUM_LEDS 70

#define COLUMN 7

#define ROWS 10

#define DATA_PIN 6

#define STROBE_PIN 3

#define RESET_PIN 2

#define BRIGHTNESS 100

#define NOISE 35

// Matrix Definition

CRGB leds[NUM_LEDS];

typedef struct ledrgb   //structure containing the parameters relating to a led

{

 int r;

 int g;

 int b;

 int hue;

 int sat;

 int val;

 int nled;

 boolean active;

} led;

led colors[COLUMN][ROWS]; //matrix containing the progressive number of each single LED

//Global Variables

int left_input[7];  //Audio Input array

int nlevel;   //level index

//-------------------------------------------------------------------------

int delta = 50;  //Value working with my matrix 7x10

//-------------------------------------------------------------------------

int hue_rainbow = 0; //global variable for the rainbow variable hue

int long rainbow_time = 0;

int long time_change = 0;

int effect = 0;

void setup() {

 Serial.begin (9600);

 pinMode (DATA_PIN, OUTPUT);

 pinMode (STROBE_PIN, OUTPUT);

 pinMode (RESET_PIN, OUTPUT);

 int n = 0;

 for (int i = 0; i < COLUMN; i++) {  //number the leds (depends on how they are connected)

  for (int j = 0; j < ROWS; j++) {

   colors[i][j].nled = n;

   n++;

  }

 }

 FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

 FastLED.setBrightness( BRIGHTNESS );

 rainbow_time = millis();

 time_change = millis();

}

void loop() {

 readMSGEQ7();

 if (millis() - time_change > 12000) {  //code that establishes how often to change effect

  effect++;

  if (effect > 7) {

   effect = 0;

  }

  time_change = millis();

 }

 switch (effect) {

  case 0:

   rainbow_dot();

   full_column();

   updateHSV();

   break;

  case 1:

   if (millis() - rainbow_time > 15) {

    dinamic_rainbow();

    rainbow_time = millis();

   }

   full_column();

   updateHSV();

   break;

  case 2:

   if (millis() - rainbow_time > 15) {

    rainbow_column();

    rainbow_time = millis();

   }

   full_column();

   updateHSV();

   break;

  case 3:

   if (millis() - rainbow_time > 15) {

    total_color_hsv(255, 255, 255);

    rainbow_time = millis();

   }

   full_column();

   updateHSV();

   break;

  case 4:

   if (millis() - rainbow_time > 15) {

    rainbow_dot();

    rainbow_time = millis();

   }

   full_column_dot();;

   updateHSV();

   break;

  case 5:

   if (millis() - rainbow_time > 15) {

    dinamic_rainbow();

    rainbow_time = millis();

   }

   full_column_dot();

   updateHSV();

   break;

  case 6:

   if (millis() - rainbow_time > 15) {

    rainbow_column();

    rainbow_time = millis();

   }

   full_column_dot();

   updateHSV();

   break;

  case 7:

   total_color_hsv(55, 255, 255);

   full_column_dot();

   updateHSV();

   break;

 }

 delay(30);

}

/////FUNCTIONS

// Function that reads the 7 bands of the audio input.

void readMSGEQ7(void) {

 digitalWrite(RESET_PIN, HIGH);

 digitalWrite(RESET_PIN, LOW);

 for (int band = 0; band < COLUMN; band++) {

  digitalWrite(STROBE_PIN, LOW);

  delayMicroseconds(30);

   

  //-----------------------------------------------------------------------

  left_input[band] = analogRead(A5) - NOISE; //saves the reading of the audio input.

  //----------------------------------------------------------------------------

   

  digitalWrite(STROBE_PIN, HIGH);

 }

}

void updateRGB(void) {

 for (int i = 0; i < COLUMN; i++) {

  for (int j = 0; j < ROWS; j++) {

   if (colors [i][j].active == 1) {

    leds[colors[i][j].nled] = CRGB(colors[i][j].r, colors[i][j].g, colors[i][j].b);

   } else {

    leds[colors[i][j].nled] = CRGB::Black;

   }

   FastLED.show();

  }

 }

}

void updateHSV(void) {

 for (int i = 0; i < COLUMN; i++) {

  for (int j = 0; j < ROWS; j++) {

   if (colors[i][j].active == 1) {

    leds[colors[i][j].nled] = CHSV(colors[i][j].hue, colors[i][j].sat, colors[i][j].val);

   } else {

    leds[colors[i][j].nled] = CRGB::Black;

   }

  }

 }

 FastLED.show();

}

void full_column(void) {

 nlevel = 0;

 for (int i = 0; i < COLUMN; i++) {

  nlevel = left_input[i] / delta;

  for (int j = 0; j < ROWS; j++) {

   if (j <= nlevel) {

    colors[i][j].active = 1;

   }

   else {

    colors[i][j].active = 0;

   }

  }

  //colors[i][peaks[i]].active=1;

 }

}

void full_column_dot(void) {

 nlevel = 0;

 for (int i = 0; i < COLUMN; i++) {

  nlevel = left_input[i] / delta;

  for (int j = 0; j < ROWS; j++) {

   if (j == nlevel) {

    colors[i][j].active = 1;

   }

   else {

    colors[i][j].active = 0;

   }

  }

  //colors[i][peaks[i]].active=1;

 }

}

void total_color_hsv(int h, int s, int v) {

 for (int i = 0; i < COLUMN; i++) {

  for (int j = 0; j < ROWS; j++) {

   colors[i][j].hue = h;

   colors[i][j].sat = s;

   colors[i][j].val = v;

  }

 }

}

void total_color_rgb(int r, int g, int b) {

 for (int i = 0; i < COLUMN; i++) {

  for (int j = 0; j < ROWS; j++) {

   colors[i][j].r = r;

   colors[i][j].g = g;

   colors[i][j].b = b;

  }

 }

}

void rainbow_column(void) {

 int n = 36;

 for (int i = 0; i < COLUMN; i++) {

  for (int j = 0; j < ROWS; j++) {

   colors[i][j].hue = n;

   colors[i][j].sat = 230;

   colors[i][j].val = 240;

  }

  n += 36;

 }

}

void rainbow_dot(void) {

 int n = 36;

 for (int i = 0; i < COLUMN; i++) {

  for (int j = 0; j < ROWS; j++) {

   colors[i][j].hue = n;

   colors[i][j].sat = 230;

   colors[i][j].val = 240;

   n += 5;

  }

 }

}

void dinamic_rainbow(void) {

 for (int i = 0; i < COLUMN; i++) {

  for (int j = 0; j < ROWS; j++) {

   colors[i][j].hue = hue_rainbow;

   colors[i][j].sat = 230;

   colors[i][j].val = 240;

  }

 }

 hue_rainbow++;

}

4 REPLIES 4
Piranha
Chief II

For a start replace most of those int variables (including in struct ledrgb) with uint8_t. On 8-bit AVR you will gain everything - RAM, flash and execution speed.

prain
Senior III

You may want to take a look ​at stm32duino project. It's an Arduino port for stm32 MCUs. The popular blue pill board is supported by stm32duino.

SDamo.1
Associate II

I am already having a blue pill and ST link hardware,

I just change the pin number and compiled like .....

#include <WS2812B.h>

#include <FastLED.h>

#define NUM_LEDS 70

#define COLUMN 7

#define ROWS 10

#define DATA_PIN PB8

#define STROBE_PIN PB3

#define RESET_PIN PB4

#define BRIGHTNESS 100

#define NOISE 35

this is the message after compiling ...

I am not an experienced programmer, and fresh with STM32,

exit status 1

no matching function for call to 'CFastLED::addLeds(CRGB [70], int)'

SDamo.1
Associate II

the sketch in the example library is working on the blue-pill