반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

[Arduino] 3rd project. 4X4X4 LED Light Cube

2017. 6. 5. 10:22 | Posted by 솔웅


반응형

I've purchased 4X4X4 LED Light Cube kit from Aliexpress. ($3.79)

4X4X4 Blue LED Light Cube Kit 3D LED DIY Kit Electronic Suite for Arduino







You can get the instruction from here. but There is not enough information to complete the set.

Fortunately, got perfect info from this blog.

https://emalliab.wordpress.com/2015/07/24/icstation-4x4x4-led-cube-shield-for-arduino-hardware-build/

 https://emalliab.wordpress.com/2015/07/24/icstation-4x4x4-led-cube-shield-for-arduino-software/


So I've followed it and could complete the set like this.




This is the blog (2nd one) explained about the source code.


icStation 4x4x4 LED Cube Shield for Arduino – Software



July 24, 2015 at 10:31 pm (computers) ()


As described in my previous post, I now have a working (I’ve run the demo code!) icStation 4x4x4 LED cube shield connected up to an Arduino Uno.  Unfortunately as I started to dig around in the sample library, I just couldn’t quite work out how it was meant to address each plane of the cube.  Eventually I decided it wasn’t possible, due to a number of bugs in the code.


이전 게시물에서 설명한 것처럼, icStation 4x4x4 LED cube shield를 Arduino Uno에 연결하는 작업을 하고 있습니다. 불행하게도 샘플 라이브러리를 파고들 수록 큐브의 각 면을 지정하는 방법에 대해 이해하기가 더 어려워 졌습니다. 결국 해당 샘플 코드에 버그가 많아 제대로작동할 수 없다는 결론에 이르렀습니다.


The demo code worked as the code just showed the same pattern on each of the four planes.  This is largely because the initialisation routines set the four Ardunio data pins to low, and then never did anything else with them!  There is some mention of pins 16 and 17, but no mention of pins 18 and 19, and actually in the main display routines, there is nothing done to actually set any of these pins once initialisation is complete.  So at this point I decided to read a bit more about the 74HC595 and just go it alone.  Attached is the result.  Feel free to use as you see fit.


그 데모 코드는 4개의 면 각각이 동일한 패턴으로 보여지도록 작동하고 있습니다. 초기화 루틴이 4 개의 Ardunio 데이터 핀을 로우로 설정 한 후 그것을 아무곳에서도 사용하지 않습니다. 핀 16과 핀 17에 대해서는 언급이 있지만, 핀 18과 19에 대한 언급이 없습니다, 메인 디스플레이 루틴에서, 초기화가 완료되면 실제로 이러한 핀들을 설정하는 일은 없습니다. 그래서 이 시점에서 필자는 74HC595에 대해 좀 더 읽고 혼자서 결정했습니다. 첨부 된 결과입니다. 이것을 토대로 당신이 적합하다고 생각하는대로 자유롭게 사용하십시오.


Basic initialisation


The two shift registers seem fairly simple to setup.  The Arduino needs some control pins setting up as outputs alongside the four digital outs used for the LEDs.  The initialisation is quite straight forward as follows:


두 개의 시프트 레지스터는 설정하는 것이 매우 간단합니다. Arduino는 LED에 사용되는 4 개의 디지털 출력과 함께 출력으로 설정되는 일부 제어 핀을 필요로합니다. 초기화는 다음과 같이 매우 간단합니다.


#include <Arduino.h>

int HC595_clockPin=0;   // SH_CP of 74HC595 
int HC595_latchPin=1;   // ST_CP of 74HC595 
int HC595_dataPin=3;    // DS of 74HC595 
int HC595_enablePin=2;  // Not OE of 74HC595
int LED_Pin16= 4;
int LED_Pin17= 5;
int LED_Pin18= 6;
int LED_Pin19= 7;

void setup() {
  // put your setup code here, to run once:
  pinMode( HC595_latchPin,  OUTPUT );
  pinMode( HC595_clockPin,  OUTPUT );
  pinMode( HC595_dataPin,   OUTPUT );
  pinMode( HC595_enablePin, OUTPUT );
  
  pinMode( LED_Pin16, OUTPUT );
  pinMode( LED_Pin17, OUTPUT );
  pinMode( LED_Pin18, OUTPUT );
  pinMode( LED_Pin19, OUTPUT );
  
  digitalWrite(LED_Pin16,HIGH);
  digitalWrite(LED_Pin17,HIGH);
  digitalWrite(LED_Pin18,HIGH);
  digitalWrite(LED_Pin19,HIGH);
//  digitalWrite(HC595_enablePin, LOW);  // Enable Not OE (negative logic)
}


I never did quite work out if I needed to do anything with the enable pin, but as it is active low, I assumed that by not doing anything, everything would be enabled by default anyway.

enable pin과 관련해서는 아무것도 하지 않았습니다. active 상태 값이 low 이기 때문입니다. 이렇게 아무것도 정의하지 않으면 디폴트로  enable이 될 것으로 생각 됩니다.


In terms of actually writing a value to the 74HC595, again that is fairly straight forward.

74HC595에 적용된 값들은 그냥 보시는 대로 입니다. 



/*
  Protocol for sending the data to the hc595 is as follows:
   (see: http://www.arduino.cc/en/Tutorial/ShiftOut)
   
   "when the clock pin goes from low to high, the shift register
    reads the state of the data pin ... when the latch pin goes
    from low to high the sent data gets moved from the shift
    registers ... to the output pins"
   
   As we have two HC595s chained together, we use a 16 bit input value
*/
void write_74HC595 (unsigned int hc595value) { 
   digitalWrite(HC595_latchPin, LOW);   // ensures LEDs don't light whilst changing values
   
   // Shift each 8 bit value in sequence - the two chained HC595s automatically grab
   // the right bits - the first 8 to the first chip, second 8 to the second chip
   shiftOut(HC595_dataPin, HC595_clockPin, LSBFIRST, hc595value);  
   shiftOut(HC595_dataPin, HC595_clockPin, LSBFIRST, (hc595value >> 8));
   
   digitalWrite(HC595_latchPin, HIGH);  // data transferred from shift register to outputs when latch goes LOW->HIGH
}


