# sine wave generator can't make a time shifting smooth one

Hello.
Now I’m going to make a sine wave generator which generates time shifting wave like a spring.

I have built a source code like the below but its performance is not smooth.

``````/*for loop で一応の目的を達成した*/

#include "Waveforms.h"

#define onefrequencySample 1000000/maxSamplesNum  // sample for the 1frequency signal expressed in microseconds
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 200;

volatile int wave1 = 0;

int i = 0;
int j = 0;
int sample;
double frequency=50.0;
double displacement=3;
/*millis for loop でプログラム作成成功*/

void setup()
{
analogWriteResolution(12);  // set the analog output resolution to 12 bit (4096 levels)
}

void loop()
{
currentMillis = millis();

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

if( (currentMillis - startMillis <= period * j ) &&(j <= 10))
{
sample = map(frequency - displacement *j, 0, 4095, 0, onefrequencySample);
sample = constrain(sample, 0, onefrequencySample);

analogWrite(DAC1, waveformsTable[wave1][i]);  // write the selected waveform on DAC1

i++;
if(i == maxSamplesNum)  // Reset the counter to repeat the wave
i = 0;

delayMicroseconds(sample);  // Hold the sample value for the sample time
}
else   if (( currentMillis - startMillis <= period *j) &&(j > 10)&&(j<21))
{
sample = map(frequency - displacement *(20-j), 0, 4095, 0, onefrequencySample);
sample = constrain(sample, 0, onefrequencySample);

analogWrite(DAC1, waveformsTable[wave1][i]);  // write the selected waveform on DAC1

i++;
if(i == maxSamplesNum)  // Reset the counter to repeat the wave
i = 0;

delayMicroseconds(sample);  // Hold the sample value for the sample time
}

/*
for(int j=10; j<20; j++)
{

if (currentMillis - startMillis <= period * j)
{
sample = map(frequency - displacement *(10-j), 0, 4095, 0, onefrequencySample);
sample = constrain(sample, 0, onefrequencySample);

analogWrite(DAC1, waveformsTable[wave1][i]);  // write the selected waveform on DAC1

i++;
if(i == maxSamplesNum)  // Reset the counter to repeat the wave
i = 0;

//      delayMicroseconds(sample);  // Hold the sample value for the sample time
}}
*/

if (currentMillis - startMillis == period * 21)
{
startMillis = currentMillis;
delayMicroseconds(sample);  // Hold the sample value for the sample time
}
}

}
``````

Waveforms.h

``````#ifndef _Waveforms_h_
#define _Waveforms_h_

#define maxWaveform 4
#define maxSamplesNum 120

static int waveformsTable[maxWaveform][maxSamplesNum] = {
// Sin wave
{
0x7ff, 0x86a, 0x8d5, 0x93f, 0x9a9, 0xa11, 0xa78, 0xadd, 0xb40, 0xba1,
0xbff, 0xc5a, 0xcb2, 0xd08, 0xd59, 0xda7, 0xdf1, 0xe36, 0xe77, 0xeb4,
0xeec, 0xf1f, 0xf4d, 0xf77, 0xf9a, 0xfb9, 0xfd2, 0xfe5, 0xff3, 0xffc,
0xfff, 0xffc, 0xff3, 0xfe5, 0xfd2, 0xfb9, 0xf9a, 0xf77, 0xf4d, 0xf1f,
0xeec, 0xeb4, 0xe77, 0xe36, 0xdf1, 0xda7, 0xd59, 0xd08, 0xcb2, 0xc5a,
0xbff, 0xba1, 0xb40, 0xadd, 0xa78, 0xa11, 0x9a9, 0x93f, 0x8d5, 0x86a,
0x7ff, 0x794, 0x729, 0x6bf, 0x655, 0x5ed, 0x586, 0x521, 0x4be, 0x45d,
0x3ff, 0x3a4, 0x34c, 0x2f6, 0x2a5, 0x257, 0x20d, 0x1c8, 0x187, 0x14a,
0x112, 0xdf, 0xb1, 0x87, 0x64, 0x45, 0x2c, 0x19, 0xb, 0x2,
0x0, 0x2, 0xb, 0x19, 0x2c, 0x45, 0x64, 0x87, 0xb1, 0xdf,
0x112, 0x14a, 0x187, 0x1c8, 0x20d, 0x257, 0x2a5, 0x2f6, 0x34c, 0x3a4,
0x3ff, 0x45d, 0x4be, 0x521, 0x586, 0x5ed, 0x655, 0x6bf, 0x729, 0x794
}
,

};

#endif m
``````

That wave looked pretty smooth to me, except the glitches, but I assume that they are when the o-scope redraws the screen because it got to the right edge.

You need to be a LOT more specific than "its performance is not smooth."

``````double frequency = 50.0;
double displacement = 3;
for (int j = 0; j <= 21; j++)
{
sample = map(frequency - displacement * j, 0, 4095, 0, onefrequencySample);
sample = constrain(sample, 0, onefrequencySample);
``````

For values of ‘j’ from 17 to 21 the value of ‘sample’ is zero (50 - 3 * 17 == -1). Perhaps that is causing the problem?