I'm trying to use a mirror galvo controlled by a mega 2560 to direct a laser. As a first test I want the laser to move along one axis by jumping equal intervals. (the exact frequency doesn't matter it just needs to be above ~20khz)
void setup()
{
TCCR3B = _BV(CS30); //high freq ~50khz
pinMode(5, OUTPUT);
}
void loop() {
int output1= 5;
while(output1<255){
analogWrite(5,output1);
output1+=40;
delay(500);
}
output1=5;
}
When I run this code the mirror does achieve the full 5 degree range of movement it should. The problem is instead of moving equal intervals the mirror rotates the equivalent of .8 deg over what I suppose are the first two iterations of the while loop, then the mirror jump around 3.5deg of rotation. Then the mirror goes back to rotating by small increments again .75 deg for the last few iterations of the while loop. I checked out the pwm wave on an oscilloscope which confirmed this non-uniform voltage jumping problem. I'm relatively sure there's no problem on the galvo side. update: previously I tested this program using the y-axis mirror, I just tried the x-axis one and that mirror actually does something different. It makes two larger jumps from around the .5 deg > 2.2 deg > 4.5 deg instead of the single jump from .75 to 4. I'm even more confused now because when powered by my function generator both mirrors rotated roughly equally per change in voltage.
I also tried to replicate roughly the same outcome as described above but with my own settings for registers TCCR3A and TCCR3B then adjusting OCR3A instead of analogWrite(). It didn't work at all, the mirror is rotated by a constant ~1 deg. I feel like I must have misunderstood something... Thanks for any insight and corrections (I'm really new to this).
void setup()
{
TCCR3A = TCCR3B = 0;
TCCR3A = _BV(COM3A1) | _BV(WGM31); //non-inverting
TCCR3B = _BV(CS30) | _BV(WGM32) | _BV(WGM33); //prescalar=1, fast pwm
ICR3= 500;
OCR3A= 6;
pinMode(5, OUTPUT);
}
void loop() {
while(OCR3A<500){
OCR3A+=80;
delay(500);
}
OCR3A=6;
}