So 16 bits of the hc595value variable are sent in two 8-bit chunks over the serial port to the shift registers and with the appropriate signalling via the latch pin, that is basically that.  This sets the outputs of the 74HC595 to high, but in order to make the LEDs come on, the Arduino data pins corresponding to the horizontal planes must be set low.  If a different pattern is required for each plane, then some kind of simple ‘scanning’ is required as illustrated below.



따라서 hc595value 변수의 16 비트는 두 개의 8 비트 chunk로 직렬 포트를 통해 shift register로 전송 되며 이것은 latch pin을 통해 적절한 신호로 전송됩니다. 이것은 74HC595의 출력을 high로 설정하지만 LED를 켜기 위해 수평면에 해당하는 Arduino 데이터 핀을 low로 설정 해야합니다. 각 평면에 다른 패턴이 필요한 경우 아래에 설명 된 것처럼 일종의 간단한 '스캐닝'이 필요합니다.



int LED_Plane[] = {LED_Pin16, LED_Pin17, LED_Pin18, LED_Pin19};

/*
  Inputs: Array of 4 integers - one for each plane
*/
void display (unsigned int *pPattern)
{
  int i;
  for (i=0; i<4; i++)
  {
    int j;
    for (j=0; j<1000; j++)
    {
      // Slow this down so that there is time for the LEDS to light
      // Experimentation shows that 200+ gives brighter LEDs
      // NB: Do it this way so an empty loop isn't optimised out
      if (j==0)
      {
        digitalWrite(LED_Plane[0], HIGH);
        digitalWrite(LED_Plane[1], HIGH);
        digitalWrite(LED_Plane[2], HIGH);
        digitalWrite(LED_Plane[3], HIGH);
        write_74HC595 (pPattern[i]);
        digitalWrite(LED_Plane[i], LOW);
      }
    }
  }
}


I guess the only thing to really note here is that without the loop involving j then the ‘scanning’ was happening too fast to allow the LEDs to have any significant brightness at all.  I initially had a simple for (j=0; j<1000; j++) {}; statement at the end, but this seemed to be optimised out – at least it didn’t seem to have the delay effect I wanted, so instead I made the code only act on one pass through the loop.


여기서 정말로 주목할 것은, j를 포함하는 루프가 없으면 '스캐닝'이 너무 빠르게 진행 되어 LED가 충분히 밝게 되지 않는 다는 겁니다. 나는 처음에는  간단한 for 문을 (j = 0; j <1000; j ++) {}; 를 끝에 사용했습니다.  이것이 최적화 된 것처럼 보였지만 내가 원한 delay 효과를 보여주지는 못했습니다. 그래서 그렇게 하는 대신에 loop를 통과하는 것을 한번만 실행되도록 코딩했습니다. 



So, with these basics, it is now possible to get a 64 bit pattern, in the form of four 16 bit values, each representing one plane of 16 LEDs, onto the cube.


이렇게 함으로서 64 bit pattern이 가능해 졌습니다. 4개의 16 비트 값 형태로 각각이 큐브의 16 LEDs 의 한 평면을 나타내게 됩니다.



I have the full demo code file below – it isn’t massively pretty, and its not cpp (sorry – I’ve always been more of a C person I’m afraid, and my C is a little rusty), but it works for me.  Your proverbial mileage, as they say, may vary.


아래에 전체 코드가 있습니다. 코드가 깔끔하지 않고 cpp 로 돼 있지 않습니다. (나는 C 언어에 대해 그렇게 능숙하지가 못해서요.) 아무튼 이 소스코드는 잘 작동합니다. 



Kevin




#include <Arduino.h>

int HC595_clockPin=0;   // SH_CP of 74HC595 
int HC595_latchPin=1;   // ST_CP of 74HC595 
int HC595_dataPin=3;    // DS of 74HC595 
int HC595_enablePin=2;  // Not OE of 74HC595
int LED_Pin16= 4;
int LED_Pin17= 5;
int LED_Pin18= 6;
int LED_Pin19= 7;
int LED_Plane[] = {LED_Pin16, LED_Pin17, LED_Pin18, LED_Pin19};

