0% found this document useful (0 votes)
34 views8 pages

공설입 코딩 예제

The document contains code for several Arduino projects including ultrasonic distance measurement, Neopixel LED control, real-time clock, and mood lamp button control. The ultrasonic code uses trig and echo pins to measure distance. The Neopixel code cycles through different colors on the LEDs. The real-time clock code reads time from a DS1302 RTC module. The mood lamp code toggles an LED on or off using a button press.

Uploaded by

조연욱
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views8 pages

공설입 코딩 예제

The document contains code for several Arduino projects including ultrasonic distance measurement, Neopixel LED control, real-time clock, and mood lamp button control. The ultrasonic code uses trig and echo pins to measure distance. The Neopixel code cycles through different colors on the LEDs. The real-time clock code reads time from a DS1302 RTC module. The mood lamp code toggles an LED on or off using a button press.

Uploaded by

조연욱
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

초음파

int trigPin = 11;

int echoPin = 12;

void setup() {

Serial.begin(9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

void loop() {

float duration, distance;

digitalWrite(trigpin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// echoPin 이 HIGH 를 유지한 시간을 저장 한다.

duration = pulseIn(echoPin, HIGH);

// HIGH 였을 때 시간(초음파가 보냈다가 다시 들어온 시간)을 가지고 거리를 계산 한다.

distance = ((float)(340 * duration) / 10000) / 2;

Serial.print(distance);

Serial.println("cm");

// 수정한 값을 출력

delay(500);

}
네오픽셀

#include <Adafruit_NeoPixel.h>

#define NEO_PIN 2 // 네오픽셀 DI 핀과 연결된 핀번호 설정

#define NUM_LEDS 8 // 네오픽셀 LED 갯수 설정

// 네오픽셀 라이브러리 funnyNeo 이름으로 연결

Adafruit_NeoPixel funnyNeo = Adafruit_NeoPixel(NUM_LEDS, NEO_PIN, NEO_GRBW +


NEO_KHZ800);

int ledBright = 255; // 초기 밝기 255(최대) 설정

// 아두이노 초기화

void setup()

funnyNeo.setBrightness(ledBright); // 밝기 설정 ( 0 ~ 255 )

funnyNeo.begin(); // 네오픽셀 시작

funnyNeo.show(); // 네오픽셀 초기화

void loop()

int i;

uint32_t colorValue; // 색상값 저장 변수

//빨간색 1 바퀴 회전

colorValue = funnyNeo.Color(255, 0, 0, 0); // Red

for(i = 0; i < NUM_LEDS; i++)

funnyNeo.setPixelColor(i, colorValue);

funnyNeo.show();

delay(50);
}

// 녹색 1 바퀴 회전

colorValue = funnyNeo.Color(0, 255, 0, 0); // Green

for(i = 0; i < NUM_LEDS; i++)

funnyNeo.setPixelColor(i, colorValue);

funnyNeo.show();

delay(50);

// 파랑색 1 바퀴 회전

colorValue = funnyNeo.Color(0, 0, 255, 0); // Blue

for(i = 0; i < NUM_LEDS; i++)

funnyNeo.setPixelColor(i, colorValue);

funnyNeo.show();

delay(50);

// 하얀색 1 바퀴 회전

colorValue = funnyNeo.Color(0, 0, 0, 255); // White

for(i = 0; i < NUM_LEDS; i++)

funnyNeo.setPixelColor(i, colorValue);

funnyNeo.show();

delay(50);

리얼타임

// CONNECTIONS:

// DS1302 CLK/SCLK --> 5


// DS1302 DAT/IO --> 4

// DS1302 RST/CE --> 2

// DS1302 VCC --> 3.3v - 5v

// DS1302 GND --> GND

#include <ThreeWire.h>

#include <RtcDS1302.h>

ThreeWire myWire(4,5,2); // IO, SCLK, CE

RtcDS1302<ThreeWire> Rtc(myWire);

void setup ()

Serial.begin(57600);

Serial.print("compiled: ");

Serial.print(__DATE__);

Serial.println(__TIME__);

Rtc.Begin();

RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);

printDateTime(compiled);

Serial.println();

if (!Rtc.IsDateTimeValid())

// Common Causes:

// 1) first time you ran and the device wasn't running yet

// 2) the battery on the device is low or even missing


Serial.println("RTC lost confidence in the DateTime!");

Rtc.SetDateTime(compiled);

if (Rtc.GetIsWriteProtected())

Serial.println("RTC was write protected, enabling writing now");

Rtc.SetIsWriteProtected(false);

if (!Rtc.GetIsRunning())

Serial.println("RTC was not actively running, starting now");

Rtc.SetIsRunning(true);

RtcDateTime now = Rtc.GetDateTime();

if (now < compiled)

Serial.println("RTC is older than compile time! (Updating DateTime)");

Rtc.SetDateTime(compiled);

else if (now > compiled)

Serial.println("RTC is newer than compile time. (this is expected)");

else if (now == compiled)

Serial.println("RTC is the same as compile time! (not expected but all is fine)");

}
void loop ()

RtcDateTime now = Rtc.GetDateTime();

printDateTime(now);

Serial.println();

if (!now.IsValid())

// Common Causes:

// 1) the battery on the device is low or even missing and the power line was
disconnected

Serial.println("RTC lost confidence in the DateTime!");

delay(10000); // ten seconds

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)

char datestring[20];

snprintf_P(datestring,

countof(datestring),

PSTR("%02u/%02u/%04u %02u:%02u:%02u"),

dt.Month(),

dt.Day(),

dt.Year(),
dt.Hour(),

dt.Minute(),

dt.Second() );

Serial.print(datestring);

무드등 조작 버튼

const int buttonPin = 2;

const int ledPin = 13;

int buttonState = LOW;

int ledState = 0;

int prevButtonState = LOW;

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

void loop(){

buttonState = digitalRead(buttonPin);

if (prevButtonState == LOW && buttonState == HIGH)

if(ledState == 0)

digitalWrite(ledPin, HIGH);

ledState=1;

else

digitalWrite(ledPin, LOW);

ledState=0;

}
else

prevButtonState = buttonState;

You might also like