85875| 199
|
[教程] 一块扩展板完成Arduino的10类37项实验(代码+图形+仿真) |
本帖最后由 驴友花雕 于 2019-8-17 06:12 编辑 采取"Arduino程序代码"+"Mind+图形编程"+"Linkboy仿真编程'三者相结合的方式 互为补充,打好基础,促进理解 实验目录(Arduino动手做) 一、LED 实验 01 Blink:D13蓝色 LED 闪烁 02 Blink2:D12、D13红蓝色 LED 交替闪烁 03 Blink3:模拟爆闪警灯 04 Breath_LED:D9 全彩 LED 模拟红色呼吸灯 二、按键实验 05 Button:D2 按键控制 D13蓝色 LED 06 Button_Lock:D3 按键控制 D13 蓝色LED 自锁实验 三、电位器实验 07 RS232_AD:串口读取 A0 电位器采集数值(旋转角度270°输出0-3.3V/5V电压信号电位器阻值10K) 08 Rotation_LED:A0 电位器控制 D11 全彩 LED 蓝色端亮度 09 RC_Motor:A0 电位器控制 D7 输出口的舵机控制其转动角度(舵机需自备Arduino 需要外部供电) 四、RGB 全彩 LED 实验 10 LED_RGB_Text:全彩 LED 基本颜色变化 11 LED_RGB:全彩 LED 彩虹变化 五、红外传感器D6实验(红外遥控器需自备) 12 IRrelay:按红外遥控器任意键,可以控制 D13 LED 开关(控制距离1-8米,频率38KHz,兼容市面上大部分红外遥控器) 13 IRrecord:串口显示读取的红外遥控代码 六、温度传感器实验 14 LM35_RS232AD:串口显示 A2 口 LM35 温度传感器读取的温度(可测试室内外温度,范围-50-150°C,灵敏度好) 七、温湿度传感器实验 15 DHT11_Test:串口显示 D4 口 DHT11 温湿度传感器读取的温湿度数据(测温范围0-50°C,湿度范围20%-90%PH) 八、亮度传感器实验 16 Light Sensor:串口显示 A1 口光敏电阻采集的数值 17 Light_LED:A1 口光敏电阻控制 D13 LED 开关(感光比较灵敏,适合教学实验及民用设备) 九、蜂鸣器实验 18 Buzzer:D5 口无源蜂鸣器模拟救护车警笛(可发出简单的音乐声,音乐需要通过程序编写) 十、扩展实验 19 模拟超声波测距传感器(探测类I,IIC/I2C接口) 20 总线DS18B20温度传感器(数值类,接扩展板数字D7接口) 21 TM1637四位数码管(数码管和点阵类,接扩展板数字D7/D8接口) 22 GY-BMP280-3.3 大气压强高度计传感器模块 ( 数值类,IIC/I2C接口) 23 GY-NEO-6MV2新款飞控GPS卫星信号接收模块 (数值类,TTL接口) 24 5V低电平触发单路继电器模块(执行类,数字D7接口) 25 带驻极话筒4线制声音传感器模块(触发类,数字D7接口) 26 BH1750FVI 数字光强度模块光照传感器(数值类, IIC/I2C接口) 27 开源DFPlayer Mini TF卡MP3播放模块(输出执行器类D7/D8数字接口) 28 LCD1602液晶屏模块(输出显示器类, IIC/I2C接口) 29 人体红外热释电运动传感器模块 (触发类,数字D7接口) 30 DS1307时钟模块 Tiny RTC I2C模块(探测传感器类, IIC/I2C接口) 31 兼容HC-06从机蓝牙模块(通信与存储类,TTL接口) (待续) |
[mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 板载端口扩展实验 32 0.96寸OLED12864液晶屏模块(显示器类,IIC接口) 实验接线:A4---SDA, A5---SCL 项目四:动画示例中的雪花飘 */ #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET 4 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define NUMFLAKES 10 #define LOGO_HEIGHT 16 #define LOGO_WIDTH 16 static const unsigned char PROGMEM logo_bmp[] = { B00000000, B11000000, B00000001, B11000000, B00000001, B11000000, B00000011, B11100000, B11110011, B11100000, B11111110, B11111000, B01111110, B11111111, B00110011, B10011111, B00011111, B11111100, B00001101, B01110000, B00011011, B10100000, B00111111, B11100000, B00111111, B11110000, B01111100, B11110000, B01110000, B01110000, B00000000, B00110000 }; void setup() { Serial.begin(9600); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } display.display(); delay(2000); display.clearDisplay(); display.drawPixel(10, 10, WHITE); display.display(); delay(2000); testdrawline(); testdrawrect(); testfillrect(); testdrawcircle(); testfillcircle(); testdrawroundrect(); testfillroundrect(); testdrawtriangle(); testfilltriangle(); testdrawchar(); testdrawstyles(); testscrolltext(); testdrawbitmap(); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); } void loop() { } void testdrawline() { int16_t i; display.clearDisplay(); for(i=0; i<display.width(); i+=4) { display.drawLine(0, 0, i, display.height()-1, WHITE); display.display(); delay(1); } for(i=0; i<display.height(); i+=4) { display.drawLine(0, 0, display.width()-1, i, WHITE); display.display(); delay(1); } delay(250); display.clearDisplay(); for(i=0; i<display.width(); i+=4) { display.drawLine(0, display.height()-1, i, 0, WHITE); display.display(); delay(1); } for(i=display.height()-1; i>=0; i-=4) { display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); display.display(); delay(1); } delay(250); display.clearDisplay(); for(i=display.width()-1; i>=0; i-=4) { display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); display.display(); delay(1); } for(i=display.height()-1; i>=0; i-=4) { display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); display.display(); delay(1); } delay(250); display.clearDisplay(); for(i=0; i<display.height(); i+=4) { display.drawLine(display.width()-1, 0, 0, i, WHITE); display.display(); delay(1); } for(i=0; i<display.width(); i+=4) { display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE); display.display(); delay(1); } delay(2000); } void testdrawrect(void) { display.clearDisplay(); for(int16_t i=0; i<display.height()/2; i+=2) { display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE); display.display(); delay(1); } delay(2000); } void testfillrect(void) { display.clearDisplay(); for(int16_t i=0; i<display.height()/2; i+=3) { // The INVERSE color is used so rectangles alternate white/black display.fillRect(i, i, display.width()-i*2, display.height()-i*2, INVERSE); display.display(); delay(1); } delay(2000); } void testdrawcircle(void) { display.clearDisplay(); for(int16_t i=0; i<max(display.width(),display.height())/2; i+=2) { display.drawCircle(display.width()/2, display.height()/2, i, WHITE); display.display(); delay(1); } delay(2000); } void testfillcircle(void) { display.clearDisplay(); for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) { display.fillCircle(display.width() / 2, display.height() / 2, i, INVERSE); display.display(); delay(1); } delay(2000); } void testdrawroundrect(void) { display.clearDisplay(); for(int16_t i=0; i<display.height()/2-2; i+=2) { display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE); display.display(); delay(1); } delay(2000); } void testfillroundrect(void) { display.clearDisplay(); for(int16_t i=0; i<display.height()/2-2; i+=2) { display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, INVERSE); display.display(); delay(1); } delay(2000); } void testdrawtriangle(void) { display.clearDisplay(); for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) { display.drawTriangle( display.width()/2 , display.height()/2-i, display.width()/2-i, display.height()/2+i, display.width()/2+i, display.height()/2+i, WHITE); display.display(); delay(1); } delay(2000); } void testfilltriangle(void) { display.clearDisplay(); for(int16_t i=max(display.width(),display.height())/2; i>0; i-=5) { display.fillTriangle( display.width()/2 , display.height()/2-i, display.width()/2-i, display.height()/2+i, display.width()/2+i, display.height()/2+i, INVERSE); display.display(); delay(1); } delay(2000); } void testdrawchar(void) { display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); display.cp437(true); for(int16_t i=0; i<256; i++) { if(i == '\n') display.write(' '); else display.write(i); } display.display(); delay(2000); } void testdrawstyles(void) { display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.println(F("Hello, world!")); display.setTextColor(BLACK, WHITE); display.println(3.141592); display.setTextSize(2); display.setTextColor(WHITE); display.print(F("0x")); display.println(0xDEADBEEF, HEX); display.display(); delay(2000); } void testscrolltext(void) { display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(10, 0); display.println(F("scroll")); display.display(); delay(100); display.startscrollright(0x00, 0x0F); delay(2000); display.stopscroll(); delay(1000); display.startscrollleft(0x00, 0x0F); delay(2000); display.stopscroll(); delay(1000); display.startscrolldiagright(0x00, 0x07); delay(2000); display.startscrolldiagleft(0x00, 0x07); delay(2000); display.stopscroll(); delay(1000); } void testdrawbitmap(void) { display.clearDisplay(); display.drawBitmap( (display.width() - LOGO_WIDTH ) / 2, (display.height() - LOGO_HEIGHT) / 2, logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1); display.display(); delay(1000); } #define XPOS 0 #define YPOS 1 #define DELTAY 2 void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) { int8_t f, icons[NUMFLAKES][3]; for(f=0; f< NUMFLAKES; f++) { icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); icons[f][YPOS] = -LOGO_HEIGHT; icons[f][DELTAY] = random(1, 6); Serial.print(F("x: ")); Serial.print(icons[f][XPOS], DEC); Serial.print(F(" y: ")); Serial.print(icons[f][YPOS], DEC); Serial.print(F(" dy: ")); Serial.println(icons[f][DELTAY], DEC); } for(;;) { display.clearDisplay(); for(f=0; f< NUMFLAKES; f++) { display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); } display.display(); delay(200); for(f=0; f< NUMFLAKES; f++) { icons[f][YPOS] += icons[f][DELTAY]; if (icons[f][YPOS] >= display.height()) { icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); icons[f][YPOS] = -LOGO_HEIGHT; icons[f][DELTAY] = random(1, 6); } } } }[/mw_shl_code] |
13 IRrecord:串口显示读取的红外遥控代码 [mw_shl_code=c,true]/* Eagler8实验程序列表 13 IRrecord:串口显示读取的红外遥控代码 */ #include <IRremote.h> int RECV_PIN = 8; int BUTTON_PIN = 12; int STATUS_PIN = 13; IRrecv irrecv(RECV_PIN); IRsend irsend; decode_results results; void setup() { Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver pinMode(BUTTON_PIN, INPUT); pinMode(STATUS_PIN, OUTPUT); } // Storage for the recorded code int codeType = -1; // The type of code unsigned long codeValue; // The code value if not raw unsigned int rawCodes[RAWBUF]; // The durations if raw int codeLen; // The length of the code int toggle = 0; // The RC5/6 toggle state // Stores the code for later playback // Most of this code is just logging void storeCode(decode_results *results) { codeType = results->decode_type; int count = results->rawlen; if (codeType == UNKNOWN) { Serial.println("Received unknown code, saving as raw"); codeLen = results->rawlen - 1; // To store raw codes: // Drop first value (gap) // Convert from ticks to microseconds // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion for (int i = 1; i <= codeLen; i++) { if (i % 2) { // Mark rawCodes[i - 1] = results->rawbuf*USECPERTICK - MARK_EXCESS; Serial.print(" m"); } else { // Space rawCodes[i - 1] = results->rawbuf*USECPERTICK + MARK_EXCESS; Serial.print(" s"); } Serial.print(rawCodes[i - 1], DEC); } Serial.println(""); } else { if (codeType == NEC) { Serial.print("Received NEC: "); if (results->value == REPEAT) { // Don't record a NEC repeat value as that's useless. Serial.println("repeat; ignoring."); return; } } else if (codeType == SONY) { Serial.print("Received SONY: "); } else if (codeType == RC5) { Serial.print("Received RC5: "); } else if (codeType == RC6) { Serial.print("Received RC6: "); } else { Serial.print("Unexpected codeType "); Serial.print(codeType, DEC); Serial.println(""); } Serial.println(results->value, HEX); codeValue = results->value; codeLen = results->bits; } } void sendCode(int repeat) { if (codeType == NEC) { if (repeat) { irsend.sendNEC(REPEAT, codeLen); Serial.println("Sent NEC repeat"); } else { irsend.sendNEC(codeValue, codeLen); Serial.print("Sent NEC "); Serial.println(codeValue, HEX); } } else if (codeType == SONY) { irsend.sendSony(codeValue, codeLen); Serial.print("Sent Sony "); Serial.println(codeValue, HEX); } else if (codeType == RC5 || codeType == RC6) { if (!repeat) { // Flip the toggle bit for a new button press toggle = 1 - toggle; } // Put the toggle bit into the code to send codeValue = codeValue & ~(1 << (codeLen - 1)); codeValue = codeValue | (toggle << (codeLen - 1)); if (codeType == RC5) { Serial.print("Sent RC5 "); Serial.println(codeValue, HEX); irsend.sendRC5(codeValue, codeLen); } else { irsend.sendRC6(codeValue, codeLen); Serial.print("Sent RC6 "); Serial.println(codeValue, HEX); } } else if (codeType == UNKNOWN /* i.e. raw */) { // Assume 38 KHz irsend.sendRaw(rawCodes, codeLen, 38); Serial.println("Sent raw"); } } int lastButtonState; void loop() { // If button pressed, send the code. int buttonState = digitalRead(BUTTON_PIN); if (lastButtonState == HIGH && buttonState == LOW) { Serial.println("Released"); irrecv.enableIRIn(); // Re-enable receiver } if (buttonState) { Serial.println("Pressed, sending"); digitalWrite(STATUS_PIN, HIGH); sendCode(lastButtonState == buttonState); digitalWrite(STATUS_PIN, LOW); delay(50); // Wait a bit between retransmissions } else if (irrecv.decode(&results)) { digitalWrite(STATUS_PIN, HIGH); storeCode(&results); irrecv.resume(); // resume receiver digitalWrite(STATUS_PIN, LOW); } lastButtonState = buttonState; }[/mw_shl_code] |
3、Eagler8模块与板载端口: 复位按键(REST) 10K旋转电位器模块(A0) 2路SW按键模块(D2、D3) Light亮度传感器模块(A1) LM35D温度传感器模块(A2) Buzzer无源蜂鸣器模块(D5) DHT11温湿度传感器模块(D4) ER Receiver红外接收模块(D6) 1路Analog Port模拟量端口(A3) RGB全彩LED模块(D9、D10、D11) 2路Digital Port数字量端口(D7、D8) 1个TTL串口(可接入蓝牙或WIFI模块等) 2路3mm LED模块(红色D12、蓝色D13) 1个双向同步串行总线I2C接口(A4 SDA及A5 SCL,可接显示屏等) |
本帖最后由 驴友花雕 于 2019-6-10 09:20 编辑 01 Blink:D13蓝色 LED 闪烁 [mw_shl_code=c,true]/* Eagler8实验程序列表 01 Blink:D13蓝色 LED 闪烁 */ int led = 13; void setup() { pinMode(led, OUTPUT); } void loop() { digitalWrite(led, HIGH); delay(1000); digitalWrite(led, LOW); delay(1000); } [/mw_shl_code] |
本帖最后由 驴友花雕 于 2019-6-10 09:57 编辑 02 Blink2:D12、D13红蓝色 LED 交替闪烁 [mw_shl_code=cpp,true]/* Eagler8实验程序列表 02 Blink2:D12、D13红蓝色 LED 交替闪烁 */ int led1 = 12; int led2 = 13; void setup() { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); } void loop() { digitalWrite(led1, HIGH); delay(1000); digitalWrite(led1, LOW); digitalWrite(led2, HIGH); delay(1000); digitalWrite(led2, LOW); }[/mw_shl_code] |
本帖最后由 驴友花雕 于 2019-6-10 10:39 编辑 03 Blink3:模拟爆闪警灯 [mw_shl_code=cpp,true]/* Eagler8实验程序列表 03 Blink3:模拟爆闪警灯 */ int led1 = 12; int led2 = 13; int i,j; void setup() { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); } void loop() { for(i=0;i<10;i++){ digitalWrite(led1, HIGH); delay(30); digitalWrite(led1, LOW); delay(40); } for(i=0;i<10;i++){ digitalWrite(led2, HIGH); delay(30); digitalWrite(led2, LOW); delay(40); } }[/mw_shl_code] |
本帖最后由 驴友花雕 于 2019-6-10 13:06 编辑 04 Breath_LED: D9 全彩 LED 模拟红色呼吸灯 [mw_shl_code=cpp,true]/* Eagler8实验程序列表 04 Breath_LED:D9 全彩 LED 模拟红色呼吸灯 */ #define LED 9 int i = 0; void setup() { pinMode(LED,OUTPUT); } void loop() { for(i=0;i<220;i++) { analogWrite(LED,i); delay(15); } for(i=220;i>0;i--) { analogWrite(LED,i); delay(15); } analogWrite(LED,0); delay(60); }[/mw_shl_code] |
本帖最后由 驴友花雕 于 2019-6-10 15:20 编辑 05 Button:D2 按键控制 D13蓝色 LED [mw_shl_code=cpp,true]/* Eagler8实验程序列表 05 Button:D2 按键控制 D13蓝色 LED */ int ledPin = 13; int inputPin = 2; int val = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(inputPin, INPUT); } void loop() { val = digitalRead(inputPin); if(val == HIGH) { digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, HIGH); } }[/mw_shl_code] |
06 Button_Lock:D3 按键控制 D13 蓝色LED 自锁实验 [mw_shl_code=c,true]/* Eagler8实验程序列表 06 Button_Lock:D3 按键控制 D13 蓝色LED 自锁实验 */ int Button=3; int LED=13; boolean onoff=LOW; void setup(){ pinMode(Button,INPUT); pinMode(LED,OUTPUT); } void loop(){ if(digitalRead(Button)==HIGH) { delay(10); if(digitalRead(Button)==LOW) { digitalWrite(LED,onoff); onoff=(!onoff); delay(10); while(digitalRead(Button)==LOW) { delay(1); } } } }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed