hello,
i try to make a tremolo lfo out of an arduino nano. i thaught this is not very complicated. i use pwm out to write an array (squarewave, sine,sawtooth...) of 20 unsigned chars with a speed form 0,5Hz to 15Hz.
i select the waveform with a 4bit encoder triggerd by interupt.
when i take 15hz this gives me 66,66ms to write the wave (66,66ms/20chars=3,33ms/char). i thaught this should work because the pwm works with 500Hz(=2ms).
but there is something strange when i take the sinewave at 0,5Hz it takes 6300ms to write the array. with squarewave 8500ms...
in fact he writes the wave and then he stops, writes some (about 10) pulses on the output and writes the next wave >:(
it sound like a steam engine: wave, some pleep sounds, wave, some pleeps...
here is my code:
unsigned char Square1 [] = {
255,255,255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,0,0
};
unsigned char Sinewave [] = {
0,5,23,52,87,127,166,202,230,249,255,248,230,202,166,127,87,52,23,5
};
unsigned char Saw [] = {
};
unsigned char Triangle [] = {
};
unsigned char Hertz = 1;
unsigned char step = 0;
int output=9;
long DelayUS;
char waveSelect =0;
unsigned long time;
int Speedpot; //potar de speed
int Speed; //variable speed pour map
int waveSwitch1 = 3;
int waveSwitch2 = 4;
int waveSwitch4 = 5;
void setup()
{
pinMode (output, OUTPUT);
pinMode (waveSwitch1,INPUT);
pinMode (waveSwitch2,INPUT);
pinMode (waveSwitch4,INPUT);
Serial.begin(9600);
readWave(); //read the 4bit encoder on startup
attachInterrupt(1,readWave,CHANGE); //read encoder when the 1 bit from encoder changes
}
void Delay()
{
time = millis() - time; //all Serial.print and millis() lines are for debugging
Serial.print("cycletime ");
Serial.println(time,DEC);
Speed = analogRead(Speedpot); //read the speedpot
Speed = map (Speed,0,1023,5,150); //remap value from the seedpot
//Serial.print("Speed ");
//Serial.println(Speed);
DelayUS = (10000/Speed)/20; //calculate the ms to wait for 1 char
//Serial.print("millis ");
//Serial.println(DelayUS);
//Serial.print("wavselect ");
//Serial.println(waveSelect,DEC);
time = millis();
}
void readWave() //read 4 bit encoder
{
waveSelect =0;
if (digitalRead(waveSwitch1)==HIGH)
{
waveSelect = waveSelect+1;
}
if (digitalRead(waveSwitch2)==HIGH)
{
waveSelect = waveSelect+2;
}
if (digitalRead(waveSwitch4)==HIGH)
{
waveSelect = waveSelect+4;
}
}
void loop()
{
Delay();
switch(waveSelect)
{
case 0: //write squareWave
for (int i = 0; i<=20; i++)
{
analogWrite(output, Square1[i]);
Serial.print("val ");
Serial.println(Square1[i],DEC);
delay(DelayUS);
}
case 1: //write Sinewave
for (int i = 0; i<=20; i++)
{
analogWrite(output, Sinewave[i]);
delay(DelayUS);
}
case 2: //write sawtooth
for (int i = 0; i<=20; i++)
{
analogWrite(output, Saw[i]);
delay(DelayUS);
}
case 3: //write triangle
for (int i = 0; i<=20; i++)
{
analogWrite(output, Triangle[i]);
delay(DelayUS);
}
}
}
my question is why it doesn't sound 'round'. where are these pulses comming from and what can i do to get it working? :-/
this my first time critical project...