r/ArduinoHelp • • Jun 02 '25

Help with easing distance sensor value

1 Upvotes

Hi! I'm doing a project where I'm going to use an ultrasonic sensor (HC-SR04) basically as a MIDI modulator (as if it was a mod wheel). I've noticed that at least with this sensor the value oscillates a lot and if for example I remove an object that was in front of it the value suddenly increases/drops too (which is normal I know because it's reading the next closest thing). I want to make it smoother considering the idea of the project and from what I've been researching an easing library would do that. I can't find much information about it so I was wondering if someone could help me in terms of using it or how to do that!

Just a heads up, in the code I'm mapping the distance value to MIDI making the closer the object, the higher the MIDI value and not the other way around.

Also, I already managed to get the previous midi value and present midi value so that I can use it in the process, I just don't really know how to use the easing library and can't find much information about it.

Thank you in advance :)

#include <EasingLib.h>

#include <MIDI_Controller.h>

#include <MIDI.h>
#include <MIDI.hpp>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>


MIDI_CREATE_DEFAULT_INSTANCE();

#define MIDI_CHANNEL 1  // MIDI Channel number (1 to 16, or MIDI_CHANNEL_OMNI)
#define CONTROLLER 1    // Modulation Wheel
#define MIDI_RECV 1     // Comment out to be the sender

// HC-SR04 Pins
const int trigPin = 5;
const int echoPin = 4;

int oldValue = 0;

Easing easing(ease_mode::LINEAR, 200);

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600); // baud rate

}

void loop() {
  long duration, distance;

  // Send trigger pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read echo time
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2; // Convert to cm

  int midiValue = map(distance, 0, 100, 127, 0); // Map 0-100cm to 0-127

 midiValue = constrain(midiValue, 0, 127);  // limits range of sensor values between 0 and 127



      // print the value to Serial Monitor

  Serial.print("old MIDI Value: ");
  Serial.print(oldValue);
  Serial.print("   ");
  Serial.print("MIDI Value: ");
  Serial.println(midiValue);

  
  Serial.println();
  Serial.println();

  delay(500);
  oldValue = midiValue;
}

r/ArduinoHelp • • Jun 02 '25

Help

2 Upvotes

When i conect an control module to my arduino it only registers voltage for a few secons and then... nothing! Does anyone know what this could be due to?


r/ArduinoHelp • • Jun 01 '25

question about an error

1 Upvotes

For my final project for school I use an arduino r4 wifi, I soldered Some sensors and a sd-Card reader. When I upload something to my arduino the first time (most of the time) Goes fine but then when i modify my code and try to upload I get the error message of no device found on com10 (I am using com10 my pc confirmed it). I am using an older version of the ide. I selected the right port and the right type of board.

The error / No device found on COM10 Set binary mode Send auto-baud Set binary mode An error occurred while loading the sketch


r/ArduinoHelp • • May 30 '25

Can't figure out how to rotate these numbers on my MAX7219 display

Post image
2 Upvotes

I've combined a few pieces of arduino code I found online to create a weather station which just shows the current static weather. The code works fine but the display is showing the numbers rotated 90 degrees anti-clockwise in each of these 8x8 segments. I think the problem is the section where int x and int y are defined but I dont really know! I have already tried setRotation(1) and it only shows one clipped number. The code is below, thank you in advance!

#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <JsonListener.h>
#include <time.h>
#include "OpenWeatherMapCurrent.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <time.h>

int pinCS = D8; 
int numberOfHorizontalDisplays = 4;
int numberOfVerticalDisplays   = 1;

String message;

// initiate the client
OpenWeatherMapCurrent client;
OpenWeatherMapCurrentData data;  // GLOBAL data variable

// Your OpenWeatherMap API key
String OPEN_WEATHER_MAP_APP_ID = "XXXX";

// Location settings
float OPEN_WEATHER_MAP_LOCATION_LAT = XXXX;
float OPEN_WEATHER_MAP_LOCATION_LON = XXXX;
String OPEN_WEATHER_MAP_LANGUAGE = "en";
boolean IS_METRIC = true;

// WiFi settings
#if defined(ESP8266)
const char* ESP_HOST_NAME = "esp-" + ESP.getFlashChipId();
#else
const char* ESP_HOST_NAME = "esp-" + ESP.getEfuseMac();
#endif
const char* WIFI_SSID     = "XXXX";
const char* WIFI_PASSWORD = "XXXX";

// initiate the WifiClient
WiFiClient wifiClient;

// MAX7219 display object
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);

void connectWifi() {
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected!");
  Serial.println(WiFi.localIP());
  Serial.println();
}

void fetchAndPrintWeather() {
  client.setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
  client.setMetric(IS_METRIC);
  
  client.updateCurrent(&data, OPEN_WEATHER_MAP_APP_ID, OPEN_WEATHER_MAP_LOCATION_LAT, OPEN_WEATHER_MAP_LOCATION_LON);

  Serial.println("------------------------------------");
  Serial.printf("Temp: %f\n", data.temp);
  Serial.println("---------------------------------------------------/\n");
}

void display_message(String message) {
  matrix.fillScreen(LOW);
  int x = 2;
  int y = (matrix.height() - 8) / 2;

  for (int i = 0; i < message.length(); i++) {
    matrix.drawChar(x, y, message[i], HIGH, LOW, 1);
    x += 6;
    if (x > matrix.width() - 6) break;
  }
  matrix.write();
}

unsigned long lastUpdate = 0;
const unsigned long interval = 10000; // 

void setup() {
  Serial.begin(115200);
  delay(500);
  connectWifi();

  matrix.setIntensity(5);
  matrix.setRotation(0);
  matrix.fillScreen(LOW);
  matrix.write();
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - lastUpdate >= interval) {
    lastUpdate = currentMillis;
    fetchAndPrintWeather();

    message = String(data.temp, 1) + "C";
    display_message(message);
  }
}