960 likes | 973 Views
Arduino 201 Class. For those who want to learn more about the Arduino. Version 2.0. About Me. Objectives. WARNING: WORK IN PROGRESS. WHAT IS AN ARDUINO COMPATIBLE?. Arduino Compatibility. Espressif ESP32/ESP8266. https://www.espressif.com/en/products/hardware
E N D
Arduino 201 Class For those who want to learn more about the Arduino Version 2.0
WHAT IS AN ARDUINO COMPATIBLE?
Espressif ESP32/ESP8266 https://www.espressif.com/en/products/hardware https://www.espressif.com/en/products/hardware/esp32/overview https://www.espressif.com/en/products/hardware/esp8266ex/overview
Circuit Playground Express https://www.adafruit.com/product/3333 https://learn.adafruit.com/makecode
Wearables Gemma Flora
More Mega Than Mega Adafruit Grand Central STM32 There are quite a few boards out there that are faster, have more I/O, more math capabilities, more power capabilities, more rugged, etc than the Arduinos. The STM32 line has many options and form factors, and are designed more for real production / commercial usage. The Grand Central has a 120MHz Cortex M4 with floating point support, more ADCs, many more PWM outputs. Both have built in AES256 encryption engines and real random number generators https://www.st.com/en/microcontrollers-microprocessors/stm32-32-bit-arm-cortex-mcus.html https://www.adafruit.com/product/4064
OBJECT ORIENTED PROGRAMMING
Example: LED dimming class // Declaration class DimmableLed { // Access specifier private: int pin; int currentLevel; void updateLed(); public: const int minLevel = 0; const int maxLevel = 255; Led(int pwmPin); void on(); void off(); void setLevel(int level); int getLevel(); }; // Constructor DimmableLed::Led(int pwmPin) { pin = pwmPin; currentLevel = 0; } void DimmableLed::updateLed() { Serial.print("LED updated to "); Serial.println(currentLevel); analogWrite(pin, currentLevel); } void DimmableLed::on() { currentLevel = maxLevel; updateLed(); } void DimmableLed::off() { currentLevel = minLevel; updateLed(); } void DimmableLed::setLevel(int level) { currentLevel = level; updateLed(); } int DimmableLed::getLevel() { return currentLevel; }
Using DimmableLed // Global variables const int PWMPIN=9; DimmableLed myLed(PWMPIN); void setup() { } void loop() { myLed.on(); delay(100); myLed.off(); delay(100); for(int x = myLed.minLevel; x <= myLed.maxLevel; x+=10) { myLed.setLevel(x); delay(100); } myLed.off(); }
DEVICES AND COMMUNICATION
Popular Libraries https://www.arduino.cc/en/reference/libraries
Creating your own library https://www.arduino.cc/en/Hacking/libraryTutorial https://github.com/arduino/Arduino/wiki/Library-Manager-FAQ https://www.youtube.com/watch?v=fE3Dw0slhIc
OPTIONAL: Use a library? We can optionally write a program demonstrating usage of an existing library to output some content, or go on to the next topic, multitasking https://github.com/contrem/arduino-timer https://github.com/arduino-libraries/Arduino_JSON/ https://github.com/thijse/Arduino-Log/ https://github.com/mmurdoch/arduinounit
“Multitasking” With a single processor with a single core, only one thing can truly be running at a time, but we can task switch to get what we want done. There are several ways of doing this. OPTIONAL: This topic has a lot of slides, and optionally we can go into them in detail or go over them quickly
Doing Multiple Things, Done Wrong void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } This is the standard Arduino “Blink” example. It’s fine for beginners, but… delay() is a blocking function. Blocking functions prevent a program from doing anything else until that particular task has completed. If you need multiple tasks to occur at the same time, you simply cannot use delay(). https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/
Multitasking with functions in loop() The most straightforward way to multitask is to have checks for what “threads” need attention in loop and process them with each iteration. It’s best to break each piece into a separate function to make the code easier to read and maintain. void loop() { if(checkInputPins()) { handleInputPins(); } if(checkBluetooth()) { handleBluetooth(); } updateProgress(); if(checkLEDStatus()){ updateLEDs(); } }
Level Up: Multitasking with OO Rather than using functions, define an update() method for your classes and call them in turn Void loop() { // We have an array that holds all of our input pin objects. // Iterate through them and let them update for(int x=0; x<MAX_INPUT_PINS; X++) { inputPins[x].update(); } bluetoothHandler.update(); progressBar.update(); // We also have an array of objects controlling LEDs // Iterate through them and let them update for(int x=0; x<MAX_LEDS; X++) { leds[x].update(); }
Multitasking Using Time Periods With this technique, you keep track of the last time you did something, and in each loop iteration you check if it’s time to do it again (hint: This can be incorporated into the previous two ways too). This is a very efficient technique because very little time and memory is spent figuring out whether it’s time to update something or not. # Global variables unsigned long previousMillisInputPins = 0; unsigned long previousMillisProgress = 0; unsigned long currentMillis; … void loop() { currentMillis = millis(); if(currentMillis - previousMillisInputPins >= INPUT_FREQ_MILLIS) { handleInputPins(); previousMillisInputPins = currentMillis; } if(currentMillis - previousMillisProgress >= PROGRESS_FREQ_MILLIS) { updateProgress(); previousMillisProgress = currentMillis; } }
Multitasking Using Interrupts https://learn.adafruit.com/multi-tasking-the-arduino-part-2/what-is-an-interrupt
External Input Interrupts https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
Doing Small Chunks Of Your Task https://learn.adafruit.com/multi-tasking-the-arduino-part-3/deconstructing-the-loop
Fading An LED With Interrupts void updateLED(unsigned long thisMillis) { // is it time to update yet? if not, nothing happens if (thisMillis - previousFadeMillis >= fadeInterval) { if (fadeDirection == UP) { fadeValue = fadeValue + fadeIncrement; if (fadeValue >= maxPWM) { fadeValue = maxPWM; fadeDirection = DOWN; } } else { //if we aren't going up, we're going down fadeValue = fadeValue - fadeIncrement; if (fadeValue <= minPWM) { fadeValue = minPWM; fadeDirection = UP; } } analogWrite(pwmLED, fadeValue); previousFadeMillis = thisMillis; } } https://www.baldengineer.com/fading-led-analogwrite-millis-example.html
More Reliable Communication http://mqtt.org/https://io.adafruit.com/https://cloud.google.com/solutions/iot/ https://docs.aws.amazon.com/iot/https://mosquitto.org/
Other Development Environments
Visual Programming Without Code https://learn.sparkfun.com/tutorials/alternative-arduino-interfaces/all https://www.microsoft.com/en-us/makecode
Alternate IDEs There are a few but for me Sloeber plugin for Eclipse the best