// Each line (8 bytes) is an entire cube, with two consecutive bytes per plane of LEDS,
// and 16 LEDS per plane. LEDs are encoded in the following order:
//    Lowest plane byte 1, lowest plane byte 2, second lowerst plane 1, then 2,
//    second from top plane 1, then 2, highest plane 1, highest plane 2.
//
//    Each plane is encoded looking at the Arduino oriented with the USB/power
//    designated by 'south' by started 'north west' as follows:
//        D0    D1    D2    D3
//        D4    D5    D6    D7
//        D8    D9    D10   D11
//        D12   D13   D14   D15
//
//        D16   D17   D18   D19
//          (USB)      (Power)
//    With D16 being the lowest plane, through to D19 being the highest plane
//    Of course, if you wire the planes up differently, that is up to you!
//
//    Each two bytes of the pattern are therefore:
//        B00000000, B00000000 -> D0-7, D8-15
//    with D0 = msb of the first value, D7 being the lsb of the first value,
//    and  D8 = msb of the second value, D15 being the lsb of the second value.
//
//    So the entire pattern is:
//    B10010000,B00001001,B00000000,B00000000,B00000000,B00000000,B10010000,B00001001,
//     |      |  |     ||                                          |      |  |     ||
//     |      |  |     |\ D15 bottom plane                         |      |  |     |\ D15 top plane
//     |      |  |     \ D14 bottom plane                          |      |  |     \ D14 top plane
//     |      |  \ D8 bottom plane                                 |      |  \ D8 top plane
//     |      \ D7 bottom plane                                    |      \ D7 top plane
//     \ D0 bottom plane                                           \ D0 top plane
//
// Comment following in or out to switch patterns in or out
#define SWAP   1
#define SNAKE  1
#define BURST  1
#define SPIRAL 1
#define ALT    1
unsigned char pattern[] = {
#ifdef SWAP
  B10010000,B00001001,B00000000,B00000000,B00000000,B00000000,B10010000,B00001001,
  B00000000,B00000000,B10010000,B00001001,B10010000,B00001001,B00000000,B00000000,
  B00000000,B00000000,B01100000,B00000110,B01100000,B00000110,B00000000,B00000000,
  B01100000,B00000110,B00000000,B00000000,B00000000,B00000000,B01100000,B00000110,
  B00001001,B10010000,B00000000,B00000000,B00000000,B00000000,B00001001,B10010000,
  B00000000,B00000000,B00001001,B10010000,B00001001,B10010000,B00000000,B00000000,
  B00000000,B00000000,B00000110,B01100000,B00000110,B01100000,B00000000,B00000000,
  B00000110,B01100000,B00000000,B00000000,B00000000,B00000000,B00000110,B01100000,
#endif
#ifdef SNAKE
  B11001100,B00000000,B11001100,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B11001100,B00000000,B11001100,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B11001100,B00000000,B11001100,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00001100,B11000000,B00001100,B11000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B11001100,B00000000,B11001100,
  B00000000,B00000000,B00000000,B00000000,B00000000,B01100110,B00000000,B01100110,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00110011,B00000000,B00110011,
  B00000000,B00000000,B00000000,B00110011,B00000000,B00110011,B00000000,B00000000,
  B00000000,B00110011,B00000000,B00110011,B00000000,B00000000,B00000000,B00000000,
  B00000011,B00110000,B00000011,B00110000,B00000000,B00000000,B00000000,B00000000,
  B00110011,B00000000,B00110011,B00000000,B00000000,B00000000,B00000000,B00000000,
  B01100110,B00000000,B01100110,B00000000,B00000000,B00000000,B00000000,B00000000,
#endif
#ifdef BURST
  B00000000,B00000000,B00000110,B01100000,B00000110,B01100000,B00000000,B00000000,
  B00000110,B01100000,B01101001,B10010110,B01101001,B10010110,B00000110,B01100000,
  B01101001,B10010110,B10010000,B00001001,B10010000,B00001001,B01101001,B10010110,
  B10010000,B00001001,B00000000,B00000000,B00000000,B00000000,B10010000,B00001001,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
#endif
#ifdef SPIRAL
  B11001100,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B01100110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00110011,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000011,B00110000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00110011,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B01100110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B11001100,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00001100,B11000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B11001100,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B11001100,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B01100110,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00110011,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000011,B00110000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00110011,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B01100110,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B11001100,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00001100,B11000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B11001100,B00000000,B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B11001100,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B01100110,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00110011,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00000011,B00110000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00110011,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B01100110,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B11001100,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00001100,B11000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B11001100,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11001100,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01100110,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00110011,B00000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000011,B00110000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00110011,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B01100110,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11001100,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001100,B11000000,
  B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11001100,B00000000,
#endif
#ifdef ALT
  B11111001,B10011111,B10010000,B00001001,B10010000,B00001001,B11111001,B10011111,
  B00000110,B01100000,B01101001,B10010110,B01101001,B10010110,B00000110,B01100000,
  B00000000,B00000000,B00000110,B01100000,B00000110,B01100000,B00000000,B00000000,
  B00000110,B01100000,B01101001,B10010110,B01101001,B10010110,B00000110,B01100000,
#endif
};
int patternNumber=0;
int numPatterns=sizeof(pattern)/8;
int tickCount=0;
int tickCountMax=50;      // How many times to loop before changing the pattern
unsigned int currentPattern[4];

void setup() {
  // put your setup code here, to run once:
  pinMode( HC595_latchPin,  OUTPUT );
  pinMode( HC595_clockPin,  OUTPUT );
  pinMode( HC595_dataPin,   OUTPUT );
  pinMode( HC595_enablePin, OUTPUT );
  
  pinMode( LED_Pin16, OUTPUT );
  pinMode( LED_Pin17, OUTPUT );
  pinMode( LED_Pin18, OUTPUT );
  pinMode( LED_Pin19, OUTPUT );
  
  digitalWrite(LED_Pin16,HIGH);
  digitalWrite(LED_Pin17,HIGH);
  digitalWrite(LED_Pin18,HIGH);
  digitalWrite(LED_Pin19,HIGH);
//  digitalWrite(HC595_enablePin, LOW);  // Enable Not OE (negative logic)

  patternNumber=0;
  tickCount = tickCountMax;
}

/*
  Protocol for sending the data to the hc595 is as follows:
   (see: http://www.arduino.cc/en/Tutorial/ShiftOut)
   
   "when the clock pin goes from low to high, the shift register
    reads the state of the data pin ... when the latch pin goes
    from low to high the sent data gets moved from the shift
    registers ... to the output pins"
   
   As we have two HC595s chained together, we use a 16 bit input value
*/
void write_74HC595 (unsigned int hc595value) { 
   digitalWrite(HC595_latchPin, LOW);   // ensures LEDs don't light whilst changing values
//   digitalWrite(HC595_enablePin, HIGH); // OE is negative logic
   
   // Shift each 8 bit value in sequence - the two chained HC595s automatically grab
   // the right bits - the first 8 to the first chip, second 8 to the second chip
   shiftOut(HC595_dataPin, HC595_clockPin, LSBFIRST, hc595value);  
   shiftOut(HC595_dataPin, HC595_clockPin, LSBFIRST, (hc595value >> 8));
   
   digitalWrite(HC595_latchPin, HIGH);  // data transferred from shift register to outputs when latch goes LOW->HIGH
//   digitalWrite(HC595_enablePin, LOW);  // re-enable (negative logic again)
}

