2024-02-26
Nicolas De Coster & Henk Buursen
Consider:
AtTiny microcontrollers are low-cost and low-power devices suitable for simple embedded applications with limited processing and I/O requirements.
Programming : UPDI (older : ISP)
AtTiny (412 / 1614 / 3216 / ...)
SAMD microcontrollers offer a balance of performance and power efficiency, making them suitable for a wide range of applications including IoT devices and wearables.
Programming : SWD / JTAG
SAMD (11C, 11D, 21E, D51)
The RP2040, developed by Raspberry Pi, is a dual-core ARM Cortex-M0+ microcontroller with versatile I/O capabilities (PIO!), making it ideal for projects requiring multitasking and connectivity.
Programming : UF2
RP2040
ESP32-C3 and ESP32-S3 are developed by Espressif Systems, known for their built-in Wi-Fi and Bluetooth capabilities.
→ IoT projects requiring wireless connectivity
Always refer to the microcontroller's datasheet and reference manual for detailed specifications and usage instructions. These documents provide essential information for programming and configuring the microcontroller.
Bootcamp instructors 2024 : different languages tests
Assembly language provides direct control over the microcontroller's hardware, making it efficient but complex to write. Programmable I/O (PIO) offers a way to offload I/O operations from the CPU, enhancing performance in certain scenarios.
Learn binary/logic basics! (worth it!)
C and C++ are widely used for microcontroller programming due to their efficiency and low-level access to hardware. They are compiled languages, meaning the code is translated into machine language before execution.
Note : Arduino is C++ based
Rust is gaining popularity for microcontroller development due to its strong safety guarantees and modern features. Like C and C++, Rust code is compiled before execution.
MicroPython offers a higher level of abstraction compared to C and assembly, making it easier to write and understand code. It can be either interpreted directly or precompiled into bytecode for execution.
JavaScript can be used for microcontroller programming (Kaluma / Esrpuino). It offers a familiar syntax for web developers but typically requires more resources compared to lower-level languages like C.
TinyGo brings the Go language to microcontrollers.
Mixing languages might give you the best of each world. E.g. : ASM → C → C++
import array, time import rp2 import machine NUM_LEDS = 10 BLACK = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) WHITE = (255, 255, 255) COLORS = (BLACK, RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE) BRIGHTNESS = 0.1 def setColor(color): r = int(color[0]*BRIGHTNESS) g = int(color[1]*BRIGHTNESS) b = int(color[2]*BRIGHTNESS) return (g<<16) + (r<<8) + b @rp2.asm_pio(set_init=rp2.PIO.OUT_LOW, out_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24) def ws2812(): # 1 step = 0.2us (clock frequency must be set to 5MHz) wrap_target() # 1 step at 0 (to wait for data in low state, reset code) out(x, 1) # start of the cycle # 2 step at 1 set(pins, 1) [1] # 2 step at x mov(pins, x) [1] # 1 step at 0 set(pins, 0) wrap() sm = rp2.StateMachine(0, ws2812, freq=5_000_000, set_base=machine.Pin(12), out_base=machine.Pin(12)) sm.active(1) ar = array.array("I", [0 for _ in range(NUM_LEDS)]) # Set all leds to green for i in range(NUM_LEDS): ar[i] = setColor(GREEN) sm.put(ar, 8) time.sleep_ms(50) # Rotate one red led cpt = 1 while True: ar[cpt-1] = setColor(GREEN) ar[cpt] = setColor(RED) sm.put(ar, 8) time.sleep_ms(50) cpt = (cpt+1)%NUM_LEDS
import array, time import rp2 import machine NUM_LEDS = 10 BLACK = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) WHITE = (255, 255, 255) COLORS = (BLACK, RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE) BRIGHTNESS = 0.1 def setColor(color): r = int(color[0]*BRIGHTNESS) g = int(color[1]*BRIGHTNESS) b = int(color[2]*BRIGHTNESS) return (g<<16) + (r<<8) + b @rp2.asm_pio(set_init=rp2.PIO.OUT_LOW, out_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24) def ws2812(): # 1 step = 0.2us (clock frequency must be set to 5MHz) wrap_target() # 1 step at 0 (to wait for data in low state, reset code) out(x, 1) # start of the cycle # 2 step at 1 set(pins, 1) [1] # 2 step at x mov(pins, x) [1] # 1 step at 0 set(pins, 0) wrap() sm = rp2.StateMachine(0, ws2812, freq=5_000_000, set_base=machine.Pin(12), out_base=machine.Pin(12)) sm.active(1) ar = array.array("I", [0 for _ in range(NUM_LEDS)]) # Set all leds to green for i in range(NUM_LEDS): ar[i] = setColor(GREEN) sm.put(ar, 8) time.sleep_ms(50) # Rotate one red led cpt = 1 while True: ar[cpt-1] = setColor(GREEN) ar[cpt] = setColor(RED) sm.put(ar, 8) time.sleep_ms(50) cpt = (cpt+1)%NUM_LEDS
import array, time import rp2 import machine NUM_LEDS = 10 BLACK = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 150, 0) GREEN = (0, 255, 0) CYAN = (0, 255, 255) BLUE = (0, 0, 255) PURPLE = (180, 0, 255) WHITE = (255, 255, 255) COLORS = (BLACK, RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE) BRIGHTNESS = 0.1 def setColor(color): r = int(color[0]*BRIGHTNESS) g = int(color[1]*BRIGHTNESS) b = int(color[2]*BRIGHTNESS) return (g<<16) + (r<<8) + b @rp2.asm_pio(set_init=rp2.PIO.OUT_LOW, out_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24) def ws2812(): # 1 step = 0.2us (clock frequency must be set to 5MHz) wrap_target() # 1 step at 0 (to wait for data in low state, reset code) out(x, 1) # start of the cycle # 2 step at 1 set(pins, 1) [1] # 2 step at x mov(pins, x) [1] # 1 step at 0 set(pins, 0) wrap() sm = rp2.StateMachine(0, ws2812, freq=5_000_000, set_base=machine.Pin(12), out_base=machine.Pin(12)) sm.active(1) ar = array.array("I", [0 for _ in range(NUM_LEDS)]) # Set all leds to green for i in range(NUM_LEDS): ar[i] = setColor(GREEN) sm.put(ar, 8) time.sleep_ms(50) # Rotate one red led cpt = 1 while True: ar[cpt-1] = setColor(GREEN) ar[cpt] = setColor(RED) sm.put(ar, 8) time.sleep_ms(50) cpt = (cpt+1)%NUM_LEDS
These are various methods for directly programming microcontrollers. They involve using specialized hardware interfaces to transfer compiled code into the microcontroller's memory.
Bootloader : small program stored memory that initialize the µC and load/rewrite code from a secondary storage device such as serial port (USB/UART), flash memory or EEPROM.
Hardcoded bootloaders like UF2 provide a convenient way to load firmware onto a microcontroller without requiring a separate programming tool. They are often used in development boards and single-board computers. (e.g. : XIAO2040)
Basic board for using Alex Tardov's Free DAP
Understanding the distinction between Arduino as a hardware platform, the Arduino libraries, and the Arduino IDE is essential for effective microcontroller programming with Arduino-compatible boards.
The Arduino Uno refers to a specific hardware board featuring an Atmel ATmega328P microcontroller. It is (was?) one of the most commonly used Arduino boards for prototyping and educational purposes.
Arduino provides a rich set of libraries that simplify common tasks such as reading analog inputs, controlling digital outputs, and communicating with peripherals like sensors and displays.
// // ring.t412.ino // // ATtiny412 ring oscillator test // // Neil Gershenfeld 11/13/20 // // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose, but must // acknowledge this project. Copyright is retained and // must be preserved. The work is provided as is; no // warranty is provided, and users accept all liability. // void setup() { CPU_CCP = CCP_IOREG_gc; // unprotect clock CLKCTRL.MCLKCTRLB = 0; // turn off prescalar (20 MHz) PORTA.DIRSET = PIN6_bm; // set output pin } void loop() { while(1) { // // VPORT: 1.808 MHz // 250 ns high (5 cycles), 300 ns low (6 cycles) // if (VPORTA.IN & PIN7_bm) VPORTA.OUT &= ~PIN6_bm; else VPORTA.OUT |= PIN6_bm; // // PORT: 1.056 MHz // /* if (PORTA.IN & PIN7_bm) PORTA.OUTCLR = PIN6_bm; else PORTA.OUTSET = PIN6_bm; */ // // digitalRead/Write: 0.331 MHz // //* if (digitalRead(1)) digitalWrite(0,LOW); else digitalWrite(0,HIGH); */ // // digitalReadFast/WriteFast: 1.808 MHz // /* if (digitalReadFast(1)) digitalWriteFast(0,LOW); else digitalWriteFast(0,HIGH); */ } }
// // ring.t412.ino // // ATtiny412 ring oscillator test // // Neil Gershenfeld 11/13/20 // // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose, but must // acknowledge this project. Copyright is retained and // must be preserved. The work is provided as is; no // warranty is provided, and users accept all liability. // void setup() { CPU_CCP = CCP_IOREG_gc; // unprotect clock CLKCTRL.MCLKCTRLB = 0; // turn off prescalar (20 MHz) PORTA.DIRSET = PIN6_bm; // set output pin } void loop() { while(1) { // // VPORT: 1.808 MHz // 250 ns high (5 cycles), 300 ns low (6 cycles) // if (VPORTA.IN & PIN7_bm) VPORTA.OUT &= ~PIN6_bm; else VPORTA.OUT |= PIN6_bm; // // PORT: 1.056 MHz // /* if (PORTA.IN & PIN7_bm) PORTA.OUTCLR = PIN6_bm; else PORTA.OUTSET = PIN6_bm; */ // // digitalRead/Write: 0.331 MHz // //* if (digitalRead(1)) digitalWrite(0,LOW); else digitalWrite(0,HIGH); */ // // digitalReadFast/WriteFast: 1.808 MHz // /* if (digitalReadFast(1)) digitalWriteFast(0,LOW); else digitalWriteFast(0,HIGH); */ } }
// // ring.t412.ino // // ATtiny412 ring oscillator test // // Neil Gershenfeld 11/13/20 // // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose, but must // acknowledge this project. Copyright is retained and // must be preserved. The work is provided as is; no // warranty is provided, and users accept all liability. // void setup() { CPU_CCP = CCP_IOREG_gc; // unprotect clock CLKCTRL.MCLKCTRLB = 0; // turn off prescalar (20 MHz) PORTA.DIRSET = PIN6_bm; // set output pin } void loop() { while(1) { // // VPORT: 1.808 MHz // 250 ns high (5 cycles), 300 ns low (6 cycles) // if (VPORTA.IN & PIN7_bm) VPORTA.OUT &= ~PIN6_bm; else VPORTA.OUT |= PIN6_bm; // // PORT: 1.056 MHz // /* if (PORTA.IN & PIN7_bm) PORTA.OUTCLR = PIN6_bm; else PORTA.OUTSET = PIN6_bm; */ // // digitalRead/Write: 0.331 MHz // //* if (digitalRead(1)) digitalWrite(0,LOW); else digitalWrite(0,HIGH); */ // // digitalReadFast/WriteFast: 1.808 MHz // /* if (digitalReadFast(1)) digitalWriteFast(0,LOW); else digitalWriteFast(0,HIGH); */ } }
The Arduino Integrated Development Environment (IDE) is a software tool used for writing, compiling, and uploading code to Arduino boards. It includes features like syntax highlighting, serial monitor, and library management.
Be aware :
Every programming language contains special section of "commented" code.
Selecting a microcontroller with appropriate processing power for your application can help conserve energy and reduce cost. Underclocking, or running the microcontroller at a lower frequency, may be beneficial for power-sensitive projects.
Underclocking may be as usefull as overclocking!
Breaking down your code into modular components enhances readability and maintainability. Organize your code into separate functions or modules based on their functionality to facilitate debugging and future updates.
Flat files are difficult to modify / maintain.
// Flat File: Single File ... void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(200); // Wait for 200ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(1000); // Wait for 1 second // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(400); // Wait for 400ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(2000); // Wait for 2 second } // Modular writing : void flipAndWait(uint8_t pin, unsigned long time){ digitalWrite(pin, !digitalRead(pin)); delay(time); } void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); //set LED high delay(200); // Wait for 1 second flipAndWait(LED_PIN, 1000); //flip and wait for 1sec flipAndWait(LED_PIN, 400); //flip and wait for 400ms flipAndWait(LED_PIN, 2000); //flip and wait for 2sec }
// Flat File: Single File ... void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(200); // Wait for 200ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(1000); // Wait for 1 second // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(400); // Wait for 400ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(2000); // Wait for 2 second } // Modular writing : void flipAndWait(uint8_t pin, unsigned long time){ digitalWrite(pin, !digitalRead(pin)); delay(time); } void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); //set LED high delay(200); // Wait for 1 second flipAndWait(LED_PIN, 1000); //flip and wait for 1sec flipAndWait(LED_PIN, 400); //flip and wait for 400ms flipAndWait(LED_PIN, 2000); //flip and wait for 2sec }
// Flat File: Single File ... void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(200); // Wait for 200ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(1000); // Wait for 1 second // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(400); // Wait for 400ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(2000); // Wait for 2 second } // Modular writing : void flipAndWait(uint8_t pin, unsigned long time){ digitalWrite(pin, !digitalRead(pin)); delay(time); } void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); //set LED high delay(200); // Wait for 1 second flipAndWait(LED_PIN, 1000); //flip and wait for 1sec flipAndWait(LED_PIN, 400); //flip and wait for 400ms flipAndWait(LED_PIN, 2000); //flip and wait for 2sec }
// Flat File: Single File ... void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(200); // Wait for 200ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(1000); // Wait for 1 second // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(400); // Wait for 400ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(2000); // Wait for 2 second } // Modular writing : void flipAndWait(uint8_t pin, unsigned long time){ digitalWrite(pin, !digitalRead(pin)); delay(time); } void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); //set LED high delay(200); // Wait for 1 second flipAndWait(LED_PIN, 1000); //flip and wait for 1sec flipAndWait(LED_PIN, 400); //flip and wait for 400ms flipAndWait(LED_PIN, 2000); //flip and wait for 2sec }
// Flat File: Single File ... void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(200); // Wait for 200ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(1000); // Wait for 1 second // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(400); // Wait for 400ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(2000); // Wait for 2 second } // Modular writing : void flipAndWait(uint8_t pin, unsigned long time){ digitalWrite(pin, !digitalRead(pin)); delay(time); } void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); //set LED high delay(200); // Wait for 1 second flipAndWait(LED_PIN, 1000); //flip and wait for 1sec flipAndWait(LED_PIN, 400); //flip and wait for 400ms flipAndWait(LED_PIN, 2000); //flip and wait for 2sec }
// Flat File: Single File ... void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(200); // Wait for 200ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(1000); // Wait for 1 second // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(400); // Wait for 400ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(2000); // Wait for 2 second } // Modular writing : void flipAndWait(uint8_t pin, unsigned long time){ digitalWrite(pin, !digitalRead(pin)); delay(time); } void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); //set LED high delay(200); // Wait for 1 second flipAndWait(LED_PIN, 1000); //flip and wait for 1sec flipAndWait(LED_PIN, 400); //flip and wait for 400ms flipAndWait(LED_PIN, 2000); //flip and wait for 2sec }
// Flat File: Single File ... void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(200); // Wait for 200ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(1000); // Wait for 1 second // Turn on the LED digitalWrite(LED_PIN, HIGH); delay(400); // Wait for 400ms // Turn off the LED digitalWrite(LED_PIN, LOW); delay(2000); // Wait for 2 second } // Modular writing : void flipAndWait(uint8_t pin, unsigned long time){ digitalWrite(pin, !digitalRead(pin)); delay(time); } void loop() { // Turn on the LED digitalWrite(LED_PIN, HIGH); //set LED high delay(200); // Wait for 1 second flipAndWait(LED_PIN, 1000); //flip and wait for 1sec flipAndWait(LED_PIN, 400); //flip and wait for 400ms flipAndWait(LED_PIN, 2000); //flip and wait for 2sec }
This improves code clarity and allows for easier modifications in the future.
// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin (led connected to pin7) as an output. pinMode(7, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level) delay(200); // flash for 200ms digitalWrite(7, LOW); // turn the LED off by making the voltage LOW delay(800); // wait for 800ms (total = ~1sec) }
// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin (led connected to pin7) as an output. pinMode(7, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level) delay(200); // flash for 200ms digitalWrite(7, LOW); // turn the LED off by making the voltage LOW delay(800); // wait for 800ms (total = ~1sec) }
// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin (led connected to pin7) as an output. pinMode(7, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level) delay(200); // flash for 200ms digitalWrite(7, LOW); // turn the LED off by making the voltage LOW delay(800); // wait for 800ms (total = ~1sec) }
#define LED 7 #define PERIOD 1000 #define FLASH_TIME 200 // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED as an output. pinMode(LED, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level) delay(FLASH_TIME); // wait for a ON_TIME digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW delay(PERIOD-FLASH_TIME); // wait to get a periodic time of ~PERIOD }
#define LED 7 #define PERIOD 1000 #define FLASH_TIME 200 // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED as an output. pinMode(LED, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level) delay(FLASH_TIME); // wait for a ON_TIME digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW delay(PERIOD-FLASH_TIME); // wait to get a periodic time of ~PERIOD }
#define LED 7 #define PERIOD 1000 #define FLASH_TIME 200 // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED as an output. pinMode(LED, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level) delay(FLASH_TIME); // wait for a ON_TIME digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW delay(PERIOD-FLASH_TIME); // wait to get a periodic time of ~PERIOD }
#define LED 7 #define PERIOD 1000 #define FLASH_TIME 200 // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED as an output. pinMode(LED, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level) delay(FLASH_TIME); // wait for a ON_TIME digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW delay(PERIOD-FLASH_TIME); // wait to get a periodic time of ~PERIOD }
Class:
// Definition of the LEDControl class class LEDControl { private: int pin; // Class variable to store the pin number public: // Constructor of the class to initialize the pin LEDControl(int pinNumber) { pin = pinNumber; pinMode(pin, OUTPUT); } // Method to turn on the LED void turnOn() { digitalWrite(pin, HIGH); } // Method to turn off the LED void turnOff() { digitalWrite(pin, LOW); } // Method to toggle the state of the LED void toggle() { digitalWrite(pin, !digitalRead(pin)); } // Method to flash the LED for a specified time void flash(int flashTime) { turnOn(); // Turn on the LED delay(flashTime); // Wait for the specified time turnOff(); // Turn off the LED } }; // Using the LEDControl class const int LED_PIN = 13; // LED pin number void setup() { // Create an instance of LEDControl with pin 13 LEDControl myLED(LED_PIN); } void loop() { // Turn on the LED for 1 second myLED.flash(200); delay(1000); }
// Definition of the LEDControl class class LEDControl { private: int pin; // Class variable to store the pin number public: // Constructor of the class to initialize the pin LEDControl(int pinNumber) { pin = pinNumber; pinMode(pin, OUTPUT); } // Method to turn on the LED void turnOn() { digitalWrite(pin, HIGH); } // Method to turn off the LED void turnOff() { digitalWrite(pin, LOW); } // Method to toggle the state of the LED void toggle() { digitalWrite(pin, !digitalRead(pin)); } // Method to flash the LED for a specified time void flash(int flashTime) { turnOn(); // Turn on the LED delay(flashTime); // Wait for the specified time turnOff(); // Turn off the LED } }; // Using the LEDControl class const int LED_PIN = 13; // LED pin number void setup() { // Create an instance of LEDControl with pin 13 LEDControl myLED(LED_PIN); } void loop() { // Turn on the LED for 1 second myLED.flash(200); delay(1000); }
// Definition of the LEDControl class class LEDControl { private: int pin; // Class variable to store the pin number public: // Constructor of the class to initialize the pin LEDControl(int pinNumber) { pin = pinNumber; pinMode(pin, OUTPUT); } // Method to turn on the LED void turnOn() { digitalWrite(pin, HIGH); } // Method to turn off the LED void turnOff() { digitalWrite(pin, LOW); } // Method to toggle the state of the LED void toggle() { digitalWrite(pin, !digitalRead(pin)); } // Method to flash the LED for a specified time void flash(int flashTime) { turnOn(); // Turn on the LED delay(flashTime); // Wait for the specified time turnOff(); // Turn off the LED } }; // Using the LEDControl class const int LED_PIN = 13; // LED pin number void setup() { // Create an instance of LEDControl with pin 13 LEDControl myLED(LED_PIN); } void loop() { // Turn on the LED for 1 second myLED.flash(200); delay(1000); }
// Definition of the LEDControl class class LEDControl { private: int pin; // Class variable to store the pin number public: // Constructor of the class to initialize the pin LEDControl(int pinNumber) { pin = pinNumber; pinMode(pin, OUTPUT); } // Method to turn on the LED void turnOn() { digitalWrite(pin, HIGH); } // Method to turn off the LED void turnOff() { digitalWrite(pin, LOW); } // Method to toggle the state of the LED void toggle() { digitalWrite(pin, !digitalRead(pin)); } // Method to flash the LED for a specified time void flash(int flashTime) { turnOn(); // Turn on the LED delay(flashTime); // Wait for the specified time turnOff(); // Turn off the LED } }; // Using the LEDControl class const int LED_PIN = 13; // LED pin number void setup() { // Create an instance of LEDControl with pin 13 LEDControl myLED(LED_PIN); } void loop() { // Turn on the LED for 1 second myLED.flash(200); delay(1000); }
Some tips :
Unified Program and Debug Interface
#define DEBUG_MODE 1 ... #if DEBUG_MODE Serial.print("DEBUG -- MyVal value = "); Serial.println(MyVal, DEC); #endif
#define DEBUG_MODE 1 ... #if DEBUG_MODE Serial.print("DEBUG -- MyVal value = "); Serial.println(MyVal, DEC); #endif