From ac052d190ae3e7c0c9c5f21358996bb22397f47e Mon Sep 17 00:00:00 2001 From: sneak Date: Sun, 9 Jan 2022 05:40:55 -0800 Subject: [PATCH] some arduino sketches --- arduino/i2c-lcd/i2c-lcd.ino | 131 ++++++++++++++++++++++++ arduino/infrared-read/infrared-read.ino | 51 +++++++++ arduino/quadalphanum/quadalphanum.ino | 82 +++++++++++++++ arduino/wwvb/wwvb.ino | 110 ++++++++++++++++++++ 4 files changed, 374 insertions(+) create mode 100644 arduino/i2c-lcd/i2c-lcd.ino create mode 100644 arduino/infrared-read/infrared-read.ino create mode 100644 arduino/quadalphanum/quadalphanum.ino create mode 100644 arduino/wwvb/wwvb.ino diff --git a/arduino/i2c-lcd/i2c-lcd.ino b/arduino/i2c-lcd/i2c-lcd.ino new file mode 100644 index 0000000..c621b0b --- /dev/null +++ b/arduino/i2c-lcd/i2c-lcd.ino @@ -0,0 +1,131 @@ +#define SSD1306_NO_SPLASH + +// OLED display width and height, in pixels +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 32 +#define OLED_RESET -1 +// i2c address for oled +///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 +#define SCREEN_ADDRESS 0x3C + +#include +#include +#include +#include +#include +#include +#include +#include + +// set the LCD address to 0x27 for a 16 chars and 2 line display +LiquidCrystal_I2C lcd(0x27, 16, 2); + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +void setup() { + lcd.init(); + lcd.backlight(); + lcd.setCursor(0, 0); + lcd.print("0x123456789abcde"); + lcd.setCursor(0, 1); + lcd.print("fghijklmnopqrstu"); + + Serial.begin(115200); + + // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally + if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { + Serial.println(F("SSD1306 allocation failed")); + for (;;); // Don't proceed, loop forever + } + + display.display(); + delay(50); // Pause for 2 seconds + + // Clear the buffer + display.clearDisplay(); + + testdrawstyles(); // Draw 'stylized' characters + + panic(); + // + // // Invert and restore display, pausing in-between + // display.invertDisplay(true); + // delay(1000); + // display.invertDisplay(false); + // delay(1000); + +} + +void panic() { + for (;;) { + display.invertDisplay(true); + delay(1000); + } +} + + +void loop() +{ +} + +#define LINE_HEIGHT_PX 9 + +void testdrawstyles(void) { + display.clearDisplay(); + display.setTextSize(1); // Normal 1:1 pixel scale + display.setTextColor(SSD1306_WHITE); // Draw white text + display.setCursor(0, 0 * LINE_HEIGHT_PX); + display.println(F("123456789112345678921")); + display.setCursor(0, 1 * LINE_HEIGHT_PX); + display.println(F("234567893123456789412")); + display.setCursor(0, 2 * LINE_HEIGHT_PX); + display.println(F("345678951234567896123")); + display.display(); + + display.startscrollright(0x00, 0xFF); + + + + /* + + display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text + display.println(3.141592); + + display.setTextSize(2); // Draw 2X-scale text + display.setTextColor(SSD1306_WHITE); + display.print(F("0x")); display.println(0xDEADBEEF, HEX); + + display.display(); + delay(2000); + */ +} + +/* + void testscrolltext(void) { + display.clearDisplay(); + + display.setTextSize(1.5); // Draw 2X-scale text + display.setTextColor(SSD1306_WHITE); + display.setCursor(10, 0); + display.println(F("1337CAFE")); + display.display(); // Show initial text + delay(70); + + // Scroll in various directions, pausing in-between: + 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); + } + +*/ diff --git a/arduino/infrared-read/infrared-read.ino b/arduino/infrared-read/infrared-read.ino new file mode 100644 index 0000000..f4b54a5 --- /dev/null +++ b/arduino/infrared-read/infrared-read.ino @@ -0,0 +1,51 @@ +/* + ESP8266 BlinkWithoutDelay by Simon Peter + Blink the blue LED on the ESP-01 module + Based on the Arduino Blink without Delay example + This example code is in the public domain + + The blue LED on the ESP-01 module is connected to GPIO1 + (which is also the TXD pin; so we cannot use Serial.print() at the same time) + + Note that this sketch uses LED_BUILTIN to find the pin with the internal LED +*/ +#define IR_RX_PIN 14 +#define IR_TX_PIN 12 +#define SERIAL_BPS 115200 + +#define POWER_BUTTON 0xFFEA15 +#define EDIT_BUTTON 0xFF7887 +#define EXIT_BUTTON 0xFF38C7 +#define ONE_BUTTON 0xFF08F7 +#define TWO_BUTTON 0xFF8877 +#define THREE_BUTTON 0xFF48B7 +#define FOUR_BUTTON 0xFFC837 +#define FIVE_BUTTON 0xFF28D7 +#define SIX_BUTTON 0xFFA857 +#define SEVEN_BUTTON 0xFFE817 +#define EIGHT_BUTTON 0xFF18E7 +#define NINE_BUTTON 0xFF9867 +#define ZERO_BUTTON 0xFFB847 + +#include + +IRrecv irrecv(IR_RX_PIN); +decode_results results; + +int ledState = LOW; +volatile byte IRInputState; + +void setup() { + IrReceiver.begin(IR_RX_PIN, ENABLE_LED_FEEDBACK); // Start the receiver + pinMode(LED_BUILTIN, OUTPUT); + Serial.begin(SERIAL_BPS); + Serial.print("herro booted\n"); +} + +void loop() { + if (IrReceiver.decode()) { + Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); + IrReceiver.printIRResultShort(&Serial); // optional use new print version + IrReceiver.resume(); // Enable receiving of the next value + } +} diff --git a/arduino/quadalphanum/quadalphanum.ino b/arduino/quadalphanum/quadalphanum.ino new file mode 100644 index 0000000..2e4d7cc --- /dev/null +++ b/arduino/quadalphanum/quadalphanum.ino @@ -0,0 +1,82 @@ +// Demo the quad alphanumeric display LED backpack kit +// scrolls through every character, then scrolls Serial +// input onto the display + +#include +#include +#include "Adafruit_LEDBackpack.h" + +Adafruit_AlphaNum4 one = Adafruit_AlphaNum4(); +Adafruit_AlphaNum4 two = Adafruit_AlphaNum4(); + +unsigned long ticks; + + +void setup() { + Serial.begin(9600); + + one.begin(0x71); // pass in the address + two.begin(0x70); + + one.writeDigitRaw(3, 0x0); + one.writeDigitRaw(0, 0xFFFF); + one.writeDisplay(); + delay(200); + one.writeDigitRaw(0, 0x0); + one.writeDigitRaw(1, 0xFFFF); + one.writeDisplay(); + delay(200); + one.writeDigitRaw(1, 0x0); + one.writeDigitRaw(2, 0xFFFF); + one.writeDisplay(); + delay(200); + one.writeDigitRaw(2, 0x0); + one.writeDigitRaw(3, 0xFFFF); + one.writeDisplay(); + delay(200); + + one.clear(); + one.writeDisplay(); + + two.clear(); + two.writeDisplay(); + + one.writeDigitAscii(0, '1'); + one.writeDigitAscii(1, '2'); + one.writeDigitAscii(2, '0'); + one.writeDigitAscii(3, '0'); + two.writeDigitAscii(0, '0'); + two.writeDigitAscii(1, '1'); + two.writeDigitAscii(2, '2'); + two.writeDigitAscii(3, '3'); + one.writeDisplay(); + two.writeDisplay(); + + delay(300); + + Serial.println("Start typing to display!"); +} + + +char displaybuffer[4] = {' ', ' ', ' ', ' '}; + +void loop() { + + ticks = millis(); + + + sprintf(displaybuffer, "%02d", ticks/1000); + sprintf(displaybuffer+2, "%02d", ticks-(ticks/1000)*1000); + + Serial.println(displaybuffer); + + // set every digit to the buffer + two.writeDigitAscii(0, displaybuffer[0]); + two.writeDigitAscii(1, displaybuffer[1]); + two.writeDigitAscii(2, displaybuffer[2]); + two.writeDigitAscii(3, displaybuffer[3]); + + // write it out! + two.writeDisplay(); + delay(10); +} diff --git a/arduino/wwvb/wwvb.ino b/arduino/wwvb/wwvb.ino new file mode 100644 index 0000000..35d2fda --- /dev/null +++ b/arduino/wwvb/wwvb.ino @@ -0,0 +1,110 @@ +/* + ESP8266 Blink by Simon Peter + Blink the blue LED on the ESP-01 module + This example code is in the public domain + + The blue LED on the ESP-01 module is connected to GPIO1 + (which is also the TXD pin; so we cannot use Serial.print() at the same time) + + Note that this sketch uses LED_BUILTIN to find the pin with the internal LED +*/ + +#define WWV_SIGNAL_PIN 14 + +void ICACHE_RAM_ATTR readLevel(); + +volatile byte wwvbInState; // store receiver signal level + +byte prevWwvbInState; // store previous signal level +unsigned int prevEdgeMillis; // store time signal was read +byte bitVal; // bit decoded 0, 1 or Mark +byte badBit; // bad bit, noise detected +byte prevMark; // store previous mark bit + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output + pinMode(WWV_SIGNAL_PIN, INPUT); + attachInterrupt(digitalPinToInterrupt(WWV_SIGNAL_PIN), readLevel, CHANGE); // fire interrupt on edge detected + Serial.begin(9600); + Serial.print("herro booted\n"); +} + +// the loop function runs over and over again forever +void loop() { + if (wwvbInState != prevWwvbInState) { + pulseValue(); + prevWwvbInState = wwvbInState; + } + yield(); +} + +void pulseValue() { + unsigned int edgeMillis = millis(); // save current time + badBit = 0; // set noise counter to zero + if (wwvbInState == 1) { // rising edge + prevEdgeMillis = edgeMillis; // set previous time to current + } + else { // falling edge + int pulseLength = edgeMillis - prevEdgeMillis; // calculate pulse length millis + if (pulseLength < 100) { // less than 100ms, noise pulses + badBit = 1; + } + else if (pulseLength < 400) { // 800ms carrier drop mark + bitVal = 2; + } + else if (pulseLength < 700) { // 500ms carrier drop one + bitVal = 1; + } + else { // 200ms carrier drop zero + bitVal = 0; + } + if (badBit == 0) { + printBitVal(); + } + } +} + + +void readLevel() { + wwvbInState = digitalRead(WWV_SIGNAL_PIN); // read signal level + digitalWrite(LED_BUILTIN, !wwvbInState); // flash WWVB receiver indicator pin + yield(); + +} + + +void printBitVal() { + if ((bitVal == 2) && (prevMark == 0)) { + Serial.print(" : "); + + prevMark = 1; + } + else if ((bitVal == 2) && (prevMark == 1)) { + Serial.print("\nBit Value: "); + Serial.print("| "); + + prevMark = 0; + } + else { + Serial.print(bitVal, DEC); + prevMark = 0; + } +} + +/***************************************************************************** + * Time display functions + *****************************************************************************/ + +void printTime() { + Serial.print("?x00?y0?f"); // movie cursor to line 1 char 1, clear screen +} + +// LCD routines to initialize LCD and clear screen +void lcdInit() { // using P H Anderson Serial LCD driver board + Serial.print("?G216"); // configure driver for 2 x 16 LCD + delay(300); + Serial.print("?BDD"); // set backlight brightness + delay(300); + Serial.print("?f"); // clear screen + Serial.print("?c0"); // set cursor off +}