/*
  Inputs: Array of 4 integers - one for each plane
*/
void display (unsigned int *pPattern)
{
  int i;
  for (i=0; i<4; i++)
  {
    int j;
    for (j=0; j<1000; j++)
    {
      // Slow this down so that there is time for the LEDS to light
      // Experimentation shows that 200+ gives brighter LEDs
      // NB: Do it this way so an empty loop isn't optimised out
      if (j==0)
      {
        digitalWrite(LED_Plane[0], HIGH);
        digitalWrite(LED_Plane[1], HIGH);
        digitalWrite(LED_Plane[2], HIGH);
        digitalWrite(LED_Plane[3], HIGH);
        write_74HC595 (pPattern[i]);
        digitalWrite(LED_Plane[i], LOW);
      }
    }
  }
}

void displayPattern ()
{
  int i;
  // only update it every tick otherwise just display as is
  tickCount--;
  if (tickCount <= 0)
  {
    tickCount = tickCountMax;
    for (i=0; i<4; i++)
    {
      currentPattern[i] = pattern[i*2 + patternNumber*8] * 256 + pattern[i*2 + 1 + patternNumber*8];
    }
    patternNumber++;
    if (patternNumber >= numPatterns)
    {
      patternNumber = 0;
    }
  }
  display(&currentPattern[0]);
}

void loop() {
  // put your main code here, to run repeatedly:
  displayPattern();
}



반응형


반응형

Simple version of LED Light stand base for 3D crystal laser cube



I have Taj Mahal 3D crystal laser Cube.

You can get it from ebay also.





I found there are many stand bases for these kind of 3D crystal cube and it makes gorgeous view of it. like below.



So I've decided to develop it using Arduino.


The result is this.





This is the Source code.


/*

  For LED Light stand for 3D Crystal laser cube


  version : 0.4


  created 03 May 2017


  by Douglas Changsoo Park

*/


// Define Pins

#define RED1 3

#define GREEN1 4

#define BLUE1 5

#define RED2 6

#define GREEN2 7

#define BLUE2 8

#define delayTime 100


// define variables

int redValue1;

int greenValue1;

int blueValue1;

int redValue2;

int greenValue2;

int blueValue2;


void setup() {

  pinMode(RED1, OUTPUT);

  pinMode(GREEN1, OUTPUT);

  pinMode(BLUE1, OUTPUT);

  digitalWrite(RED1, HIGH);

  digitalWrite(GREEN1, HIGH);

  digitalWrite(BLUE1, HIGH);


  pinMode(RED2, OUTPUT);

  pinMode(GREEN2, OUTPUT);

  pinMode(BLUE2, OUTPUT);

  digitalWrite(RED2, HIGH);

  digitalWrite(GREEN2, HIGH);

  digitalWrite(BLUE2, HIGH);


  redValue1 = random(1, 255);

  greenValue1  = random(1, 255);

  blueValue1  = random(1, 255);

  redValue2 = random(1, 255);

  greenValue2  = random(1, 255);

  blueValue2  = random(1, 255);

}


void loop() {

  LEDLights(redValue1, greenValue1, blueValue1, redValue2, greenValue2, blueValue2);

  delay(delayTime * 4);

}


void LEDLights(int red1, int green1, int blue1, int red2, int green2, int blue2) {


  redValue1 = red1;

  greenValue1 = green1;

  blueValue1 = blue1;

  redValue2 = red2;

  greenValue2 = green2;

  blueValue2 = blue2;


  int redResult1 = randomValue(redValue1);

  int greenResult1 = randomValue(greenValue1);

  int blueResult1 = randomValue(blueValue1);


  int redResult2 = randomValue(redValue2);

  int greenResult2 = randomValue(greenValue2);

  int blueResult2 = randomValue(blueValue2);


  for (int i = 0; i < 25; i += 1) {

    analogWrite(RED1, redValue1 + redResult1);

    analogWrite(GREEN1, greenValue1 + greenResult1);

    analogWrite(BLUE1, blueValue1 + blueResult1);

    analogWrite(RED2, redValue2 + redResult2);

    analogWrite(GREEN2, greenValue2 + greenResult2);

    analogWrite(BLUE2, blueValue2 + blueResult2);


    redValue1 = redValue1 + redResult1;

    greenValue1 = greenValue1 + greenResult1;

    blueValue1 = blueValue1 + blueResult1;

    redValue2 = redValue2 + redResult2;

    greenValue2 = greenValue2 + greenResult2;

    blueValue2 = blueValue2 + blueResult2;


    delay(delayTime);

  }

}


int randomValue(int value) {

  int result;

  int minN;

  int maxN;


  int interval = random(1, 5);


  if (interval == 1) {

    minN = 26;

    maxN = 229;

  } else if (interval == 2) {

    minN = 51;

    maxN = 204;

  } else if (interval == 3) {

    minN = 76;

    maxN = 179;

  } else if (interval == 4) {

    minN = 101;

    maxN = 154;

  } else {

    minN = 126;

    maxN = 129;

  }


  if (value < minN) {

    result = interval;

  } else if (value > maxN) {

    result = -interval;

  } else {

    int randomVal = random(0, 1);

    if (randomVal == 0) {

      result = interval;

    } else {

      result = -interval;

    }

  }


  return result;

}




I wanted to make a combination and change of a more varied and softer color using random() in randomValue() function.


LEDLights() function will turn on and change the LED Light with the return value of the randomValue() function.


The loop() function will call the LED LIghts() function repeatedly every given time.


The components will be configured as below.





I've used Arduino Nano, 12V battery pack with 8 AAA batteries, 1 car charger adapter, small bread board and 2 RGB LEDs.


