קורס אמבדד – 003 – RB18
RTC מה זה ? DS1307
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <Wire.h> #include <TimeLib.h> #include <DS1307RTC.h> void setup() { Serial.begin(9600); while (!Serial) ; // wait for serial delay(200); Serial.println("DS1307RTC Read Test"); Serial.println("-------------------"); } void loop() { tmElements_t tm; if (RTC.read(tm)) { Serial.print("Ok, Time = "); print2digits(tm.Hour); Serial.write(':'); print2digits(tm.Minute); Serial.write(':'); print2digits(tm.Second); Serial.print(", Date (D/M/Y) = "); Serial.print(tm.Day); Serial.write('/'); Serial.print(tm.Month); Serial.write('/'); Serial.print(tmYearToCalendar(tm.Year)); Serial.println(); } else { if (RTC.chipPresent()) { Serial.println("The DS1307 is stopped. Please run the SetTime"); Serial.println("example to initialize the time and begin running."); Serial.println(); } else { Serial.println("DS1307 read error! Please check the circuitry."); Serial.println(); } delay(9000); } delay(1000); } void print2digits(int number) { if (number >= 0 && number < 10) { Serial.write('0'); } Serial.print(number); } |
https://wokwi.com/projects/340968513860534868?fbclid=IwAR2wnFOohSxwchGhE44iAwQbLOcvAlBgAOO4n9JY4jSfORdNxKrsIvsC6do
אינטראפ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
bool pressed; uint32_t numberKeyPresses; void IRAM_ATTR isr() { // interrupt hundler numberKeyPresses++; pressed = true; // raise interrupt flag } void setup() { Serial.begin(115200); pinMode(18, INPUT_PULLUP); attachInterrupt(18, isr, FALLING); pressed=false; } void loop() { if (pressed) { Serial.printf("Button pressed %u times\n", numberKeyPresses); pressed = false; } } |
https://wokwi.com/projects/347702954229236307?fbclid=IwAR0sZC3fwq3u6FMWG2VFSSOyjAcrmtvfY695Xy48puzfgNF7kj1j7Gy5Z8o
פרוטוקול I2C – (הסבר כללי)
(בהמשך הקורס נילמד לעומק )
מסכים
OLED פשוטה
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED width, in pixels #define SCREEN_HEIGHT 64 // OLED height, in pixels // create an OLED display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with I2C address 0x3C if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("failed to start SSD1306 OLED")); while (1); } delay(2000); // wait two seconds for initializing oled.clearDisplay(); // clear display oled.setTextSize(1); // set text size oled.setTextColor(WHITE); // set text color oled.setCursor(0, 2); // set position to display (x,y) oled.println("Robotronix"); // set text oled.display(); // display on OLED } void loop() { } |
https://wokwi.com/projects/350341364144144978
https://wokwi.com/projects/new/esp32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{ "version": 1, "author": "יניב מאור", "editor": "wokwi", "parts": [ { "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": 14, "left": -75.33, "attrs": {} }, { "type": "board-ssd1306", "id": "oled1", "top": 106.62, "left": 49.17, "attrs": {} } ], "connections": [ [ "esp:TX0", "$serialMonitor:RX", "", [] ], [ "esp:RX0", "$serialMonitor:TX", "", [] ], [ "oled1:SCL", "esp:D22", "green", [ "v-63.52", "h-7.35" ] ], [ "esp:D21", "oled1:SDA", "blue", [ "h90.32", "v1.08" ] ], [ "oled1:VCC", "esp:3V3", "red", [ "v-19.12", "h-53.31", "v79.2" ] ], [ "esp:GND.1", "oled1:GND", "black", [ "h12.54", "v-74.37", "h52.92" ] ] ] } |
מסך 2 על 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
{ "version": 1, "author": "Arvind Patil", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 182.67, "left": 58.01, "attrs": {} }, { "type": "wokwi-lcd1602", "id": "lcd1", "top": -13.37, "left": 77.8, "attrs": { "pins": "i2c" } } ], "connections": [ [ "lcd1:SDA", "uno:A4.2", "green", [ "h-15.33", "v128.74", "h81.33" ] ], [ "lcd1:VCC", "uno:5V", "red", [ "h-24", "v256.24", "h156" ] ], [ "lcd1:GND", "uno:GND.2", "black", [ "h-48.67", "v255.07", "h184.67" ] ], [ "lcd1:SCL", "uno:A5.2", "white", [ "h-5.33", "v105.9", "h55.33" ] ] ] } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
//sketch created by Akshay Joseph #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { Serial.begin(9600); lcd.init(); lcd.backlight(); lcd.clear(); lcd.setCursor(1,0); lcd.print("write on lcd"); lcd.setCursor(1,1); lcd.print("by arvind patil"); delay(3000); lcd.clear(); lcd.setCursor(1,0); lcd.print("have a nice day"); lcd.setCursor(1,1); lcd.print("arvind patil"); delay(3000); } void loop() {Serial.print(" how to write on lcd by arvind patil "); delay(3000); lcd.clear(); lcd.setCursor(1,0); lcd.print("write on lcd"); lcd.setCursor(1,1); lcd.print("by arvind patil"); delay(3000); lcd.clear(); lcd.setCursor(1,0); lcd.print("have a nice day"); lcd.setCursor(1,1); lcd.print("arvind patil"); delay(20000); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// LCD1602 to Arduino Uno connection example #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 10, 9, 8, 7); void setup() { lcd.begin(20, 4); // you can now interact with the LCD, e.g.: lcd.print("first they love "); lcd.setCursor(0,1); lcd.print("then laught "); lcd.setCursor(0,2); delay(2000); lcd.print("then they happy "); lcd.setCursor(3,3); delay(2000); lcd.print("then you will win! "); } void loop() { //lcd.print("Hello World!"); } |
https://wokwi.com/projects/348205725313401427?fbclid=IwAR1eZJmpQ9RbegVqyygv8c1BRGa_UK5AsYRkrDpf7bPPh6ihbiZUBw-fvJA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED width, in pixels #define SCREEN_HEIGHT 64 // OLED height, in pixels // create an OLED display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with I2C address 0x3C if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("failed to start SSD1306 OLED")); while (1); } delay(2000); // wait two seconds for initializing oled.setCursor(0, 0); } void loop() { // draw a circle oled.clearDisplay(); oled.drawCircle(20, 35, 20, WHITE); oled.display(); delay(1000); // fill a circle oled.clearDisplay(); oled.fillCircle(20, 35, 20, WHITE); oled.display(); delay(1000); // draw a triangle oled.clearDisplay(); oled.drawTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(1000); // fill a triangle oled.clearDisplay(); oled.fillTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(1000); // draw a rectangle oled.clearDisplay(); oled.drawRect(0, 15, 60, 40, WHITE); oled.display(); delay(1000); // fill a rectangle oled.clearDisplay(); oled.fillRect(0, 15, 60, 40, WHITE); oled.display(); delay(1000); // draw a round rectangle oled.clearDisplay(); oled.drawRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(1000); // fill a round rectangle oled.clearDisplay(); oled.fillRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(1000); } |
גרפיקה
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED width, in pixels #define SCREEN_HEIGHT 64 // OLED height, in pixels // create an OLED display object connected to I2C Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // initialize OLED display with I2C address 0x3C if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("failed to start SSD1306 OLED")); while (1); } delay(2000); // wait two seconds for initializing oled.setCursor(0, 0); } void loop() { oled.clearDisplay(); // oled clear screen oled.drawLine(50,140,50,5,WHITE); // fill round rect on oled x:64 y:31 w:30 h:30 r:5 color:white oled.display(); // display rectal on oled delay(1000); // waiting 3sec // draw a circle oled.clearDisplay(); oled.drawCircle(20, 35, 20, WHITE); oled.display(); delay(1000); // fill a circle oled.clearDisplay(); oled.fillCircle(20, 35, 20, WHITE); oled.display(); delay(1000); // draw a triangle oled.clearDisplay(); oled.drawTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(1000); // fill a triangle oled.clearDisplay(); oled.fillTriangle(30, 15, 0, 60, 60, 60, WHITE); oled.display(); delay(1000); // draw a rectangle oled.clearDisplay(); oled.drawRect(0, 15, 60, 40, WHITE); oled.display(); delay(1000); // fill a rectangle oled.clearDisplay(); oled.fillRect(0, 15, 60, 40, WHITE); oled.display(); delay(1000); // draw a round rectangle oled.clearDisplay(); oled.drawRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(1000); // fill a round rectangle oled.clearDisplay(); oled.fillRoundRect(0, 15, 60, 40, 8, WHITE); oled.display(); delay(1000); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include "SPI.h" #include "Adafruit_GFX.h" #include "Adafruit_ILI9341.h" #define TFT_DC 9 #define TFT_CS 10 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); void setup() { tft.begin(); tft.setCursor(26, 120); tft.setTextColor(ILI9341_RED); tft.setTextSize(2); tft.println("Hello, Robotronix"); tft.setCursor(30, 160); tft.setTextColor(ILI9341_GREEN); tft.setTextSize(2); tft.println("2.4 lcd"); } void loop() { } |
https://wokwi.com/projects/350345239352836692
SPI
כתיבתה ללכרטיס SD CARD שימוש ב SPI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <SD.h> #define CS_PIN 10 File root; void setup() { Serial.begin(115200); Serial.print("Initializing SD card... "); if (!SD.begin(CS_PIN)) { Serial.println("Card initialization failed!"); while (true); } Serial.println("initialization done."); Serial.println("Files in the card:"); root = SD.open("/"); printDirectory(root, 0); Serial.println(""); // Example of reading file from the card: File textFile = SD.open("wokwi.txt"); if (textFile) { Serial.print("wokwi.txt: "); while (textFile.available()) { Serial.write(textFile.read()); } textFile.close(); } else { Serial.println("error opening wokwi.txt!"); } } void loop() { // nothing happens after setup finishes. } void printDirectory(File dir, int numTabs) { while (true) { File entry = dir.openNextFile(); if (! entry) { // no more files break; } for (uint8_t i = 0; i < numTabs; i++) { Serial.print('\t'); } Serial.print(entry.name()); if (entry.isDirectory()) { Serial.println("/"); printDirectory(entry, numTabs + 1); } else { // files have sizes, directories do not Serial.print("\t\t"); Serial.println(entry.size(), DEC); } entry.close(); } } |
https://wokwi.com/projects/345199259266581074?fbclid=IwAR2ZDzcN6FN9vb55SZu1BxvUFou-uBBnTe9j82GqRSZIGEiepXnKZ_v89Oo
RDB WS2811
תרגיל :
- מצא את הרכיב WS2811
- הר. את התוכנה
https://wokwi.com/projects/348544933189124690?fbclid=IwAR1bOD6pXdN-Ub3BwPUyIe0wZ9z73Hxi3d71NIlWJSOnDEApERJXc_JZV0o
4. WS281
כיצד עובד הרכיב
המשך
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "version": 1, "author": "Uri Shaked", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-nano", "id": "uno", "top": -85.51, "left": -139.01, "attrs": {} }, { "type": "wokwi-neopixel-matrix", "id": "neopixels", "top": -588.94, "left": -470.29, "attrs": { "rows": "8", "cols": "32", "matrixLayout": "serpentine" } } ], "connections": [ [ "uno:GND.1", "neopixels:VSS", "black", [ "v-20", "*", "h2" ] ], [ "uno:5V", "neopixels:VDD", "red", [ "v20", "h-95", "*", "v0", "h-5" ] ], [ "neopixels:GND", "uno:GND.1", "black", [ "v229.46", "h41.23" ] ], [ "neopixels:VCC", "uno:5V", "red", [ "v549.39", "h88.62" ] ], [ "neopixels:DIN", "uno:3", "green", [ "v226.43", "h130.31" ] ] ] } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
#include <FastLED.h> #define LED_PIN 3 #define COLOR_ORDER GRB #define CHIPSET WS2811 #define BRIGHTNESS 64 // Helper functions for an two-dimensional XY matrix of pixels. // Simple 2-D demo code is included as well. // // XY(x,y) takes x and y coordinates and returns an LED index number, // for use like this: leds[ XY(x,y) ] == CRGB::Red; // No error checking is performed on the ranges of x and y. // // XYsafe(x,y) takes x and y coordinates and returns an LED index number, // for use like this: leds[ XY(x,y) ] == CRGB::Red; // Error checking IS performed on the ranges of x and y, and an // index of "-1" is returned. Special instructions below // explain how to use this without having to do your own error // checking every time you use this function. // This is a slightly more advanced technique, and // it REQUIRES SPECIAL ADDITIONAL setup, described below. // Params for width and height const uint8_t kMatrixWidth = 32; const uint8_t kMatrixHeight = 8; // Param for different pixel layouts const bool kMatrixSerpentineLayout = true; // Set 'kMatrixSerpentineLayout' to false if your pixels are // laid out all running the same way, like this: // // 0 > 1 > 2 > 3 > 4 // | // .----<----<----<----' // | // 5 > 6 > 7 > 8 > 9 // | // .----<----<----<----' // | // 10 > 11 > 12 > 13 > 14 // | // .----<----<----<----' // | // 15 > 16 > 17 > 18 > 19 // // Set 'kMatrixSerpentineLayout' to true if your pixels are // laid out back-and-forth, like this: // // 0 > 1 > 2 > 3 > 4 // | // | // 9 < 8 < 7 < 6 < 5 // | // | // 10 > 11 > 12 > 13 > 14 // | // | // 19 < 18 < 17 < 16 < 15 // // Bonus vocabulary word: anything that goes one way // in one row, and then backwards in the next row, and so on // is call "boustrophedon", meaning "as the ox plows." // This function will return the right 'led index number' for // a given set of X and Y coordinates on your matrix. // IT DOES NOT CHECK THE COORDINATE BOUNDARIES. // That's up to you. Don't pass it bogus values. // // Use the "XY" function like this: // // for( uint8_t x = 0; x < kMatrixWidth; x++) { // for( uint8_t y = 0; y < kMatrixHeight; y++) { // // // Here's the x, y to 'led index' in action: // leds[ XY( x, y) ] = CHSV( random8(), 255, 255); // // } // } // // uint16_t XY( uint8_t x, uint8_t y) { uint16_t i; if( kMatrixSerpentineLayout == false) { i = (y * kMatrixWidth) + x; } if( kMatrixSerpentineLayout == true) { if( y & 0x01) { // Odd rows run backwards uint8_t reverseX = (kMatrixWidth - 1) - x; i = (y * kMatrixWidth) + reverseX; } else { // Even rows run forwards i = (y * kMatrixWidth) + x; } } return i; } // Once you've gotten the basics working (AND NOT UNTIL THEN!) // here's a helpful technique that can be tricky to set up, but // then helps you avoid the needs for sprinkling array-bound-checking // throughout your code. // // It requires a careful attention to get it set up correctly, but // can potentially make your code smaller and faster. // // Suppose you have an 8 x 5 matrix of 40 LEDs. Normally, you'd // delcare your leds array like this: // CRGB leds[40]; // But instead of that, declare an LED buffer with one extra pixel in // it, "leds_plus_safety_pixel". Then declare "leds" as a pointer to // that array, but starting with the 2nd element (id=1) of that array: // CRGB leds_with_safety_pixel[41]; // CRGB* const leds( leds_plus_safety_pixel + 1); // Then you use the "leds" array as you normally would. // Now "leds[0..N]" are aliases for "leds_plus_safety_pixel[1..(N+1)]", // AND leds[-1] is now a legitimate and safe alias for leds_plus_safety_pixel[0]. // leds_plus_safety_pixel[0] aka leds[-1] is now your "safety pixel". // // Now instead of using the XY function above, use the one below, "XYsafe". // // If the X and Y values are 'in bounds', this function will return an index // into the visible led array, same as "XY" does. // HOWEVER -- and this is the trick -- if the X or Y values // are out of bounds, this function will return an index of -1. // And since leds[-1] is actually just an alias for leds_plus_safety_pixel[0], // it's a totally safe and legal place to access. And since the 'safety pixel' // falls 'outside' the visible part of the LED array, anything you write // there is hidden from view automatically. // Thus, this line of code is totally safe, regardless of the actual size of // your matrix: // leds[ XYsafe( random8(), random8() ) ] = CHSV( random8(), 255, 255); // // The only catch here is that while this makes it safe to read from and // write to 'any pixel', there's really only ONE 'safety pixel'. No matter // what out-of-bounds coordinates you write to, you'll really be writing to // that one safety pixel. And if you try to READ from the safety pixel, // you'll read whatever was written there last, reglardless of what coordinates // were supplied. #define NUM_LEDS (kMatrixWidth * kMatrixHeight) CRGB leds_plus_safety_pixel[ NUM_LEDS + 1]; CRGB* const leds( leds_plus_safety_pixel + 1); uint16_t XYsafe( uint8_t x, uint8_t y) { if( x >= kMatrixWidth) return -1; if( y >= kMatrixHeight) return -1; return XY(x,y); } // Demo that USES "XY" follows code below void loop() { uint32_t ms = millis(); int32_t yHueDelta32 = ((int32_t)cos16( ms * (27/1) ) * (350 / kMatrixWidth)); int32_t xHueDelta32 = ((int32_t)cos16( ms * (39/1) ) * (310 / kMatrixHeight)); DrawOneFrame( ms / 65536, yHueDelta32 / 32768, xHueDelta32 / 32768); if( ms < 5000 ) { FastLED.setBrightness( scale8( BRIGHTNESS, (ms * 256) / 5000)); } else { FastLED.setBrightness(BRIGHTNESS); } FastLED.show(); } void DrawOneFrame( byte startHue8, int8_t yHueDelta8, int8_t xHueDelta8) { byte lineStartHue = startHue8; for( byte y = 0; y < kMatrixHeight; y++) { lineStartHue += yHueDelta8; byte pixelHue = lineStartHue; for( byte x = 0; x < kMatrixWidth; x++) { pixelHue += xHueDelta8; leds[ XY(x, y)] = CHSV( pixelHue, 255, 255); } } } void setup() { FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050); FastLED.setBrightness( BRIGHTNESS ); } |
https://wokwi.com/projects/329819476151239252?fbclid=IwAR0cRCUTrhzEFz9ospxLws_cQ9yrg8ptsGT4n33GZOXNAUQYX-3EFLyMmk8