# Synchronus Motor via PWM and Interrupt

Hello!

I am currently trying to operate a 3~ 24V Synchronus motor via PWM. For that, I have a print with 3 half - bridges with the IR2104 controller on it, what works very well with the motor if you send a 3 phase 0-5V PWM.

To test the motor, I've written the following program:

``````int n = 0;
int x = 0;
int r = {64,88,109,123,128,123,109,88,64,40,19,5,0,5,19,40};
int s = {119,103,81,56,32,13,2,1,9,25,47,72,96,115,126,127};
int t = {9,1,2,13,32,56,81,103,119,127,126,115,96,72,47,25};    //These 3 arrays contain the previously calculated 3~ PWM, from 0 to 128.
int rpin = 10;
int spin = 11;
int tpin = 12;

void setup() {
pinMode(rpin, OUTPUT);
pinMode(spin, OUTPUT);
pinMode(tpin, OUTPUT);
}

void loop() {
if(x>=25)       //Changing that variable makes the frequency (and motor rpm) go up or down.
{

x=0;
if(n>=16) n=0;
analogWrite(rpin,r[n]);
analogWrite(spin,s[n]);
analogWrite(tpin,t[n]);
n=n+1;

}
delay(1);
x++;
}
``````

In principle that works well, but because that program works with delay I can't combine it with other programs. So I wanted to write a program which uses a timer interrupt, so I downloaded the library "Timer1" and wrote a new one:

``````#include "TimerOne.h"
int ledpin=13;
int alle_x_millisekunden=100;
int x=0;
int y=0;
int rpin = 10;
int spin = 11;
int tpin = 12;
int r = {64,88,109,123,128,123,109,88,64,40,19,5,0,5,19,40};
int s = {119,103,81,56,32,13,2,1,9,25,47,72,96,115,126,127};
int t = {9,1,2,13,32,56,81,103,119,127,126,115,96,72,47,25};
int periode=5;

void setup()   {
pinMode(rpin, OUTPUT);
pinMode(spin, OUTPUT);
pinMode(tpin, OUTPUT);
Timer1.initialize(alle_x_millisekunden*1000);
Timer1.attachInterrupt(motor);
}

void motor() {
if(y>=periode)
{
y=0;
if(x>=16) x=0;
analogWrite(rpin, r[x]);
analogWrite(spin, s[x]);
analogWrite(tpin, t[x]);
x++;

}
y++;
}

void loop()    {
// Main program
}
``````

Unfortunately, it dosen't work that way and the motor is frozen in one position. However, if I set one output pin to 13 and wath the LED, it looks like a sinus :?

Can anyone help me? (Please excuse my not so good English)

Greets, Se11001