Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: 3DDD on Oct 30, 2017, 02:57 am

Title: Blinking LED at 40HZ square wave
Post by: 3DDD on Oct 30, 2017, 02:57 am
Hi Everyone,

I am trying to make a LED blink at 40HZ with square wave and I was wondering if I have the right code. Any help will be greatly appreciated.

Thanks in advance :)

int PIN = 13;

void setup() {
  pinMode(PIN, OUTPUT);
}

void loop() {
  int state = 0;

  while(1) {
    if(state == 0) {
      digitalWrite(PIN, LOW);
      state = 1;
    }
    else {
      digitalWrite(PIN, HIGH);
      state = 0;
    }
    delay(12.5);
  }
}
Title: Re: Blinking LED at 40HZ square wave
Post by: Wawa on Oct 30, 2017, 03:11 am
delay(12.5); // won't work. Integers only.
can use
delayMicroseconds(12500);
Leo..
Code: [Select]
int PIN = 13;

void setup() {
  pinMode(PIN, OUTPUT);
}

void loop() {
  digitalWrite(PIN, HIGH);
  delayMicroseconds(12500);
  digitalWrite(PIN, LOW);
  delayMicroseconds(12500);
}
Title: Re: Blinking LED at 40HZ square wave
Post by: larryd on Oct 30, 2017, 03:29 am
tone(pin, frequency)

Try:
tone(13, 40);
Title: Re: Blinking LED at 40HZ square wave
Post by: 3DDD on Oct 30, 2017, 09:15 pm
Hi Leo and Larryd, I appreciate the help, thanks alot !

Larryd, is there a difference in the light output with this code when compared to the code Leo (Wawa) posted ? If so, where do I add this code to in order to try it ?

Title: Re: Blinking LED at 40HZ square wave
Post by: larryd on Oct 30, 2017, 09:21 pm
Light o/p will be the same.

Code: [Select]
void setup()
{
    tone(13, 40);
}

void loop()
{
}   
Title: Re: Blinking LED at 40HZ square wave
Post by: Wawa on Oct 30, 2017, 09:30 pm
A human can't see a LED blinking at 40Hz.
Too fast. It will appear dim.
The frequency has to be less than ~10Hz for "blinking".

delay() is blocking code.
Try to understand the non-blocking "BlinkWithoutDelay" sketch in the examples of the IDE.

Both codes are  50% of the time fully on and 50% of the time off, so the same brightness.

If you want to change the brightness of an LED, use analogWrite() on a PWM pin.
That changes the on/off time. Humans see that as dimming.
Leo..