An Arduino library to make sine waves easier

WaveCraft

A simple sine wave generator library for Arduino.

Features

  • Generates sine wave values based on frequency, amplitude, and phase
  • Provides methods for setting and getting frequency, amplitude, and phase values
  • Provides a getValue() method that returns the current sine wave value based on the current time
  • Provides a mapFloat method that maps a value from one range to another.

Methods

  • WaveCraft(float frequency, float amplitude, float phase = 0) : constructor that sets the frequency, amplitude, and phase of the wave.
  • void setAmplitude(float amplitude) : sets the amplitude of the wave
  • void setFrequency(float frequency) : sets the frequency of the wave
  • void setPhase(float phase) : sets the phase of the wave
  • void setLow(float low) : sets the minimum output value
  • void setHigh(float high) : sets the maximum output value
  • float getLow() : returns the minimum output value
  • float getHigh() : returns the maximum output value
  • float mapFloat(float x, float in_min, float in_max, float out_min, float out_max) : maps a value from one range to another
  • float getValue() : returns the current value of the sine wave

Usage

  1. Download the WaveCraft library and add it to your Arduino libraries directory
  2. Include the WaveCraft.h header in your sketch
  3. Create a WaveCraft object and set the desired frequency, amplitude, and phase values
  4. Use the getValue() method to get the current sine wave value

WaveCraft makes it very easy to make and fine tune custom sine waves.

#include <WaveCraft.h>

WaveCraft ledPulse(1, 100);
//this will create a pulse that has a frequency of 1hz and a peak to peak amplitude of 100 (-50 to +50)
void setup() {
  pinMode(13,OUTPUT);
}

void loop() {
 
  ledPulse.setFrequency(1);
  ledPulse.setLow(0);
  ledPulse.setHigh(255);

  //this will generate a pulse between 0 and 255 with a frequency of 1hz
  //the use of setLow and setHigh overides the amplitude and lets you set your own specific values.
  
  int pulse = (int)ledPulse.getValue();

  analogWrite(pulse);
  
}

#include <WaveCraft.h>

WaveCraft wave1(1, 100);
WaveCraft wave2(1, 100);

void setup() {
  Serial.begin(115200);
  pinMode(13,OUTPUT);
}

void loop() {
 
  wave1.setFrequency(1.5);
  wave1.setLow(0);
  wave1.setPhase(0);
  wave1.setHigh(255);

  wave2.setFrequency(1.5);
  wave2.setLow(0);
  wave2.setPhase(60);
  wave2.setHigh(255);
  
  Serial.print(wave1.getValue());
  Serial.print(" ");
  Serial.print(wave2.getValue());
  Serial.print(" ");
  Serial.print(255);
  Serial.print(" ");
  Serial.print(0);
  Serial.print(" ");
  Serial.println(" ");
  
}

In the above example, 2 identical sine waves are created and their properties are changed later so that they are out of phase with each other by 60 degrees.

This two function are almost the same

float WaveCraft::getValue() {
  unsigned long currentTime = millis();
  float time = (currentTime - _startTime) / 1000.0;
  float value = _amplitude * sin(2 * PI * _frequency * time + _phase);
  return mapFloat(value, -_amplitude, _amplitude, _low, _high);
}

int WaveCraft::getIntValue() {
  unsigned long currentTime = millis();
  float time = (currentTime - _startTime) / 1000.0;
  float value = _amplitude * sin(2 * PI * _frequency * time + _phase);
  return (int)mapFloat(value, -_amplitude, _amplitude, _low, _high);
}

Good style of programming is always avoiding a copy-paste the code. The second function can be represented by first:

int WaveCraft::getIntValue() {
 
  return (int) WaveCraft::getValue();
}

Truuueee ty ty. Im building massive lithium ion batteries rn so I will update that code later.

What is the available frequency range? Since the timing is done in milliseconds I'm guessing it can't go much higher than 100 Hz.

You know what, im not 100 percent sure. I believe the limitation will depend on both the speed of the chip and also the type of counter used.