I'm usually used to using the labview interface for arduino, but lately i've been trying to write my own code on the arduino compiler/uploader. I've already been able to use labview build a working 3-phase sine wave generator using the pwm outputs of my arduino uno, and the code i wrote works on relatively the same basis as the one on labview. here is how it's supposed to work:
upon pressing an external push-button, the code should enter a loop where 3 outputs are constantly updated at a rate proportional to a pot. the dead time of each of the 3 outputs is controlled by a sine function which outputs a value from 0-255, and each output has its separate equation for determining its dead-time. each sine function uses the variable x, which is supposed be the value of a counter, which by increases by pi/120 each cycle, and resetting to 0 when x=2*pi. im using 3.14 for pi. when the button is released, the 3 outputs are all supposed to go to LOW, and x is supposed to reset to 0. unfortunately, writing code for a counter is a lot harder to make than on labview, and the counter is essential for x to increase.
anyway, here is the code(the outputs are labeled led1, led2, led3, since i'm first going to test the sine wave output on LED's):
int led1 = 11;
int led2 = 10;
int led3 = 9;
const double pi = 3.14;
int x = 0;
int enable = 8;
int freqpin = A0;
double p1 = 0;
double p2 = 0;
double p3 = 0;
const int y = 1;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(enable, INPUT);
pinMode(freqpin, INPUT);
}
void loop() {
while(digitalRead(enable) == HIGH) {
x = x + y;
analogWrite(led1, p1);
analogWrite(led2, p2);
analogWrite(led3, p3);
p1 = 127.5sin(x+(2pi/3))+127.5;
p2 = 127.5sin(x+(4pi/3))+127.5;
p3 = 127.5sin(x+(2pi))+127.5;
if(x = 2*pi) x = 0;
delay(freqpin/10);
}
analogWrite(led1, 0);
analogWrite(led2, 0);
analogWrite(led3, 0);
x = 0;
}
anyway, when i upload the code and press the button, the 3 led's light up, each one at a difference brightness, as expected, but they all stay that way. the brightness on each led doesn't change, which leads me to conclude that the counter isnt working.
what changes(if any) should i make to the code to make it work?