all parts are what I already have. 


It will cost about $ 15 for a new purchase.



Link to my project in hackster.


https://www.hackster.io/solkit/portable-led-light-stand-for-3d-crystal-laser-cube-5d8bd1







반응형


반응형

I don't like you

Practice scripting for the project 'I like you'


Source Code


/*

  For I don't like you. (practice project for the project 'I like you')


  version : 0.1


  created 07 May 2017


  by Douglas Changsoo Park

*/


#include <Servo.h>

#include <NewPing.h>


#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.

#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

Servo myservo;//create servo object to control a servo


int distance;


void setup() {

  myservo.attach(9);//attachs the servo on pin 9 to servo object

  myservo.write(0);//back to 0 degrees 

  delay(1000);//wait for a second


  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.

}


void loop() {

  delay(500);  // 

  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

  Serial.print("Ping: ");

  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)

  Serial.println("cm");

 

  distance = uS / US_ROUNDTRIP_CM;


  delay(500);

  // Servo motor moves when the distance is less than 5cm

  if (distance < 5 && distance != 0) {

    Serial.println("distance : ");

    Serial.println(distance); 

    myservo.write(90);//goes to 45 degrees 

    delay(500);

    myservo.write(-90);//goes to 45 degrees

    delay(500);

  }


}


==> Servo motor will move 90 degree and -90 degree when ultrasonic sensor detects object (person) in 5 cm.
Ultrasonic sonic sensor is going to be a pet's head so it looks like the pet is shaking his head because he doesn't like the person.

==> This is practice project of the project "I like you"
with the project, As you move, the pet will keep looking at you.
So the project name is "I like you" 

==> to do that I am planning to use 2 (or 3) ultrasonic sensor.

Anyway for the practice project "I don't like you", Arduino components should be configured as follows.



It will print the distance in 'cm' if it is run.




반응형


반응형

MIT App Inventor



Today I've developed Mega Millions Jackpot number generator using MIT App Inventor.

MIT App Inventor is GUI Android Mobile App Development tool.



Wikipedia

App Inventor for Android

App Inventor for Android is an open-source web application originally provided by Google, and now maintained by the Massachusetts Institute of Technology (MIT).

It allows newcomers to computer programming to create software applications for the Android operating system (OS). It uses a graphical interface, very similar to Scratch and the StarLogo TNG user interface, which allows users to drag-and-drop visual objects to create an application that can run on Android devices. In creating App Inventor, Google drew upon significant prior research in educational computing, as well as work done within Google on online development environments.[1]



MIT App Inventor Website


What is MIT App Inventor?

MIT App Inventor is an innovative beginner's introduction to programming and app creation that transforms the complex language of text-based coding into visual, drag-and-drop building blocks. The simple graphical interface grants even an inexperienced novice the ability to create a basic, fully functional app within an hour or less.






This is the Mega Jackpot Number generator what I've developed today.





This is the apk file. so you can install in your android phone if you have.


LotteryNumber.apk





See the graphical design of the app here.




Here is core logic of the app.





The first 'for' statement is the one to get 5 quick pick numbers.

The second 'for' statement makes list who has numbers from 1~75.

3rd 'for' statement will remove 70 items from the list randomly.

so we will get 5 jackpot numbers for Mega Million.


The good point of this logic is simplification.


If you pickup random numbers from the 75 list then you need to use double 'for' statement to sort the numbers.

The logic is going to be much more complicated.


Above logic is much simpler because it is already sorted.




반응형


반응형

Grew up like this in 4 days. :)





In a meanwhile, I've updated the source code.


/*
* For Automated Mung Bean Sprouts growing machine
*
* Watering every 2 hours
* play music when watering
*
 * Relay pin 12
* speaker pin 8
*
 * created 22 Jan 2017
 *
 * updated 24 Jan 2017
 * -Added one more song (Butterfly)
 * -Randomly select a song (Schoolbell or Butterfly)
 * -watering every an hour from every 2 hours.
 * -increase watering time (Schoolbell - play 3 times, Butterfly - play 2 times)
*
 *by Douglas Changsoo Park
*
 */
 
#include <SimpleTimer.h>
#include "pitches.h"
 
int relay = 12;
int melodyPin = 8;
 
SimpleTimer timer;
 
int schoolbell_melody[] = {
  NOTE_G4,  NOTE_G4,  NOTE_A4,  NOTE_A4,  NOTE_G4,  NOTE_G4,  NOTE_E4,  NOTE_G4,  NOTE_G4,  NOTE_E4,  NOTE_E4,  NOTE_D4,  0,
  NOTE_G4,  NOTE_G4,  NOTE_A4,  NOTE_A4,  NOTE_G4,  NOTE_G4,  NOTE_E4,  NOTE_G4,  NOTE_E4,  NOTE_D4,  NOTE_E4,  NOTE_C4,  0 };
 
int schoolbell_noteDurations[] = {  1,1,1,1,  1,1,2,  1,1,1,1,  3,1,  1,1,1,1,  1,1,2,  1,1,1,1,  3,1  };

int butterfly_melody[] = {
  NOTE_G4,NOTE_E4,NOTE_E4,NOTE_F4,NOTE_D4,NOTE_D4,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_G4,NOTE_G4,
  NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_F4,NOTE_D4,NOTE_D4,NOTE_C4,NOTE_E4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,0,
  NOTE_D4,NOTE_D4,NOTE_D4,NOTE_D4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_E4,NOTE_F4,NOTE_G4,
  NOTE_G4,NOTE_E4,NOTE_E4,NOTE_F4,NOTE_D4,NOTE_D4,NOTE_C4,NOTE_E4,NOTE_G4,NOTE_G4,NOTE_E4,NOTE_E4,NOTE_E4,0
};

