logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

Let’s Make Arduino LED Holiday Lighting using Proximity Sensor

Device Plus Editorial Team
Published by Device Plus Editorial Team at December 16, 2017
Categories
  • Arduino
Tags
  • Arduino
  • christmas
  • holiday
  • led

2. Customizing the program

We introduced “NeoPixel” library in our last article to control the full-color LED string or strip lights. We’ll continue using NeoPixel this time.

Below is a program for an LED string with proximity sensor as input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <Wire.h>
#include <RPR-0521RS.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6   //pin number of the signal terminal
#define LED_NUM 50  //number of LEDs
RPR0521RS rpr0521rs;
Adafruit_NeoPixel ledtape = Adafruit_NeoPixel(LED_NUM, PIN, NEO_GRB + NEO_KHZ800);  //if the control IC of the LED tape used is WS2812, the third parameter is NEO_KHZ800 and if WS2811 then NEO_KHZ400
int wait = 200;
void setup() {
  ledtape.begin();
  ledtape.show();   //reflects once in all OFF state
  
  byte rc;
  Serial.begin(9600);
  while (!Serial);
  Wire.begin();
  rc = rpr0521rs.init();
}
void loop() {
  byte rc;
  unsigned short ps_val;
  float als_val;
  byte near_far;
  Serial.println("=============================");
  
  rc = rpr0521rs.get_psalsval(&ps_val, &als_val);
  //when the sensor value is acquired
  if (rc == 0) {
    Serial.print("PS:");
    Serial.print(ps_val);
    Serial.println();
    //LEDs light up when approaching
    if(ps_val > 5){
      ps_val  = 500 - ps_val;
      if(ps_val < 0){
         ps_val = 0;
      }
      simpleLED(ps_val/10);
    }
  
    if (als_val != RPR0521RS_ERROR) {
      Serial.print("ALS:");
      Serial.print(als_val);
      Serial.println();
    }
  }
  delay(5);
}
int LEDtale[10];  //arrangement for the trajectory of LED
//
//Lights individually
//
void simpleLED(int delaytime){
  uint16_t i, j;
  j=0;
  for(i=0; i < ledtape.numPixels(); i++) {
    ledtape.setPixelColor(i, rotateColor(((i) * 256 / ledtape.numPixels()) & 255));
    LEDtale[0]  = rotateColor(((i) * 256 * 9/10 / ledtape.numPixels()) & 255);
    for(j=1; j < 10; j++){
      LEDtale[j]  = rotateColor(((i) * 256 * (10-j)/10 / ledtape.numPixels()) & 255);
      uint16_t m = i-j;
      if(m < 0){
        m += 50;
      }
      ledtape.setPixelColor(m, LEDtale[j]);
    }
    ledtape.show();
    for(j=1; j < 10; j++){
      uint16_t m = i-j;
      if(m < 0){
        m += 50;
      }
      delay(0);
      ledtape.setPixelColor(m, ledtape.Color(0,0,0));
      ledtape.show();
    }
    ledtape.setPixelColor(i, ledtape.Color(0,0,0));
    ledtape.show();
  }
  ledtape.show();
  delay(delaytime);
}
//RGB color transition function
uint32_t rotateColor(byte WheelPos) {
  if(WheelPos < 85) {
   return ledtape.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return ledtape.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return ledtape.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

We changed a couple of things in the last program we modified. First, we added the input processing of the proximity sensor. Second, we changed the way the LEDs light up.

The input of the proximity sensor should check the sample sketch from the Sensor Evaluation Kit. The purpose is to handle the numerical value entered from the sensor. If you simply respond to the sensor with an if statement, you can merely turn on / off the LEDs. By passing the value of the sensor to a function that lights the LEDs and changing the way the LEDs light up, it is possible to create more interactive lighting effect.

1
2
3
4
5
//LEDs light up when approaching
    if(ps_val > 5){
      ps_val  = 500 - ps_val;
      simpleLED(ps_val/10); //Use the sensor value to light the LEDs
    }


simpleLED
is a function that lights the LED string, but we are processing it to light the LEDs one by one from start to end of the LED string. This probably isn’t enough, so this time we’re using an array to make the LED tape shine to some extent linearly.

In the array, we keep the lighting method for 10 times before counting from the position of the currently shining LED.

1
int LEDtale[10];  //arrangement for the trajectory of LED

It’s pretty fun to play with this part of the program. Please feel free to customize according to your preference.

1 2 3
Device Plus Editorial Team
Device Plus Editorial Team
Device Plus is for everyone who loves electronics and mechatronics.

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Let’s Make Arduino LED Holiday LightingLet’s Make Arduino LED Holiday Lighting
  • Raspberry Pi WebIOPi IOT – Full-Color LED Christmas DecorationRaspberry Pi WebIOPi IOT – Full-Color LED Christmas Decoration
  • Make an LED Arduino Christmas Tree with ROHM Sensor KitMake an LED Arduino Christmas Tree with ROHM Sensor Kit
  • Glowing Christmas Snowman Using ESP-WROOM-02 & Weather APIGlowing Christmas Snowman Using ESP-WROOM-02 & Weather API
  • Using ESP-WROOM-02 Wifi Module As Arduino MCUUsing ESP-WROOM-02 Wifi Module As Arduino MCU
  • Step By Step Guide To Your First Project With ArduinoStep By Step Guide To Your First Project With Arduino
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort