/*
14CORE Pulsing / Breating LED Sketch
*/
int LEDPin = 11;
float sinVal;
int ledVal;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int x=0; x<180; x++) {
// convert degrees to radians then obtain the sin value
then obtain sin value
sinVal = (sin(x*(3.1412/180)));
/*
sin() function, which is a mathematical
function to work out the sine of an angle. We need to
give the function the degree in radians. We have a for
loop that goes from 0 to 179, we donʼt want to go past
halfway as this will take us into negative values and
the brightness value we need to put out to Pin 11
needs to be from 0 to 255 only.
The sin() function requires the angle to be in radians
and not degrees so the equation of x*(3.1412/180) will
convert the degree angle into radians. We then
transfer the result to ledVal, multiplying it by 255 to
give us our value. The result from the sin() function will
be a number between -1 and 1 so we need to multiply
that by 255 to give us our maximum brightness. We
ʻcastʼ the floating point value of sinVal into an integer
by the use of int() in the statement ledVal = int(sinVal*255);
Then we send that value out to Digital Pin 11 using the
statement
*/
ledVal = int(sinVal*255);
analogWrite(ledPin, ledVal);
delay(25); // Delay 2 seconds
}
}
This code doesn’t compile, it contains an error and an incorrect comment:
The error:
– It define LEDPin but then refers to ledPin. Replace one or the other to agree with the other one.
The Incorrect comment:
– The “delay(25)” does not pause for two seconds. The parameter to delay() is in milliseconds. it should be delay(2000)
None of this is really hard to fix but you may want to correct the code.
Thank you