int butterflyNoteDurations[] = {
  1,1,2,1,1,2, 1,1,1,1,1,1,2,  1,1,1,1,1,1,2, 1,1,1,1,1,1,3,1,
  1,1,1,1,1,1,2,1,1,1,1,1,1,2,  1,1,2,1,1,2,1,1,1,1,1,1,3,1
};

void setup() {
  // put your setup code here, to run once:
  pinMode(relay, OUTPUT);
 digitalWrite(relay, HIGH);
  //timer.setInterval(7200000, watering);
  timer.setInterval(3600000, watering);
  //timer.setInterval(25000, watering); // Test Mode
}
 
void loop() {
  // put your main code here, to run repeatedly:
  timer.run();
}
 
void watering() {
  digitalWrite(relay, LOW);
  int song = random(1,3);
  if(song == 1) {
    music_schoolBell();
    music_schoolBell();
    music_schoolBell();
  } else {
    music_butterfly();
    music_butterfly();
  }
  digitalWrite(relay, HIGH);
  noTone(8);
}
 
void music_schoolBell() {
    int size = sizeof(schoolbell_melody) / sizeof(int);
  for(int thisNote = 0; thisNote < size; thisNote++) {
      int noteDuration = 250 * schoolbell_noteDurations[thisNote];
      tone(melodyPin, schoolbell_melody[thisNote], noteDuration);
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      noTone(melodyPin);
  }
}

void music_butterfly(){
  int size = sizeof(butterfly_melody) / sizeof(int);
  for (int thisNote = 0; thisNote < size; thisNote++) {
      int noteDuration = 250 * butterflyNoteDurations[thisNote];
      tone(melodyPin, butterfly_melody[thisNote], noteDuration);
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      noTone(melodyPin);
  }
}


반응형


반응형

Automated Mung Bean Sprouts growing machine

(자동 콩나물 재배기)




※ How to grow mung bean sprouts


1. Rinse over beans and soak around for a day (24 hours)
2. Place net in pot and spread beans on the net and then cover the pot with something to make inside dark.
3. watering every 2 hours ==> Automate this step



※ Target : develop automated watering machine using Arduino



※  Components and Supplies



- Arduino Uno : $13.98



- 12V mini water pump  : $7.72



- 1 channel Relay : $3.37



- 12V Adapter + Barrel Jack : $5.3


- Small Breadboard and some jumper wires


- a piece of hose





※ Circuit




※ Source Code


/*
* For Automated Mung Bean Sprouts growing machine
*
* Watering every 2 hours
* play music when watering
*
 * Relay pin 12
* speaker pin 8
*
 * created 22 Jan 2017
*
 *by Douglas Changsoo Park
*
 */
 
#include <SimpleTimer.h>
#include "pitches.h"
 
int relay = 12;
 
SimpleTimer timer;
 
int melody[] = {
  NOTE_G4,
  NOTE_G4,
  NOTE_A4,
  NOTE_A4,
  NOTE_G4,
  NOTE_G4,
  NOTE_E4,
  NOTE_G4,
  NOTE_G4,
  NOTE_E4,
  NOTE_E4,
  NOTE_D4,
  0,
  NOTE_G4,
  NOTE_G4,
  NOTE_A4,
  NOTE_A4,
  NOTE_G4,
  NOTE_G4,
  NOTE_E4,
  NOTE_G4,
  NOTE_E4,
  NOTE_D4,
  NOTE_E4,
  NOTE_C4,
  0 };
 
  int noteDurations[] = {
  1,1,1,1,
  1,1,2,
  1,1,1,1,
  3,1,
  1,1,1,1,
  1,1,2,
  1,1,1,1,
  3,1
  };
 
void setup() {
  // put your setup code here, to run once:
  pinMode(relay, OUTPUT);
 digitalWrite(relay, HIGH);
  timer.setInterval(7200000, watering);

}
 
void loop() {
  // put your main code here, to run repeatedly:
  timer.run();
}
 
void watering() {
  digitalWrite(relay, LOW);
  music();

  music();

  digitalWrite(relay, HIGH);
  noTone(8);
}
 
void music() {
  for(int thisNote = 0; thisNote < 26; thisNote++) {
      int noteDuration = 250 * noteDurations[thisNote];
      tone(8, melody[thisNote], noteDuration);
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      noTone(8);
  }
}




※ Picture





※ Youtube



반응형

[Arduino] Motion Sensor and Water Level Sensor

2017. 1. 17. 07:56 | Posted by 솔웅


반응형

더 재밌게 놀기 연구소



※ Motion Seosor (HC-SR501)



Data sheet :

HC-SR501_Datasheet.pdf



Motion sensor will work without Arduino

When the sensor detects motion, the output pin will go "high"


* Using Arduino


Circuit



Source Code


//www.elegoo.com
//2016.06.13

/*
 * PIR sensor tester
 */
 
int ledPin = 13;                // choose the pin for the LED
int inputPin = 7;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}


Result





※ Water Level Sensor


Circuit



Source Code


//www.elegoo.com
//2016.06.13

int adc_id = 0;
int HistoryValue = 0;
char printBuffer[128];

void setup()
{
  Serial.begin(9600);
}

void loop()
{
    int value = analogRead(adc_id); // get adc value

    if(((HistoryValue>=value) && ((HistoryValue - value) > 10)) || ((HistoryValue<value) && ((value - HistoryValue) > 10)))
    {
      sprintf(printBuffer,"ADC%d level is %d\n",adc_id, value);
      Serial.print(printBuffer);
      HistoryValue = value;
    }
}


Result













반응형


반응형

더 재밌게 놀기 연구소


※ Joystick


Circuit



Source Code


//www.elegoo.com
//2016.06.13

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(9600);
}

void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}


Result






※ MAX7219 LED Dot Matrix Module


Circuit



Add this Library

LedControl.zip



Source Code


//www.elegoo.com
//2016.11.5

