初始化提交

This commit is contained in:
王立帮
2024-07-20 22:09:06 +08:00
commit c247dd07a6
6876 changed files with 2743096 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
/*
Basic usage example
Demonstrated some of the basic functionality of the library. Initialize the display,
set the backlight brightness, print some text, count from 0 to 100 and
print and blink some text.
Note: make sure to set your serial monitor to line end: NEW LINE!
The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND
Created 25 September 2015
By Bram Harmsen
https://github.com/bremme/arduino-tm1637
*/
// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 4; // define CLK pin (any digital pin)
const byte PIN_DIO = 5; // define DIO pin (any digital pin)
SevenSegmentTM1637 display(PIN_CLK, PIN_DIO);
// run setup code
void setup() {
Serial.begin(9600); // initializes the Serial connection @ 9600 baud
display.begin(); // initializes the display
display.setBacklight(100); // set the brightness to 100 %
display.print("INIT"); // display INIT on the display
delay(1000); // wait 1000 ms
};
// run loop (forever)
void loop() {
display.print("LOOP"); // display LOOP on the display
delay(1000); // wait 1000 ms
display.print("COUNTING SOME DIGITS");// print COUNTING SOME DIGITS
display.clear(); // clear the display
for (uint8_t i=0; i < 100; i++) { // loop from 0 to 100
display.print(i); // display loop counter
delay(100); // wait 100 ms
};
display.clear(); // clear the display
display.print("SUCC"); // print SUCC for success
display.blink(); // blink SUCC
delay(1000); // wait 1000 ms
};

View File

@@ -0,0 +1,53 @@
/*
Clock example example (Extended class example)
Display a clock on the display. For this demo you can add a speed multiplier to make the clock run faster. For a real clock you want to use a delay of 1 min or even use a Real Time Clock module (RTC)
The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND
Created 25 September 2015
By Bram Harmsen
https://github.com/bremme/arduino-tm1637
*/
// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 4; // define CLK pin (any digital pin)
const byte PIN_DIO = 5; // define DIO pin (any digital pin)
SevenSegmentExtended display(PIN_CLK, PIN_DIO);
const unsigned int clockSpeed = 10000; // speed up clock for demo
// run setup code
void setup() {
Serial.begin(9600); // initializes the Serial connection @ 9600 baud
display.begin(); // initializes the display
display.setBacklight(100); // set the brightness to 100 %
delay(1000); // wait 1000 ms
};
// run loop (forever)
void loop() {
byte hours = 14; // initialize hours
byte minutes = 39; // initialize minutes
for ( ; hours < 24; hours++) { // count hours up to 24
for ( ; minutes < 60; minutes++) { // count minutes up to 59
display.printTime(hours, minutes, true); // display time
delay(60000 / clockSpeed); // clock delay ms
};
minutes = 0; // reset minutes
};
};

View File

@@ -0,0 +1,114 @@
/*
Dual counter example (Extended class example)
You could do many things with a dual counter. This example demoonstrates a scoreboard. Two players rol a dice every round, the player with the highest score wins the round. The number of rounds won are displayed on the scoreboard. Once one of the players reaches a maximum score the game is over. At the end of the game a player won message or draw will be displayed.
The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND
Created 25 September 2015
By Bram Harmsen
https://github.com/bremme/arduino-tm1637
*/
// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 4; // define CLK pin (any digital pin)
const byte PIN_DIO = 5; // define DIO pin (any digital pin)
SevenSegmentExtended display(PIN_CLK, PIN_DIO);
// sets the maximum score for the game
const byte maxScore = 10;
// run setup code
void setup() {
Serial.begin(9600); // initializes the Serial connection @ 9600 baud
display.begin(); // initializes the display
display.setBacklight(100); // set the brightness to 100 %
delay(1000); // wait 1000 ms
randomSeed(analogRead(0)); // get a random seed for throwing dices
};
// run loop (forever)
void loop() {
byte playerOneScore = 0; // initialize player one score
byte playerTwoScore = 0; // initialize player two score
bool gameEnd = false; // initialize gameEnd?
display.print("START NEW GAME "); // Start a new round
delay(500); // delay 1000 ms
display.clear(); // clear the display
while( !gameEnd ) { // play till the end of the game
byte playerOneTurn = rollDice(); // player one rolls a dice
byte playerTwoTurn = rollDice(); // player two rolls a dice
if ( playerOneTurn > playerTwoTurn ) { // player one had a higher dice
playerOneScore++; // increase player one's score
} else if ( playerOneTurn < playerTwoTurn ){
playerTwoScore++; // increase player two's score
} else { // player one and two had same score
display.clear();
display.print("SAME");
display.blink();
display.clear();
}
// update scoreboard
display.printDualCounter(playerOneScore, playerTwoScore);
delay(500);
// check if game has ended (player one or two's score >= maxScore)
if ( (playerOneScore >= maxScore || playerTwoScore >= maxScore) ) {
gameEnd = true; // game has ended
printGameEnd(); // print end game message
break; // break from while loop (jump out)
};
};
// check who won
if ( playerOneScore > playerTwoScore ) {
printPlayerWon(1); // player one won
} else if ( playerOneScore < playerTwoScore ){
printPlayerWon(2); // player two won
} else {
display.clear(); display.print("DRAW"); // draw
display.blink(); display.clear();
};
};
// roll a dice: random rumber fom 0 to 5 + 1 = 0 to 6
byte rollDice() {
return (random(6) + 1);
}
// print winning player message
void printPlayerWon(byte player) {
display.clear();
char winText[] = "PLAYER x WON ";
winText[7] = (char)player + '0';
display.print(winText);
delay(500);
display.clear();
};
// print end game message
void printGameEnd(void) {
display.clear();
display.print("GAME END ");
display.blink();
delay(500);
};

