本文是上篇的延续,可以测量从LED亮到按下按钮的具体时长,并显示出来。但在功能增加的同时,占用的晶体管资源增加了多少呢?得到有效利用的又有多少呢?
目录
一、Arduino是什么?
二、部件
1.Arduino uno
简介:
引脚图:
Hello World:
2.SH1106
驱动程序:
二、主程序
总结
一、Arduino是什么?
What is Arduino?
Arduino designs, manufactures, and supports electronic devices and software, allowing people around the world to easily access advanced technologies that interact with the physical world. Our products are straightforward, simple, and powerful, ready to satisfy users’ needs from students to makers and all the way to professional developers.
Find out more/en/Guide/Introduction
Our Mission & Vision
Arduino’s mission is to enable anyone to enhance their lives through accessible electronics and digital technologies. There was once a barrier between the electronics, design, and programming world and the rest of the world. Arduino has broken down that barrier.
Over the years, our products have been the brains behind thousands of projects, from everyday objects to complex scientific instruments. A worldwide community, comprising students, hobbyists, artists, programmers, and professionals, has gathered around this open-source platform, their contributions adding up to an incredible amount of accessible knowledge.
Our vision is to make Arduino available to everyone, whether you are a student, maker or professional, which is why we now have three segments to our business. These segments work together as an ecosystem with a shared mindset: we started with Maker, and that has evolved into Education and PRO solutions.
二、部件
1.Arduino uno
简介:
Overview
Arduino Unois a microcontroller board based on the ATmega328P (datasheet). It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz ceramic resonator (CSTCE16M0V53-R0), a USB connection, a power jack, an ICSP header and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.. You can tinker with your Uno without worrying too much about doing something wrong, worst case scenario you can replace the chip for a few dollars and start over again.
"Uno" means one in Italian and was chosen to mark the release of Arduino Software (IDE) 1.0. The Uno board and version 1.0 of Arduino Software (IDE) were the reference versions of Arduino, now evolved to newer releases. The Uno board is the first in a series of USB Arduino boards, and the reference model for the Arduino platform; for an extensive list of current, past or outdated boards see the Arduino index of boards.
Tech specs
引脚图:
Hello World:
// the setup function runs once when you press reset or power the boardvoid setup() {// initialize digital pin LED_BUILTIN as an output.pinMode(LED_BUILTIN, OUTPUT);}// the loop function runs over and over again forevervoid loop() {digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)delay(1000); // wait for a seconddigitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOWdelay(1000); // wait for a second}
2.SH1106
这里原本是要用mega2560的,但新手入门,不知到屏怎么连,网上也没有现成的解决方案。最后还是买了块uno,解决了问题。所以入门省事还是建议uno。
驱动程序:
#include <SPI.h>#include <Wire.h>#include <Adafruit_GFX.h>#include <Adafruit_SH110X.h>#define OLED_MOSI10#define OLED_CLK8#define OLED_DC 7#define OLED_CS 5#define OLED_RST9// Create the OLED displayAdafruit_SH1106G display = Adafruit_SH1106G(128, 64,OLED_MOSI, OLED_CLK, OLED_DC, OLED_RST, OLED_CS);#define NUMFLAKES 10#define XPOS 0#define YPOS 1#define DELTAY 2#define LOGO16_GLCD_HEIGHT 16#define LOGO16_GLCD_WIDTH 16static const unsigned char PROGMEM logo16_glcd_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);//display.setContrast (0); // dim display// Start OLEDdisplay.begin(0, true); // we dont use the i2c address but we will reset!// Show image buffer on the display hardware.// Since the buffer is intialized with an Adafruit splashscreen// internally, this will display the splashscreen.display.display();delay(2000);// Clear the buffer.display.clearDisplay();// draw a single pixeldisplay.drawPixel(10, 10, SH110X_WHITE);// Show the display buffer on the hardware.// NOTE: You _must_ call display after making any drawing commands// to make them visible on the display hardware!display.display();delay(2000);display.clearDisplay();// draw many linestestdrawline();display.display();delay(2000);display.clearDisplay();// draw rectanglestestdrawrect();display.display();delay(2000);display.clearDisplay();// draw multiple rectanglestestfillrect();display.display();delay(2000);display.clearDisplay();// draw mulitple circlestestdrawcircle();display.display();delay(2000);display.clearDisplay();// draw a SH110X_WHITE circle, 10 pixel radiusdisplay.fillCircle(display.width() / 2, display.height() / 2, 10, SH110X_WHITE);display.display();delay(2000);display.clearDisplay();testdrawroundrect();delay(2000);display.clearDisplay();testfillroundrect();delay(2000);display.clearDisplay();testdrawtriangle();delay(2000);display.clearDisplay();testfilltriangle();delay(2000);display.clearDisplay();// draw the first ~12 characters in the fonttestdrawchar();display.display();delay(2000);display.clearDisplay();// text display testsdisplay.setTextSize(1);display.setTextColor(SH110X_WHITE);display.setCursor(0, 0);display.println("Failure is always an option");display.setTextColor(SH110X_BLACK, SH110X_WHITE); // 'inverted' textdisplay.println(3.141592);display.setTextSize(2);display.setTextColor(SH110X_WHITE);display.print("0x"); display.println(0xDEADBEEF, HEX);display.display();delay(2000);display.clearDisplay();// miniature bitmap displaydisplay.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1);display.display();delay(1);// invert the displaydisplay.invertDisplay(true);delay(1000);display.invertDisplay(false);delay(1000);display.clearDisplay();// draw a bitmap icon and 'animate' movementtestdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);}void loop() {}void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {uint8_t icons[NUMFLAKES][3];// initializefor (uint8_t f = 0; f < NUMFLAKES; f++) {icons[f][XPOS] = random(display.width());icons[f][YPOS] = 0;icons[f][DELTAY] = random(5) + 1;Serial.print("x: ");Serial.print(icons[f][XPOS], DEC);Serial.print(" y: ");Serial.print(icons[f][YPOS], DEC);Serial.print(" dy: ");Serial.println(icons[f][DELTAY], DEC);}while (1) {// draw each iconfor (uint8_t f = 0; f < NUMFLAKES; f++) {display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SH110X_WHITE);}display.display();delay(200);// then erase it + move itfor (uint8_t f = 0; f < NUMFLAKES; f++) {display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SH110X_BLACK);// move iticons[f][YPOS] += icons[f][DELTAY];// if its gone, reinitif (icons[f][YPOS] > display.height()) {icons[f][XPOS] = random(display.width());icons[f][YPOS] = 0;icons[f][DELTAY] = random(5) + 1;}}}}void testdrawchar(void) {display.setTextSize(1);display.setTextColor(SH110X_WHITE);display.setCursor(0, 0);for (uint8_t i = 0; i < 168; i++) {if (i == '\n') continue;display.write(i);if ((i > 0) && (i % 21 == 0))display.println();}display.display();delay(1);}void testdrawcircle(void) {for (int16_t i = 0; i < display.height(); i += 2) {display.drawCircle(display.width() / 2, display.height() / 2, i, SH110X_WHITE);display.display();delay(1);}}void testfillrect(void) {uint8_t color = 1;for (int16_t i = 0; i < display.height() / 2; i += 3) {// alternate colorsdisplay.fillRect(i, i, display.width() - i * 2, display.height() - i * 2, color % 2);display.display();delay(1);color++;}}void testdrawtriangle(void) {for (int16_t i = 0; i < min(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, SH110X_WHITE);display.display();delay(1);}}void testfilltriangle(void) {uint8_t color = SH110X_WHITE;for (int16_t i = min(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, SH110X_WHITE);if (color == SH110X_WHITE) color = SH110X_BLACK;else color = SH110X_WHITE;display.display();delay(1);}}void testdrawroundrect(void) {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, SH110X_WHITE);display.display();delay(1);}}void testfillroundrect(void) {uint8_t color = SH110X_WHITE;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, color);if (color == SH110X_WHITE) color = SH110X_BLACK;else color = SH110X_WHITE;display.display();delay(1);}}void testdrawrect(void) {for (int16_t i = 0; i < display.height() / 2; i += 2) {display.drawRect(i, i, display.width() - 2 * i, display.height() - 2 * i, SH110X_WHITE);display.display();delay(1);}}void testdrawline() {for (int16_t i = 0; i < display.width(); i += 4) {display.drawLine(0, 0, i, display.height() - 1, SH110X_WHITE);display.display();delay(1);}for (int16_t i = 0; i < display.height(); i += 4) {display.drawLine(0, 0, display.width() - 1, i, SH110X_WHITE);display.display();delay(1);}delay(250);display.clearDisplay();for (int16_t i = 0; i < display.width(); i += 4) {display.drawLine(0, display.height() - 1, i, 0, SH110X_WHITE);display.display();delay(1);}for (int16_t i = display.height() - 1; i >= 0; i -= 4) {display.drawLine(0, display.height() - 1, display.width() - 1, i, SH110X_WHITE);display.display();delay(1);}delay(250);display.clearDisplay();for (int16_t i = display.width() - 1; i >= 0; i -= 4) {display.drawLine(display.width() - 1, display.height() - 1, i, 0, SH110X_WHITE);display.display();delay(1);}for (int16_t i = display.height() - 1; i >= 0; i -= 4) {display.drawLine(display.width() - 1, display.height() - 1, 0, i, SH110X_WHITE);display.display();delay(1);}delay(250);display.clearDisplay();for (int16_t i = 0; i < display.height(); i += 4) {display.drawLine(display.width() - 1, 0, 0, i, SH110X_WHITE);display.display();delay(1);}for (int16_t i = 0; i < display.width(); i += 4) {display.drawLine(display.width() - 1, 0, i, display.height() - 1, SH110X_WHITE);display.display();delay(1);}delay(250);}
二、主程序
#include <SPI.h>#include <Wire.h>#include <Adafruit_GFX.h>#include <Adafruit_SH110X.h>#define OLED_MOSI10 sda#define OLED_CLK8 sck#define OLED_DC 7#define OLED_CS 5#define OLED_RST9// Create the OLED displayAdafruit_SH1106G display = Adafruit_SH1106G(128, 64,OLED_MOSI, OLED_CLK, OLED_DC, OLED_RST, OLED_CS);#define NUMFLAKES 10#define XPOS 0#define YPOS 1#define DELTAY 2#define LOGO16_GLCD_HEIGHT 16#define LOGO16_GLCD_WIDTH 16const int buttonpin = 2;const int ledpin = 13;int buttonpintwo;int startsignal;unsigned long timebox;unsigned long rtime1;unsigned long rtime2;static const unsigned char PROGMEM logo16_glcd_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);//display.setContrast (0); // dim display// Start OLEDdisplay.begin(0, true); // we dont use the i2c address but we will reset!// Show image buffer on the display hardware.// Since the buffer is intialized with an Adafruit splashscreen// internally, this will display the splashscreen.//display.display();//delay(2000);// Clear the buffer.display.clearDisplay();display.setCursor(8, 24);display.setTextSize(1);display.setTextColor(SH110X_WHITE);display.println("HAND SPEED TEST");display.setCursor(8, 36);display.println("Ver: 0.1");display.setCursor(8, 48);display.println("Date: 0820");display.display();delay(2000);display.clearDisplay(); pinMode (buttonpin,INPUT);pinMode (ledpin,OUTPUT);digitalWrite (ledpin,HIGH);delay (500);digitalWrite (ledpin,LOW);delay (500); startsignal=1;}void loop() {if(startsignal==1) {display.clearDisplay();display.setCursor(8, 8);display.setTextSize(2);display.setTextColor(SH110X_WHITE);display.println(" Look at ");display.println(" ");display.println(" LED!!!");display.display();rtime1=millis();}digitalWrite (ledpin,HIGH);startsignal=0;buttonpintwo=digitalRead(buttonpin);if(buttonpintwo==HIGH) { digitalWrite(ledpin,LOW);rtime2=millis();timebox=rtime2-rtime1;//Serial.print(timebox);//Serial.print("ms");//oleddisplay.clearDisplay();display.setCursor(0, 8);display.setTextSize(1);display.setTextColor(SH110X_WHITE);display.println("Your hand speed is ");display.setTextSize(2);display.setCursor(24, 24);if(timebox<999){display.print(timebox);display.println("ms"); }else{ display.println(">1s"); }display.display();delay(5000);display.clearDisplay();startsignal=1;}}
总结
arduino uno板(ATmega328P)的性能相比于555有了巨大的飞跃。通过arduino uno板(ATmega328P)实现的时间间隔感测试器的功能相比于通过555的实现多了很多。也基本了解了屏的驱动方法。
如果觉得《时间间隔感测试器(下):Arduino uno》对你有帮助,请点赞、收藏,并留下你的观点哦!