//We always have to include the library
#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn
 pin 11 is connected to the CLK
 pin 10 is connected to LOAD
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime1=500;
unsigned long delaytime2=50;
void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);
}

/*
 This method will display the characters for the
 word "Arduino" one after the other on the matrix.
 (you need at least 5x7 leds to see the whole chars)
 */
void writeArduinoOnMatrix() {
  /* here is the data for the characters */
  byte a[5]={B01111110,B10001000,B10001000,B10001000,B01111110};
  byte r[5]={B00010000,B00100000,B00100000,B00010000,B00111110};
  byte d[5]={B11111110,B00010010,B00100010,B00100010,B00011100};
  byte u[5]={B00111110,B00000100,B00000010,B00000010,B00111100};
  byte i[5]={B00000000,B00000010,B10111110,B00100010,B00000000};
  byte n[5]={B00011110,B00100000,B00100000,B00010000,B00111110};
  byte o[5]={B00011100,B00100010,B00100010,B00100010,B00011100};

  /* now display them one by one with a small delay */
  lc.setRow(0,0,a[0]);
  lc.setRow(0,1,a[1]);
  lc.setRow(0,2,a[2]);
  lc.setRow(0,3,a[3]);
  lc.setRow(0,4,a[4]);
  delay(delaytime1);
  lc.setRow(0,0,r[0]);
  lc.setRow(0,1,r[1]);
  lc.setRow(0,2,r[2]);
  lc.setRow(0,3,r[3]);
  lc.setRow(0,4,r[4]);
  delay(delaytime1);
  lc.setRow(0,0,d[0]);
  lc.setRow(0,1,d[1]);
  lc.setRow(0,2,d[2]);
  lc.setRow(0,3,d[3]);
  lc.setRow(0,4,d[4]);
  delay(delaytime1);
  lc.setRow(0,0,u[0]);
  lc.setRow(0,1,u[1]);
  lc.setRow(0,2,u[2]);
  lc.setRow(0,3,u[3]);
  lc.setRow(0,4,u[4]);
  delay(delaytime1);
  lc.setRow(0,0,i[0]);
  lc.setRow(0,1,i[1]);
  lc.setRow(0,2,i[2]);
  lc.setRow(0,3,i[3]);
  lc.setRow(0,4,i[4]);
  delay(delaytime1);
  lc.setRow(0,0,n[0]);
  lc.setRow(0,1,n[1]);
  lc.setRow(0,2,n[2]);
  lc.setRow(0,3,n[3]);
  lc.setRow(0,4,n[4]);
  delay(delaytime1);
  lc.setRow(0,0,o[0]);
  lc.setRow(0,1,o[1]);
  lc.setRow(0,2,o[2]);
  lc.setRow(0,3,o[3]);
  lc.setRow(0,4,o[4]);
  delay(delaytime1);
  lc.setRow(0,0,0);
  lc.setRow(0,1,0);
  lc.setRow(0,2,0);
  lc.setRow(0,3,0);
  lc.setRow(0,4,0);
  delay(delaytime1);
}

/*
  This function lights up a some Leds in a row.
 The pattern will be repeated on every row.
 The pattern will blink along with the row-number.
 row number 4 (index==3) will blink 4 times etc.
 */
void rows() {
  for(int row=0;row<8;row++) {
    delay(delaytime2);
    lc.setRow(0,row,B10100000);
    delay(delaytime2);
    lc.setRow(0,row,(byte)0);
    for(int i=0;i<row;i++) {
      delay(delaytime2);
      lc.setRow(0,row,B10100000);
      delay(delaytime2);
      lc.setRow(0,row,(byte)0);
    }
  }
}

/*
  This function lights up a some Leds in a column.
 The pattern will be repeated on every column.
 The pattern will blink along with the column-number.
 column number 4 (index==3) will blink 4 times etc.
 */
void columns() {
  for(int col=0;col<8;col++) {
    delay(delaytime2);
    lc.setColumn(0,col,B10100000);
    delay(delaytime2);
    lc.setColumn(0,col,(byte)0);
    for(int i=0;i<col;i++) {
      delay(delaytime2);
      lc.setColumn(0,col,B10100000);
      delay(delaytime2);
      lc.setColumn(0,col,(byte)0);
    }
  }
}

/*
 This function will light up every Led on the matrix.
 The led will blink along with the row-number.
 row number 4 (index==3) will blink 4 times etc.
 */
void single() {
  for(int row=0;row<8;row++) {
    for(int col=0;col<8;col++) {
      delay(delaytime2);
      lc.setLed(0,row,col,true);
      delay(delaytime2);
      for(int i=0;i<col;i++) {
        lc.setLed(0,row,col,false);
        delay(delaytime2);
        lc.setLed(0,row,col,true);
        delay(delaytime2);
      }
    }
  }
}

void loop() {
  writeArduinoOnMatrix();
  rows();
  columns();
  single();
}



※ ADXL335 Module


Circuit




Source Code


//www.elegoo.com
//2016.06.13

int x, y, z;
int a1 = A0;
int a2 = A1;
int a3 = A2;
void setup()
{
  pinMode(a1,INPUT);
  pinMode(a2,INPUT);
  pinMode(a3,INPUT);
  Serial.begin(9600);  
}
void loop()
{
  x = analogRead(a1);    
  y = analogRead(a2);    
  z = analogRead(a3);    
  Serial.print("x:  ");
  Serial.print(x, DEC); 
  Serial.print(" ");
 Serial.print("y:  ");
  Serial.print(y, DEC);  
  Serial.print(" ");
 Serial.print("z:  ");
  Serial.println(z, DEC);
  delay(100);           
}


Result




반응형


반응형

더 재밌게 놀기 연구소


※ Ultrasonic Sensor (external library)


Circuit



Add zip library to include it in the source code.

HC-SR04_Library.zip