View File

@@ -0,0 +1,139 @@
/*
Fun Print all example
This is a demonstration off a lot of the possibilities of the Fun superclass
The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND
Created 25 September 2015
By Bram Harmsen
https://github.com/bremme/arduino-tm1637
*/
// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
#include "SevenSegmentFun.h"
/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 4; // define CLK pin (any digital pin)
const byte PIN_DIO = 5; // define DIO pin (any digital pin)
SevenSegmentFun display(PIN_CLK, PIN_DIO);
// run setup code
void setup() {
Serial.begin(9600); // initializes the Serial connection @ 9600 baud
display.begin(); // initializes the display
display.setBacklight(100); // set the brightness to 100 %
delay(1000); // wait 1000 ms
};
// run loop (forever)
void loop() {
// vertical level (e.g. audio volume)
introDuceNextDemo("AUDIO VOLUME DEMO");
audioVolume();
// bouncing ball
introDuceNextDemo("BOUNCING BALL DEMO");
unsigned int numMoves = 100; unsigned int timeDelay = 100;
display.bouncingBall(numMoves, timeDelay);
// scrolling text
introDuceNextDemo("SCROLLING TEXT DEMO");
byte repeats = 2;
display.scrollingText("ARDUINO TM1637 FUN", repeats);
// nightrider
introDuceNextDemo("REMEMBER KIT? NIGHTRIDER DEMO");
repeats = 4;
display.nightrider(repeats);
// snake
introDuceNextDemo("SNAKE DEMO");
display.snake(repeats);
// horizontal level (e.g equalizer)
introDuceNextDemo("EQUALIZER DEMO");
equalizer();
// bomb timer
introDuceNextDemo("GET READY FOR THE BOMB");
byte hours = 5; byte min = 16; unsigned int speed = 10000;
display.bombTimer(hours, min, speed, " RUN ");
delay(1000);;
};
// Demo for displaying a vertical level, for example audio volume, battery charge etc.
void audioVolume() {
for (byte repeats=0; repeats < 2; repeats++) {
for (byte level=0; level < 125; level+=25) {
display.printLevelVertical(level);
delay(200);
};
for (byte level=100; level != 0; level-=25) {
display.printLevelVertical(level);
delay(200);
};
}
// maybe try another symbol instead of default | |
byte symbol = display.encode((byte)0);
bool leftToRight = false; // print the other direction
for (byte repeats=0; repeats < 2; repeats++) {
for (byte level=0; level < 125; level+=25) {
display.printLevelVertical(level, leftToRight, symbol);
delay(200);
if ( level == 100 ) {
display.blink();
}
};
for (byte level=100; level != 0; level-=25) {
display.printLevelVertical(level, leftToRight, symbol);
delay(200);
};
}
// 0, 25, 50, 75, 100
}
void equalizer() {
// initialize horizontal level counters
char i,j,k,l;
byte levels[4];
// repeat 5 times
for ( byte r=0; r < 4; r++) {
// increae i and k, decrease j and l
for (i=0,j=100,k=0,l=100; i <= 100; i+=33, j-=33, k+=33, l-=33) {
levels[0] = i; levels[1] = j; levels[2] = k; levels[3] = l;
display.printLevelHorizontal(levels);
delay(100);
}
// increae j and l, decrease i and k
for (i=100,j=0,k=100,l=0; i >= 0; i-=33, j+=33, k-=33, l+=33) {
levels[0] = i; levels[1] = j; levels[2] = k; levels[3] = l;
display.printLevelHorizontal(levels);
delay(100);
}
}
}
void introDuceNextDemo(char* str) {
display.print(str);
delay(500);
}

