I'm trying to convert the serial.print rpm into text to display on the r4 led matrix

Hi,
I am new to electronics in general and very, very new to programming. I followed a guide to make a cpu fan controller using an arduino and a temperature sensor. i got it working, but i want to get the rpms to display on the LED matrix of the uno r4 wifi arduino. I'm not sure what to even look up for this. Here is the fan control and led scrolling text code I'm using:

#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;
float temperature;
const int fan_control_pin = 3;
int count = 0;
unsigned long start_time;
int rpm;
void setup() {
    matrix.begin();

  matrix.beginDraw();

  matrix.stroke(0xFFFFFFFF);
  matrix.textScrollSpeed(100);

  const char text[] = "  UNO r4  ";
  matrix.textFont(Font_4x6);
  matrix.beginText(0, 1, 0xFFFFFF);
  matrix.println(text);
  matrix.endText(SCROLL_LEFT);

  matrix.endDraw();
  
  pinMode (fan_control_pin, OUTPUT);
  analogWrite (fan_control_pin, 0);
  Serial.begin (9600);
  attachInterrupt (digitalPinToInterrupt(2), counter, RISING);
}

void loop() {
   matrix.beginDraw();

  matrix.stroke(0xFFFFFFFF);
  matrix.textScrollSpeed(50);
   const char text[] = "    Scrolling text!    ";
  matrix.textFont(Font_5x7);
  matrix.beginText(0, 1, 0xFFFFFF);
  matrix.println(text);
  matrix.endText(SCROLL_LEFT);

  matrix.endDraw();

  temperature = float(analogRead(A0)) * 500.0 / 1024.0;
  if (temperature < 100.0) analogWrite(fan_control_pin, 0);
  if ((temperature >= 100.0) && (temperature < 120.0)) analogWrite(fan_control_pin, 126);
  else analogWrite(fan_control_pin, 255);
 delay(1000);{
}
rpm = count * 60 / 2;
  Serial.print("The temperature is ");
  Serial.print(temperature);
  Serial.print((char)176);
  Serial.println("F");
  Serial.print("The fan speed is ");
  Serial.print(rpm);
  Serial.println("rpm");
}
void counter () {
  count++;
}

Your way to use millis() is incorrect, the lines above is equivalent to a simple delay(1000)

1 Like

would i just write this instead? or just Delay(1000)?

start_time = delay (1000)

The delay() is blocking (as well as your "millis" code). If the blocking the sketch for 1 second suit you - you can easily replace your millis to the line delay(1000)

1 Like

Clearly I need to start with basic 101, lol. I found a thread of someone explaining code blocking and I am having a hard time wrapping my head around that... I think I'll go check out that learn code section now, haha.

Okay I changed the code in the original post to just have delay(1000), idk if I need a blocking code there, but I'll keep the delay until I have a deeper understanding of coding in general.

This variable should be declared volatile because it is updated by an interrupt function and read in loop().

It's not safe to simply use count like that because it could get updated, by the interrupt function, part-way through reading it's value. You need to protect against that happening. You also need to set count to zero at some point, otherwise the RPM will keep going up!

noInterrupts();
int newCount = count;
count = 0;
interrupts();
rpm = newCount * 60 / 2;

For displaying the RPM, can't you simply use

matrix.println(rpm);

?

If not, try

sprintf(text, "%4i", rpm);
matrix.println(text);
2 Likes

I don't think think you necessarily need to remove the blocking code, but what you will need to do is accurately measure the time over which count has been increasing, using millis() or micros(). Currently your code is assuming that will be 1000ms, but you don't know how long those other statements take to execute.

1 Like

is this how i make it volatile?

int volatile count = 0;
noInterrupts();
int newCount = count;
count = 0;
interrupts();
rpm = newCount * 60 / 2;
  Serial.print("The temperature is ");
  Serial.print(temperature);
  Serial.print((char)176);
  Serial.println("F");
  Serial.print("The fan speed is ");
  Serial.print(rpm);
  Serial.println("rpm");
}
void counter () {
  count++;
}

If it compiles, it's fine. If not:

volatile int count = 0;
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.