Arduino Input Simulator

I have been googling and haven't found exactly what I am looking for. I am wondering if there is a way out there to test my code by using some data that I logged from the Arduino. I am trying to calculate a few things based off a consistent cycle on one of my presses using a pressure sensor and linear potentiometer, and I don't want to continue to run my press to test my code.

I would like to be able to test my code based off two raw analog signals that I got while the machine was running.

Thanks

Sounds possible.

Please post the code you are trying to test.

Can you also add a simple drawing of the analog voltage cycle you are trying to emulate.

You might be able to program another Arduino to generate the signals, or with one Arduino you would use a millis() timer to generate values which the program will respond.

What you are asking for is not exactly a code-simulator. Because you want to simulate sensor-input.

Depending on how much values you want to "feed in" into your code you could use an array and non-blocking timing to test your code.

You should post what exact type of microcontroller you are using.

If this datastream is much more than 200 values you could use python to read in all your data and send it via serial to your microcontroller.

If it doesn't matter for your testing that the values follow a certain reproducable sequence of values you could simply use the wokwi-simulator with a potentiometer.

Below is a snapshot of an example of the cycle I would like to drive my controller with for testing. There will be two datasets I am just waiting on the pressure transducers to collect that data as well. For reference the cycle is a 24 second cycle and I am collecting data every 20 ms to get the resolution I need. I did try wokwi, but I need to drive a specific cycle in as I need to write code to determine distances and pressures at certain parts of the cycle so I need something to represent an actual cycle on one of my machines.

Currently running this on a Nano RP2040 Connect to get the 12bit analog signal.

I will look into Python, might be able to get what I want from there.

Hello arkantor56

Check and study this small example for your signal simulator project:

//https://forum.arduino.cc/t/arduino-input-simulator/1268416/4
#define ProjectName "Arduino Input Simulator"
#define NotesOnRelease "Arduino MEGA tested"

// make names
enum TimerEvent {NotExpired, Expired};
enum TimerControl {Halt, Run};

// make variables
uint32_t currentMillis = millis();
constexpr uint8_t signalOutPin {9};

// make structures
//-----------------------------------------
struct TIMER
{
  uint32_t interval;
  uint8_t control;
  uint32_t now;
  uint8_t expired(uint32_t currentMillis)
  {
    uint8_t timerEvent = currentMillis - now >= interval and control;
    if (timerEvent == Expired) now = currentMillis;
    return timerEvent;
  }
};
TIMER signalOut {1000, Run, 0};
//-----------------------------------------
struct SIGNAL
{
  uint8_t value;
  uint32_t stepTime;
};
// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application
void setup()
{
  Serial.begin(115200);
  for (uint8_t n = 0; n < 32; n++) Serial.println("");
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
  delay(2000);
  Serial.println(" =-> and off we go\n");
}
// add signal points here
SIGNAL signal[]
{
  // value, stepTime
  {0, 2000},
  {64, 2000},
  {128, 2000},
  {172, 2000},
  {255, 2000},
};


void loop()
{
  currentMillis = millis();
  heartBeat(LED_BUILTIN, currentMillis);

  if (signalOut.expired(currentMillis) == Expired)
  {
    static uint8_t element = 0;
    element = (element + 1) % (sizeof(signal) / sizeof(signal[0]));
    analogWrite(signalOutPin, signal[element].value);
    signalOut.interval = signal[element].stepTime;
  }
}

Have a nice day and enjoy coding in C++.

Thanks guys will take a look later this week and let you know how it goes.

Thanks everyone for the help here. I decided to take a stab at developing the code in excel based off a log of the signals and got it to work that way. Was easier than trying to learn a new process to do things. I still had some minor tweaking to do on the press but was able to get code 95% there with just using excel.

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