=> This NewPing library converts ping time to distance and print result.



Source Code


//www.elegoo.com
//2016.06.13

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(500);  // Wait 500ms between pings (about 2 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  Serial.println("cm");
}


==> Result






※ Membrane switch (Keypad)


Circuit




Download and add KeyPad Library


http://playground.arduino.cc/Code/Keypad



Source Code


//www.elegoo.com
//2016.06.13

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(9600);
}
 
void loop(){
  char customKey = customKeypad.getKey();
 
  if (customKey){
    Serial.println(customKey);
  }
}


==> Result : If you press any button then the value of the button will be display








※ Temperature and Humidity Sensor


Circuit


add SimpleDHT library


SimpleDHT.zip



Source Code


//www.elegoo.com
//2016.06.13

#include <SimpleDHT.h>

// for DHT11,
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
 
  // read with raw sample data.
  byte temperature = 0;
  byte humidity = 0;
  byte data[40] = {0};
  if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
    Serial.print("Read DHT11 failed");
    return;
  }
 
  Serial.print("Sample RAW Bits: ");
  for (int i = 0; i < 40; i++) {
    Serial.print((int)data[i]);
    if (i > 0 && ((i + 1) % 4) == 0) {
      Serial.print(' ');
    }
  }
  Serial.println("");
 
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" %");
 
  // DHT11 sampling rate is 1HZ.
  delay(1000);
}


==> Result



반응형

[Arduino] 7 Segment, Servo, LCD and Thermometer

2017. 1. 14. 21:38 | Posted by 솔웅


반응형

더 재밌게 놀기 연구소



※ Four 7 digital segment with 74 HC595


Circuit

Source Code


int latch=9;  //74HC595  pin 12 STCP
int clock=10; //74HC595  pin 11 SHCP
int data=8;   //74HC595  pin 14 DS

unsigned char table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c
,0x39,0x5e,0x79,0x71,0x00};

void setup() {
  pinMode(latch,OUTPUT);
  pinMode(clock,OUTPUT);
  pinMode(data,OUTPUT);
}
void Display(unsigned char num)
{

  digitalWrite(latch,LOW);
  shiftOut(data,clock,MSBFIRST,table[num]);
  digitalWrite(latch,HIGH);
 
}
void loop() {
  Display(1);
  delay(500);
  Display(2);
  delay(500);
  Display(3);
  delay(500);
  Display(4);
  delay(500);
  Display(5);
  delay(500);
  Display(6);
  delay(500);
  Display(7);
  delay(500);
  Display(8);
  delay(500);
  Display(9);
  delay(500);
  Display(10);
  delay(500);
  Display(11);
  delay(500);
  Display(12);
  delay(500);
  Display(13);
  delay(500);
  Display(14);
  delay(500);
  Display(15);
  delay(500);
}



※ Servo Motor


Circuit




Source Code


//www.elegoo.com
//2016.06.13
/************************************************/
#include <Servo.h>
/************************************************/
Servo myservo;//create servo object to control a servo
/************************************************/
void setup()
{
  myservo.attach(9);//attachs the servo on pin 9 to servo object
  myservo.write(0);//back to 0 degrees
  delay(1000);//wait for a second
}
/*************************************************/
void loop()

  myservo.write(15);//goes to 15 degrees
  delay(1000);//wait for a second
  myservo.write(30);//goes to 30 degrees
  delay(1000);//wait for a second.33
  myservo.write(45);//goes to 45 degrees
  delay(1000);//wait for a second.33
  myservo.write(60);//goes to 60 degrees
  delay(1000);//wait for a second.33
  myservo.write(75);//goes to 75 degrees
  delay(1000);//wait for a second.33
  myservo.write(90);//goes to 90 degrees
  delay(1000);//wait for a second
  myservo.write(75);//back to 75 degrees
  delay(1000);//wait for a second.33
  myservo.write(60);//back to 60 degrees
  delay(1000);//wait for a second.33
  myservo.write(45);//back to 45 degrees
  delay(1000);//wait for a second.33
  myservo.write(30);//back to 30 degrees
  delay(1000);//wait for a second.33
  myservo.write(15);//back to 15 degrees
  delay(1000);//wait for a second
  myservo.write(0);//back to 0 degrees
  delay(1000);//wait for a second
}
/**************************************************/





※ LCD


Circuit




Source Code


//www.elegoo.com
//2016.06.13

/*
  LiquidCrystal Library - Hello World

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.

 This sketch prints "Hello World!" to the LCD
 and shows the time.

  The circuit:
 * LCD RS pin to digital pin 7
 * LCD Enable pin to digital pin 8
 * LCD D4 pin to digital pin 9
 * LCD D5 pin to digital pin 10
 * LCD D6 pin to digital pin 11
 * LCD D7 pin to digital pin 12
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}





※ LCD with Thermometer


Circuit



Source Code


//www.elegoo.com
//2016.06.13

/*
Adafruit Arduino - Lesson 12. Light and Temperature
*/

#include <LiquidCrystal.h>

int tempPin = 0;
int lightPin = 1;

//                BS  E  D4 D5  D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup()
{
  lcd.begin(16, 2);
}

void loop()
{
  // Display Temperature in C
  int tempReading = analogRead(tempPin);
  float tempVolts = tempReading * 5.0 / 1024.0;
  float tempC = (tempVolts - 0.5) * 100.0;
  float tempF = tempC * 9.0 / 5.0 + 32.0;
  //         ----------------
  lcd.print("Temp         F  ");
  lcd.setCursor(6, 0);
  lcd.print(tempF);
 
  // Display Light on second row
  int lightReading = analogRead(lightPin);
  lcd.setCursor(0, 1);
  //         ----------------
  lcd.print("Light           "); 
  lcd.setCursor(6, 1);
  lcd.print(lightReading);
  delay(500);
}


반응형
이전 1 2 다음