超声波测距传感器
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
LiquidCrystal_I2C lcd(0x27,16,2);//0x27 0x3F
#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // 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(115200); // Open serial monitor at 115200 baud to see ping results.
lcd.init();
lcd.backlight();
}
void loop() {
delay(100); // Wait 50ms between pings (about 20 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 in cm and print result (0 = outside set distance range)
Serial.println("cm");
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(9, 1);
lcd.print(uS / US_ROUNDTRIP_CM);
lcd.setCursor(12, 1);
lcd.print("cm");
}
温度传感器
/****************************************************
name:Digital Temperature Sensor-ds18b20
function:you can see the value of current temperature displayed on the LCD.
****************************************************/
/****************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 and 0x3F for a 16 chars and 2 line display
#define ONE_WIRE_BUS 7 //ds18b20 module attach to pin7
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(9600);
sensors.begin(); // initialize the bus
lcd.init(); //initialize the lcd
lcd.backlight(); //turn on the backlight
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
lcd.setCursor(0, 0);
lcd.print("TemC: "); //print "Tem: " on lcd1602
lcd.print(sensors.getTempCByIndex(0));//print the temperature on lcd1602
//Serial.print("Tem: ");
//Serial.print(sensors.getTempCByIndex(0));
//Serial.println(" C");
lcd.print(char(223));//print the unit" ℃ "
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("TemF: ");
lcd.print(1.8*sensors.getTempCByIndex(0) + 32.0);//print the temperature on lcd1602
lcd.print(char(223));//print the unit" ℉ "
lcd.print(" F");
//Serial.print("Tem: ");
//Serial.print(1.8*sensors.getTempCByIndex(0) + 32.0);
//Serial.println(" F");
//Serial.println("");
//Serial.print("Temperature for the device 1 (index 0) is: ");
//Serial.println(sensors.getTempCByIndex(0)); //print the temperature on serial monitor
}
直流电机风扇
//Small Fan
/****************************************************************
* As explained above, the amount of times you press the button should change the rotation speed of the fan.
* Pressing it once will cause it to rotate slowly,
* while pressing it three times will cause it to rotate quickly, and pressing it four times will cause it to stop.
****************************************************************/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int motorIn1 = 9;
const int motorIn2 = 10;
int stat = 0;
#define rank1 150
#define rank2 200
#define rank3 250
// Variables will change:
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
/******************************************************************************/
void setup()
{
//set theled,motors as OUTPUT,button as INPUT
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(motorIn1,OUTPUT);
pinMode(motorIn2,OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
if (reading != lastButtonState)// If the button state is different from last time
{
lastDebounceTime = millis();// reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != buttonState)
{
buttonState = reading; // Store the state of button in buttonState
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH)
{
digitalWrite(ledPin, HIGH); //turn on the LED
stat = stat + 1;
if(stat >= 4)// When stat>=4, set it as 0.
{
stat = 0;
}
}
else
digitalWrite(ledPin, LOW);
}
}
switch(stat)
{
case 1:
clockwise(rank1);// When stat=1, set the rotate speed of the motor as rank1=150
break;
case 2:
clockwise(rank2);// When stat=2, set the rotate speed of the motor as rank1=200
break;
case 3:
clockwise(rank3);// When stat=3, set the rotate speed of the motor as rank1=250
break;
default:
clockwise(0);// else, set the rotate speed of the motor as rank1=150
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
/***********************************************************/
void clockwise(int Speed)//
{
analogWrite(motorIn1,0);
analogWrite(motorIn2,Speed);
}
/***********************************************************/
DS1302时钟模块
/*****************************************************
* 湖南创乐博智能科技有限公司
* name:Real-time Clock Module
* function:you can see the current date and time displayed on the I2C LCD1602.
******************************************************/
//include the libraries
#include <stdio.h>
#include <string.h>
#include <DS1302.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 and0x3F for a 16 chars and 2 line display
uint8_t RST_PIN = 5; //RST pin attach to
uint8_t SDA_PIN = 6; //IO pin attach to
uint8_t SCL_PIN = 7; //clk Pin attach to
/* Create buffers */
char buf[50];
char day[10];
String comdata = "";
int numdata[7] ={ 0}, j = 0, mark = 0;
/* Create a DS1302 object */
DS1302 rtc(RST_PIN, SDA_PIN, SCL_PIN);//create a variable type of DS1302
void print_time()
{
/* Get the current time and date from the chip */
Time t = rtc.time();
/* Name the day of the week */
memset(day, 0, sizeof(day));
switch (t.day)
{
case 1:
strcpy(day, "Sun");
break;
case 2:
strcpy(day, "Mon");
break;
case 3:
strcpy(day, "Tue");
break;
case 4:
strcpy(day, "Wed");
break;
case 5:
strcpy(day, "Thu");
break;
case 6:
strcpy(day, "Fri");
break;
case 7:
strcpy(day, "Sat");
break;
}
/* Format the time and date and insert into the temporary buffer */
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d", day, t.yr, t.mon, t.date, t.hr, t.min, t.sec);
/* Print the formatted string to serial so we can see the time */
Serial.println(buf);
lcd.setCursor(2,0);
lcd.print(t.yr);
lcd.print("-");
lcd.print(t.mon/10);
lcd.print(t.mon%10);
lcd.print("-");
lcd.print(t.date/10);
lcd.print(t.date%10);
lcd.print(" ");
lcd.print(day);
lcd.setCursor(4,1);
lcd.print(t.hr);
lcd.print(":");
lcd.print(t.min/10);
lcd.print(t.min%10);
lcd.print(":");
lcd.print(t.sec/10);
lcd.print(t.sec%10);
}
void setup()
{
Serial.begin(9600);
rtc.write_protect(false);
rtc.halt(false);
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
Time t(2018, 6, 23, 18, 41, 50, 7);//initialize the time
/* Set the time and date on the chip */
rtc.time(t);
}
void loop()
{
/*add the data to comdata when the serial has data */
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
mark = 1;
}
/* Use a comma to separate the strings of comdata,
and then convert the results into numbers to be saved in the array numdata[] */
if(mark == 1)
{
Serial.print("You inputed : ");
Serial.println(comdata);
for(int i = 0; i < comdata.length() ; i++)
{
if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
{
j++;
}
else
{
numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
}
}
/* The converted numdata add up to the time format, then write to DS1302*/
Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
rtc.time(t);
mark = 0;
j=0;
/* clear comdata ,in order to wait for the next input */
comdata = String("");
/* clear numdata */
for(int i = 0; i < 7 ; i++) numdata[i]=0;
}
/* print the current time */
print_time();
delay(1000);
}