Saturday, August 20, 2016

LED class with blink method



Below is a class I created to blink multiple LEDs at independent blink rates. This code below is the initial test of the class and shows you how to go about building a class of your own but you can save a lot of typing by downloading the library from Github using the following link:

led class code library

Just download led.cpp and led.h and put them in a library folder (call it led) where the Arduino libraries are located.

The code sample program is self explanintory. Just create an LED object using the following:

LED LED1 = LED(3); where 3 is the pin number you want to hook to the LED.

LED1.blink(100); Sets the blink rate of the LED to 100 milliseconds.

LED1.on(); Turns on the LED

LED1.off(); Turns off the LED

LED1.status(); Returns the status of the LED on or off

Have fun.


LED class and test of class ----------------------------------------------------------

class LED
{
public:
LED(int pin);
void on();
void off();
bool status();
void blink(int delayTime);

private:
int _pin;
bool _status;
int _delayTime;
int _rate;
unsigned long previousMillis = millis();
};


LED::LED(int pin)
{
  _pin = pin;
  pinMode(_pin,OUTPUT);
}

 void LED::on()
{
  digitalWrite(_pin, HIGH);
_status = 1;
}

 void LED::off()
{
  digitalWrite(_pin, LOW);
 _status = 0;}

 bool LED::status()
{
  return _status;
}

 void LED::blink(int delayTime)
{
  _delayTime = delayTime;

  if (millis() - previousMillis >= _delayTime) //if current system time - last time is greater then desired delay then make last system
  // equal to current system time
  {
    previousMillis = millis();

    if (_status == 0)
    {
      on();
    }
    else
    {
      off();
    }

  }
 }

//Test of class --------------------

LED LED1 = LED(3);
LED LED2 = LED(4);
LED LED3 = LED(2);


void setup()
{

}

void loop()
{
LED1.blink(100);
LED2.blink(1000);
LED3.blink(500);
}

Thursday, August 11, 2016

Ultrasonic Sensor

This circuit lights up dots successively as objects move further away from the ultrasonic sensor.

Modify the sensitivity and increments by changing the numbers in the "if" statement.

The 5 LED in the circuit can be used to indicate the values from a wide variety of sensors.

Wednesday, August 10, 2016

3 LED control
Traffic Light
Phase 4
This circuit provides the same sequencing as a traffic light except all the delay times are the same and must be adjusted by the circuit builder. The circuit also provides the following possibilities:

- Tiny Knight Rider light show (back and forth blink)
- 3 bit binary output
- Three level signal indication (low, medium and high)

Challenge your students or come up with your own applications that only require a software change to indicate sensor values, put on little light shows or indicate binary values.
Three LED sequencer

Alternate LED Blink

Alternate LED Blink
Phase 3

This circuit provides an alternate LED blink capability. When one LED is on the other is off and vice versa. This is a good starting point for exploring circuits with more than one LED and what these circuits are capable of doing:

- High-Low indication
- Lower 50% and upper 50% signal levels
- 2 bit binary indicator

External LED control
Fade
Phase 2


External LED Control
Blink
Phase 1

This circuit provide a simple blink capability to an LED. Many are quick to move on to another circuit before exploring the possibilities of this circuit.
  • Challenge yourself or your students to blink the LED at several frequencies and have them calculate the delay periods needed to to this
  • What is the fastest blink rate you can create and still see the blink (persistence of vision)
  • Experience with PWM and duty cycle by changing the ON/OFF ratio
  • Use a high-brightness LED and create a strobe light to explore rotating objects and make them appear to stand still
  • Shake your LED back and forth after setting a low duty cycle PWM and high duty cycle PWM and notice what you see when you look at the LED

You can read about PWM by following this link https://www.arduino.cc/en/Tutorial/PWM



Arduino Circuit
LED blink with sensor input


The circuit below blinks the LED when a light sensor threshold is reached. The threshold can be adjusted in the code. To view how it works click on Start Simulation and click on the light sensor to adjust the value of light with the slider control that appears.