1 latching toggle switch connected to pin 2 that changes the led behavior between puls or continuous. The second switch connected to pin3 is a "master switch" for the led to be on or off.
Works well with the toggle in low position and led start to pulse when the "master switch" is pressed.
But when I change the toggle switch (so its not low) the "master switch" function should be the same except that led should be continuous instad of pulse but that does not happen.
Hope my description is not too confusing!
Help needed
Thanks
int toggle;
int onoffswitch;
void setup(){
pinMode(2, INPUT_PULLUP); // toggle switch pin "LED MODE" puls or continuous
pinMode(3, INPUT_PULLUP); // led on/off pin
pinMode(11, OUTPUT); // led pin
}
void loop()
{
onoffswitch = digitalRead(3);
if(onoffswitch == LOW) {
toggle = digitalRead(2);
digitalWrite(11, HIGH);
if(toggle == LOW) {
float in, out;
for (in = 4.712; in < 10.995; in = in + 0.00700)
{
out = sin(in) * 127.5 + 127.5;
analogWrite(11,out);
delay(2);
}
}
}
}
for (in = 1.5*Pi; in < 3.5*Pi; in = in + (Pi/450))
That loop is about 900 iterations long. At 2 milliseconds delay that will be about 1.8 seconds total. During that time your 'on/off' switch won't be active.
You could check the on/off switch each time through the loop so the delay would only be 2 milliseconds:
void loop()
{
// Calculate the pulse 'out' value
for (in = 4.712; in < 10.995; in = in + 0.00700)
{
out = sin(in) * 127.5 + 127.5;
// Check the switches
if (digitalRead(3) == LOW)
{
// on/off switch is off
digitlalWrite(11, LOW); // Off
}
else
{
// on/off switch is on
if (digitalRead(2) == LOW)
{
// pulse/solid switch is set to solid
digitalWrite(11, HIGH);
}
else
{
// pulse/solid switch is set to pulse
analogWrite(11, out);
delay(2); // Pulse timing (very roughly, seconds per pulse)
}
}
}
}
It works perfect now. Your code was exactly what I needed. Thanks for your help!
Full code
int toggle;
int onoffswitch;
void setup(){
pinMode(2, INPUT_PULLUP); // toggle switch pin "LED MODE" puls or continuous
pinMode(3, INPUT_PULLUP); // led on/off pin
pinMode(11, OUTPUT); // led pin
}
void loop()
{
// Calculate the pulse 'out' value
float in, out;
for (in = 4.712; in < 10.995; in = in + 0.00700)
{
out = sin(in) * 127.5 + 127.5;
// Check the switches
if (digitalRead(3) == HIGH)
{
// on/off switch is off
digitalWrite(11, LOW); // Off
}
else
{
// on/off switch is on
if (digitalRead(2) == LOW)
{
// pulse/solid switch is set to solid
digitalWrite(11, HIGH);
}
else
{
// pulse/solid switch is set to pulse
analogWrite(11, out);
delay(2); // Pulse timing (very roughly, seconds per pulse)
}
}
}
}
Just one thing I just noticed. With the old code the pulse led always started from 0 brightness, and that was very nice. Now it seems more random. How do I get that back?
Set 'in' to 4.712 when the LED is set to LOW (off) or HIGH (on solid). Then when you switch to 'on pulsing' the loop will be at the beginning and start from there.
Only the first time through the loop. The second time it has had 0.00700 added to it. Setting it back to 4.172 will start the loop over at black: sin(1.5 Pi) * 127.5 - 127.5
code just before the analogWrite command and then it worked. Now Led pulse always start from 0 brightness on every new press
void setup(){
pinMode(2, INPUT_PULLUP); // toggle switch pin. puls/solid
pinMode(3, INPUT_PULLUP); // led on/off pin
pinMode(11, OUTPUT); // led pin
}
void loop()
{
// Check the switches
if (digitalRead(3) == HIGH)
{
// on/off switch is off
digitalWrite(11, LOW); // Off
}
else
{
// on/off switch is on
if (digitalRead(2) == LOW)
{
// pulse/solid switch is set to solid
digitalWrite(11, HIGH);
}
else
{
// Calculate the pulse 'out' value
float in, out;
for (in = 4.712; in < 10.995; in = in + 0.00700)
{
out = sin(in) * 127.5 + 127.5;
// pulse/solid switch is set to pulse
analogWrite(11, out);
delay(2); // Pulse timing (very roughly, seconds per pulse)
}
}
}
}