View File

@@ -0,0 +1,52 @@
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
#include "SevenSegmentFun.h"
// define clock and digital input pins
#define PIN_CLK 4
#define PIN_DIO 5
// initialize TM1637 Display objects
SevenSegmentFun display(PIN_CLK, PIN_DIO);
void setup() {
// initialize the display
display.begin();
}
void loop() {
// increase level from 0 to 100
for (uint8_t i=0; i <= 100; i+=25) {
display.printLevelVertical(i);
delay(100);
};
// decrease level from 100 to 0
for (int8_t i=100; i >=0; i-=25) {
display.printLevelVertical(i);
delay(100);
}
// initialize horizontal level counters
int8_t i,j,k,l;
uint8_t levels[4];
// repeat 5 times
for ( uint8_t r=0; r < 4; r++) {
// increae i and k, decrease j and l
for (i=0,j=100,k=0,l=100; i <= 100; i+=33, j-=33, k+=33, l-=33) {
levels[0] = i; levels[1] = j; levels[2] = k; levels[3] = l;
display.printLevelHorizontal(levels);
delay(100);
}
// increae j and l, decrease i and k
for (i=100,j=0,k=100,l=0; i >= 0; i-=33, j+=33, k-=33, l+=33) {
levels[0] = i; levels[1] = j; levels[2] = k; levels[3] = l;
display.printLevelHorizontal(levels);
delay(100);
}
}
}

View File

@@ -0,0 +1,111 @@
/*
Low level (advanced) example
In this example the display is controlled by the low level command function.
See SevenSegmentTM1637.h for more details on how the protocol actually works and which commands it accepts.
The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND
Created 25 September 2015
By Bram Harmsen
https://github.com/bremme/arduino-tm1637
*/
// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 4; // define CLK pin (any digital pin)
const byte PIN_DIO = 5; // define DIO pin (any digital pin)
SevenSegmentTM1637 display(PIN_CLK, PIN_DIO);
// run setup code
void setup() {
Serial.begin(9600); // initializes the Serial connection @ 9600 baud
};
void loop() {
// set brightness
uint8_t setDisplayCmd = B10000000; // bit 6,7
uint8_t brightnessBits = B111; // bit 0,1,2 (7 = max)
uint8_t displayOnBit = B1000; // bit 3
// construct complete command
uint8_t command = setDisplayCmd | brightnessBits | displayOnBit;
// turn display on and set brightness to max (7)
bool ack = display.command(command);
// print acknowledged?
Serial.println(ack);
// write init to display using automatic address
uint8_t setDataCmd = B01000000; // bit 6,7
ack = display.command(setDataCmd);
Serial.println(ack);
uint8_t setAddrCmd = B11000000; // bit 6,7 set addr to 0
uint8_t dataI = B00000110; // I
uint8_t dataN = B00110111; // N
uint8_t dataT = B01111000; // T
uint8_t commands[5];
commands[0] = setAddrCmd;
commands[1] = dataI;
commands[2] = dataN;
commands[3] = dataI;
commands[4] = dataT;
ack = display.command(commands, 5);
Serial.println(ack);
delay(1000);
// dim display
brightnessBits = B001; // bit 0,1,2 level=1;
command = setDisplayCmd | brightnessBits | displayOnBit;
display.command(command);
delay(1000);
// set back to full brightness
brightnessBits = B111; // bit 0,1,2 (7 = max)
command = setDisplayCmd | brightnessBits | displayOnBit;
display.command(command);
delay(1000);
// write characters to display in non auto mode
setDataCmd |= 1 << 2; // bit 2 control auto/manual address
ack = display.command(setDataCmd);
Serial.println(ack);
commands[1] = B01110111;
commands[2] = B01111111;
commands[3] = B00111001;
commands[4] = B01011110;
for (uint8_t i = 1; i < 5; i++) {
commands[1] = commands[i]; // set character
display.command(commands, 2); // write character to display
commands[0]++; // increase address
}
delay(1000);
// set back to auto address mode
setDataCmd &= ~(1 << 2); // bit 2 control auto/manual address
ack = display.command(setDataCmd);
Serial.println(ack);
// clear display
commands[0] = setAddrCmd;
commands[1] = 0; commands[2] = 0;commands[3] = 0; commands[4] = 0;
// clear display
display.command(commands, 5);
delay(1000);
}

