Last active
March 21, 2017 12:39
-
-
Save NetBUG/8996f2c7d2297dc2cc8c1e736b109411 to your computer and use it in GitHub Desktop.
Подключение сенсоров к Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Устанавливаем библиотеку: https://github.com/amperka/dht | |
// подключаем библиотеку | |
#include <dht.h> | |
// создаём объект-сенсор | |
DHT sensor = DHT(); | |
void setup() | |
{ | |
Serial.begin(9600); | |
// методом attach объявляем к какому контакту подключен | |
// сенсор. В нашем примере это нулевой аналоговый контакт | |
sensor.attach(A0); | |
// | |
// после подачи питания ждём секунду до готовности сенсора к работе | |
delay(1000); | |
} | |
void loop() | |
{ | |
// метод update заставляет сенсор выдать текущие измерения | |
sensor.update(); | |
switch (sensor.getLastError()) | |
{ | |
case DHT_ERROR_OK: | |
char msg[128]; | |
// данные последнего измерения можно считать соответствующими | |
// методами | |
sprintf(msg, "Temperature = %dC, Humidity = %d%%", | |
sensor.getTemperatureInt(), sensor.getHumidityInt()); | |
Serial.println(msg); | |
break; | |
case DHT_ERROR_START_FAILED_1: | |
Serial.println("Error: start failed (stage 1)"); | |
break; | |
case DHT_ERROR_START_FAILED_2: | |
Serial.println("Error: start failed (stage 2)"); | |
break; | |
case DHT_ERROR_READ_TIMEOUT: | |
Serial.println("Error: read timeout"); | |
break; | |
case DHT_ERROR_CHECKSUM_FAILURE: | |
Serial.println("Error: checksum error"); | |
break; | |
} | |
delay(2000); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* \par Copyright (C), 2012-2016, MakeBlock | |
* @file NumberDisplay.ino | |
* @author MakeBlock | |
* @version V1.0.1 | |
* @date 2015/09/10 | |
* @brief Description: this file is sample code for Seven-Segment Display device. | |
* It displays number from 0 to 100. | |
* Function List: | |
* 1. void Me7SegmentDisplay::display(int16_t value) | |
*/ | |
// Подключим дисплей от Makeblocks к Genuino | |
// http://learn.makeblock.com/wp-content/uploads/2016/01/start_0s.jpg - подключите дисплей к выводам SCL и SDA | |
// На Arduino Uno это выводы A4 (SDA) и A5 (SCL) - https://www.arduino.cc/en/reference/wire | |
// | |
// Есть пример на смену отображаемого значения по таймеру (раз в секунду) и изменения отдельного символа из четырёх | |
// https://github.com/Makeblock-official/Makeblock-Libraries/blob/master/makeblock/examples/Me_7SegmentDisplay/TimeDisplay/TimeDisplay.ino | |
#include "MeOrion.h" | |
Me7SegmentDisplay disp(PORT_6); | |
int i = 0; | |
void setup() | |
{ | |
} | |
void loop() | |
{ | |
if(i > 100) | |
{ | |
i = 0; | |
} | |
disp.display(i++); | |
delay(100); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Sweep | |
by BARRAGAN <http://barraganstudio.com> | |
by Scott Fitzgerald | |
http://www.arduino.cc/en/Tutorial/Sweep | |
Пример кода, берущего значение с аналогового вывода и преобразующего его в угол поворота сервомотора (сервомашинки) | |
*/ | |
#include <Servo.h> | |
Servo myservo; // Создаём объект Servo, который будет олицетворять сервомотор в коде | |
// Обычно можно создать до 12 таких объектов | |
int pos = 0; // Здесь мы храним положение, в которое ставим серву | |
void setup() { | |
myservo.attach(9); // Задаём, что серву мы подключили к выводу 9 | |
} | |
void loop() { | |
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees | |
// in steps of 1 degree | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(15); // waits 15ms for the servo to reach the position | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment