void setup() {
Serial.begin(SERIAL_BAUD);
}
void loop() {
timer = millis();
int ledValue = ledValue = OFFSET + AMPLITUDE*(cos(OMEGA*timer)+PHASE);
analogWrite(LED_PIN, checkValue(ledValue));
}
int checkValue(int val) {
if (val > 255)
val = 255;
else if(val < 0)
val = 0;
return val;
}
So,if i delete some of those define syntax (not all but only that written below)and replace it with
const int OFFSET = 127;
const int AMPLITUDE = 128;
const int PERIOD = 2000;
const int OMEGA = (2*PI)/PERIOD;
than the program doesnt work properly.Led isnt blinking but its only turned on.
Please what does make that mess?
OFFSET, PERIOD, PHASE and OMEGA are all integer constants. OMEGA is 2PI/2000 and as an integer is 0.
Zero times timer (anything) will always be zero so cos(OMEGAtimer)+PHASE will always be 1. Nothing in
int ledValue = ledValue = OFFSET + AMPLITUDE(cos(OMEGA*timer)+PHASE);
will ever change so it’s calculated once at compile and replace with a constant. No further calculations necessary, optimized out.