View File

@@ -0,0 +1,76 @@
/*
Printing numbers example
Demonstrate a couple different ways nubers can be printed: positive, negative,
with padding and with rollover.
The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND
Created 22 June 2020
By Bram Harmsen
https://github.com/bremme/arduino-tm1637
*/
// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 4; // define CLK pin (any digital pin)
const byte PIN_DIO = 5; // define DIO pin (any digital pin)
SevenSegmentExtended display(PIN_CLK, PIN_DIO);
// run setup code
void setup() {
Serial.begin(9600); // initializes the Serial connection @ 9600 baud
display.begin(); // initializes the display
display.setBacklight(100); // set the brightness to 100 %
display.print("INIT"); // display INIT on the display
delay(1000); // wait 1000 ms
};
// run loop (forever)
void loop() {
// print positive numbers
display.print("PRINT POSITIVE NUMBERS");
delay(1000); // wait 1000 ms
for (int16_t number=0; number < 2000; number++) {
display.printNumber(number);
delay(1);
};
// print negative numbers
display.print("PRINT NEGATIVE NUMBERS");
delay(1000); // wait 1000 ms
for (int16_t number=0; number > -999; number--) {
display.printNumber(number);
delay(2);
};
// print with positive with zero padding
display.print("PRINT POSITIVE NUMBERS WITH PADDING");
delay(1000); // wait 1000 ms
for (int16_t number=0; number < 2000; number++) {
display.printNumber(number, true);
delay(1);
};
// print with rollover (e.g. 10000 -> 0, 10001 -> 1)
display.print("PRINT POSITIVE NUMBERS WITH ROLLOVER");
delay(1000); // wait 1000 ms
for (int16_t number=0; number < 20000; number+=250) {
display.printNumber(number, false, true);
delay(50);
};
};

View File

@@ -0,0 +1,79 @@
/*
Serial Print example
Prints text typed to the Serial Monitor on the display. Connect an Arduino to a TM1637 4 digit 7-segment display. Connect the wires as descibed below. Run the sketch and open the Serial Monitor. Set the the speed to 9600 baud, set the line ending to Newline. Now type some text and press enter, the text will be displayed on the 7-segment display.
Note: make sure to set your serial monitor to line end: NEW LINE!
The circuit:
* connect TM1637 pin CLK to Arduino pin D4
* connect TM1637 pin DIO to Arduino pin D5
* connect TM1637 pin Vcc to Arduino pin 5V
* connect TM1637 pin GND to Arduino pin GND
Created 25 September 2015
By Bram Harmsen
http://url/of/online/tutorial.cc
*/
// include the SevenSegmentTM1637 library
#include "SevenSegmentTM1637.h"
/* initialize global TM1637 Display object
* The constructor takes two arguments, the number of the clock pin and the digital output pin:
* SevenSegmentTM1637(byte pinCLK, byte pinDIO);
*/
const byte PIN_CLK = 4; // define CLK pin (any digital pin)
const byte PIN_DIO = 5; // define DIO pin (any digital pin)
SevenSegmentTM1637 display(PIN_CLK, PIN_DIO);
// define a fixed buffer size for receiving characters via Serial
const byte BUFFER_SIZE = 128;
char serialBuffer[BUFFER_SIZE]; // initialize global serial buffer
// run setup code
void setup() {
display.begin(); // initializes the display
Serial.begin(9600); // initializes the Serial connection @ 9600 baud
Serial.println(F("Enter some text followed by ENTER"));
Serial.println(F("also make sure to set the line ending to Newline (\\n)"));
};
// run loop (forever)
void loop() {
// if received new serial data, print to display
if ( receivedSerialString() ) {
display.print(serialBuffer); // Print received serial data to display
Serial.print(F("Echo:\t")); // Echo serial data back to Serial Monitor
Serial.println(serialBuffer);
};
};
// Read in bytes from Serial Monitor and return true is ether the serialBuffer is full or a Newline character is received
bool receivedSerialString() {
static unsigned int i=0; // init static counter to keep track of count
while( Serial.available() ) { // check if new data arrived
if ( i == BUFFER_SIZE-1) { // if buffer is full RETURN true
serialBuffer[i] = '\0'; // add termination char
i = 0;
Serial.println(F("Buffer full!"));
return true;
};
char c = Serial.read(); // read new char from serial port
if ( c == '\n') { // if new line RETURN true
serialBuffer[i] = '\0'; // add termination char
i = 0;
return true;
} else {
serialBuffer[i] = c; // add received character to buffer
}
i++; // increase counter
};
return false; // default RETURN false
};