1.三灯 Blink
const int p1 = 1;
const int p2 = 4;
const int p3 = 12;
void setup() {
pinMode(p1, OUTPUT);//定义输出端口
pinMode(p2, OUTPUT);
pinMode(p3, OUTPUT);
}
void loop() {
digitalWrite(p1, HIGH); //定义电流输出
digitalWrite(p2, LOW);
digitalWrite(p3, LOW);
delay(500);
digitalWrite(p2, HIGH);
digitalWrite(p1, LOW);
digitalWrite(p3, LOW);
delay(500);
digitalWrite(p3, HIGH);
digitalWrite(p1, LOW);
digitalWrite(p2, LOW);
delay(500);
}
2.蜂鸣器 Blink
void setup() {
pinMode(12, OUTPUT);
}
void loop(){
unsigned char i,j;
while(1)
{
for(i=0;i<80;i++)//输出一个频率的声音
{
digitalWrite(12,HIGH);//发声音
delay(1000);//延时1ms
digitalWrite(12,LOW);//不发声音
delay(1000);//延时ms
}
}
}
3.程序交互 Digital->Button
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("HI!");
}
4.灯亮 输出到虚列埠 Digital Button
int mouth = 12; // variable for reading the pushbutton status
double time = 0.0;
void setup() {
// initialize the LED pin as an output:
Serial.begin(9600);
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(12,HIGH);
Serial.print("灯已经亮了");
Serial.print(time);
Serial.println("s");
time=time+0.5;
delay(500);
digitalWrite(12,LOW);
Serial.print("灯已经亮了");
Serial.print(time);
Serial.println("s");
time=time+0.5;
delay(500);
}
5.开关控制灯亮 频比信号 主界面
int newdata = 0;
int sensor = A0;
int sensorRead = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorRead = analogRead(sensor);
newdata = map(sensorRead,0,1024,0,255);
Serial.println(newdata);
analogWrite(12,newdata);
delay(200);
}
6.开关控制灯亮灯灭 开发板 NodeMCU 1.0 样例:ESP8266WIFI WIFIWebServer
/*
* 本实作将教你如何将板子设定成http的伺服器
* 本伺服器会依照接收到的讯息去改变*脚位
* http://server_ip/gpio/0会将脚位变成低电压
* http://server_ip/gpio/1会将脚位变成高电压
* 本伺服器的IP会在虚列埠中展示
*/
#include
const char* ssid = "Tony";//WIFI名称
const char* password = "19980408";//WIFI密码
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);//开启板子的port80
void setup() {
Serial.begin(115200);//开启电脑虚列埠 速度设置为115200
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT);//2号脚位设定为输出 D9脚位
digitalWrite(2, 0);//将脚位设置成为低电压
// Connect to WiFi network
//截下来板子开始连接到你家基地台
Serial.println();
Serial.println();
Serial.print("Connecting to ");//印出连线到你家
Serial.println(ssid);
WiFi.begin(ssid, password);//使用你家wifi与密码开始连线
while (WiFi.status() != WL_CONNECTED) {//这是无穷循环,还没成功连线成功会一直跳出点点点
delay(500);
Serial.print(".");
}
Serial.println("");//连线成功后在这里跳出连线成功
Serial.println("WiFi connected");
// Start the server
server.begin();//伺服器启动
Serial.println("Server started");//列印出此伺服器被分配到的IP
// Print the IP address
Serial.println(WiFi.localIP());//列印出此伺服器被分配到的IP
}
void loop() {
// Check if a client has connected
//确定是否连线中,不是的话跳出去
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
//等待传到此伺服器的讯息,一直等一直等
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
//收到传过来的讯息之后,将收到的讯息存成req
String req = client.readStringUntil('\r');
Serial.println(req);//印出收到的讯息
client.flush();//看不懂?高手请教
// Match the request
//开始比对收到的讯息来决定做什么事
int val;
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(2, val);//依照收到的讯息改变灯的亮暗
client.flush();
// Prepare the response
//准备给发讯者的回应,内容以html的格式,代表发讯者会收到的简单网页
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now ";
s += (val)?"high":"low";
s += "\n";
// Send the response to the client
//准备好了,发送给发讯者!!
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
7.温湿度传感器
#include
DHT11 dht11(8); // creating the object sensor on pin 'D7'
int C; // temperature C readings are integers
float F; // temperature F readings are returned in float format
int H; // humidity readings are integers
void setup()
{
// initialize serial communications at 9600 bps
Serial.begin(9600);
}
void loop()
{
dht11.update();
C = dht11.readCelsius(); // Reading the temperature in Celsius degrees and store in the C variable
F = dht11.readFahrenheit(); // Reading the temperature in Fahrenheit degrees and store in the F variable
H = dht11.readHumidity(); // Reading the humidity index
// Print the collected data in a row on the Serial Monitor
Serial.print("H: ");
Serial.print(H);
Serial.print("\tC: ");
Serial.print(C);
Serial.print("\tF: ");
Serial.println(F);
delay(1000); // Wait one second before get another temperature reading
}
8.LCD1602
#include
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("Your ID:");
lcd.setCursor(0,1);
lcd.print("2016203515");
}
void loop() {
// Turn off the display:
// Turn on the display:
lcd.display();
}
9.MFRC522读卡
#include
#include
// #define SS_PIN 4
// #define RST_PIN 9
MFRC522 mfrc522(D4, RST_PIN); // 创建MFRC实例
void setup()
{
pinMode(D10,OUTPUT);
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("系统开启,请将你的卡片放置感应区...");
}
void loop()
{
// 寻找新卡
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//显示卡号 b6a5eebc 182165238188
//Serial.print("UID tag :");
String content= "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
//Serial.println(mfrc522.uid.uidByte[i]);
//Serial.print(mfrc522.uid.uidByte[i], HEX);
content+=mfrc522.uid.uidByte[i];
}
Serial.println(content);
if(content!="")
{
digitalWrite(D10,HIGH);
delay(300);
digitalWrite(D10,LOW);
}
delay(2000);
}
10.连WIFI
#include //引入ESP8266Wifi库
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.begin("Tenda_261AD0", "ghq13336261416"); //就只需要这一句就可以连接到一个网络了
//括号内参数分别为网络名称(SSID)和网络密码
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) //等待网络连接成功
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP()); //通过串口打印输出模块在网络中的IP地址
}
void loop() {}