L.O. (Learning Objectives) Week 8:¶ ¶ • Identify relevant information in a microcontroller data sheet o Document what I learned from reading a microcontroller datasheet • Implement programming protocols • Program my board to do something o With as many different programming languages o And programming environments as possible 8.0. Notes during the class and workshops:FABLAB¶ Architectures: • There’s a whole universe – complexity, etc. – within an architecture. • John von Neumann • TinyFPGA o High performance C: • Very efficient • Other languages ultimately end up being written in C • Bottom level of programming lanague • GCC o Open compiler • AVR o If you’re using AVR, you’re going to use C o For Mac, you can use homebrew-avr (https://github.com/osx-cross/homebrew-avr) • “Toolchain” o Turns your code into a chain /hex file IDE: • It lets you write the code, run the code, and debug the code. • An Integrated Development Environment Arduino: • A standard board • An installation of a toolchain • When you say “Arduino”, you are actually calling 6 different things. • When you write Arduino, you’re writing in C (ultimately) • Using Arduino’s libraries • Cores: compiles program, upload program, etc. o Easiest way to set program up? Syntax • Really important with circuit boards and embedded programming 8.1. Tips on time management, project management, and learning:¶ Fab Lab Main Purpose: • Share knowledge/information o On personal website o Verbally o Group-chat platform e.g. KakaoTalk Doesn’t have to be synchronous communication (we don’t have to be in the same room for communicating) o Group work o Chatting in a meeting with classmates (e.g. Fab Family Meeting) • Share questions (distribution) o Take notes of questions o Effectively use certain periods of time good for asking questions (e.g. workshops) – Interrupt the instructor with questions during workshops and class time o Co-teaching o KakaoTalk o Time management Demand-side time management: e.g. to-do list Supply-side time management: e.g. Calendar Very important Understand + Execute this skill Schedule your time Getting the sense of time estimate of each task Spiral development (e.g. little bit of sketching, little bit of CAD, little bit of cutting little more bit of sketching, little more bit of CAD, …) • You need to keep moving. • Building confidence with this skill “Design sprints” Not a bunch of competitions Getting inspired and inspire Fab Lab keeps teaching this because it is a real robust way to solve problems applicable in every scale. Everything scales, especially in Fab Lab. • What is important is not really about the technical skills learned at Fab Lab, it is really about learning how to learn. • Why we are here and why we are trying to do • Q. how to have a reflection time after each week’s process 8.3. Program my board to do something:¶ Programming languages I decided to use: • Arduino (written in "Arduino") • C (written in "Visual Studio Code", with a built-in app called "Platform IO") note: Platform IO app is needed when uploading the C code to our ATtiny85 board or any other circuit board. What I made the code to do: 1. upload a program in C language to a board ATtiny85. 2. light on when pressed a button 3. light off when released the button note: I haven't added a button function in my C code yet. First, I played with Arduino. The process I went through: 1. Re-soldered my switch pin. 2. Wrote test codes to test if my switch works after resoldering. 3. Accidentally made a "lotto" code. 4. Edited it, by adding one more variable. note: Success! 1. Re-soldered my switch pin. When I first soldered my switch on my board, I rotated it 90 degress from which direction it should be placed. I assume this happened because I chose a different footprint when I designed my board on Kidcad. With help from Craig, I successfully used wires to reconnect the switch pin to my board. 2. Wrote test codes to see if my switch works after resoldering. After finishing soldering, I had to test if the switch would work properly this time. I wrote some code to show two different words on the screen -- "pond" and "SSSSSSSS" -- depending on whether or not the button is pressed and the current flows through it properly. video: Source code: < Test Code for Switch -- pressed? > #include SoftwareSerial mySerial(2, 1); // RX, TX //Constants int ledPin = 4; int buttonPin = 3; //Variables int buttonState; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); // set the data rate for the SoftwareSerial port mySerial.begin(4800); mySerial.println("Hello, world?"); digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); delay(1000); } void loop() { // run over and over //mySerial.write("something"); buttonState = digitalRead(buttonPin); if (mySerial.available()) { digitalWrite(ledPin, HIGH); } if (buttonState == LOW) { mySerial.write("pond"); } if (buttonState == HIGH) { mySerial.write("SSSSSSSSSSSSSSSSSS"); } } 3. Accidentally wrote a "lotto" code. When I press the button, no one can garentee if the LED will light up, stay on, stay off, or light off. video: Source code: < Lotto Code -- Random Light On Off > //Constants const int ledPin = 4; const int buttonPin = 3; //Variables boolean buttonState = false; boolean flag = false; #include SoftwareSerial mySerial(2, 1); // RX, TX void setup() { //Initialize serial communication at 9600 bits per second //Serial.begin(9600); //Input or output? pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); mySerial.begin(4800); mySerial.write("flag"); } void loop() { //Read button state (pressed or not pressed?) buttonState = digitalRead(buttonPin); boolean flag = digitalRead(buttonPin); if (buttonState == true && flag == true) { buttonState == false; } if (buttonState == false && flag == false) { buttonState == true; } //If button pressed... if (buttonState == HIGH) { //...once, turn led on if (flag == true){ digitalWrite(ledPin, HIGH); flag = 1; //change flag variable } else if (flag == false) { //...twice, turn led off digitalWrite(ledPin, LOW); flag = 0; //change flag variable again } } 4. Edited it, by adding one more variable. Source code: Your code title //Constants const int ledPin = 4; const int buttonPin = 3; //Variables boolean buttonState = false; boolean flag = false; boolean light = false; <-- newly added variable #include SoftwareSerial mySerial(2, 1); // RX, TX void setup() { //Initialize serial communication at 9600 bits per second //Serial.begin(9600); //Input or output? pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); mySerial.begin(4800); mySerial.write("flag"); } void loop() { //Read button state (pressed or not pressed?) buttonState = digitalRead(buttonPin); //boolean flag = digitalRead(buttonPin); digitalWrite(ledPin, light); <-- newly added line if (buttonState == HIGH && flag == false) { light = !light; flag = true; } if (buttonState == LOW && flag == true) { flag = false; } } After that, I wrote C code to do the same. The process that I went through: 1. Wrote C code to lights up the LED 2. Spent a day to figure out and understand how to upload C code to my board. 3. Compiled successfully, but there was an error when uploading the code to the board. 4. Got help from Craig. 5. Edited it, by changing the code in "platformio.ini". 6. Added a button function. (in progress at the moment) First, I wrote some code in C to if it would work and successfully upload to my board. Video: It lighted up successfully when the connectors were plugged to my laptop. Source code: Your code title /** * This code was written to: * 1. upload a program in C language * to a board ATtiny85. * 2. light on when pressed a button <-- not yet * 3. light off when released the button <-- not yet * **/ #include #include #include // ==================================== // ATtiny // 25/45/85 // +--------+ // BTN - PB3 --+ o Vcc +------------ // LED - PB4 --+ +-- // --+ +-- // ------------+ GND +-- // +--------+ // ==================================== // Define the I/O port to be used for the LED. // This a number between 0 and 7 that tells which bit to use. #define LED_PORT PB4 int main(void) { // Set the LED port number as output. // The DDRB is the data direction for port B. // This ... // - shifts the "1" on left to the desired position and ... // - does bitwise "OR" with the value in the port register. DDRB |= (1 << LED_PORT); // Start infinite loop. // (this is how most programs work) while (1) { // Set the LED bit to "1" - LED will be "on". PORTB |= (1 << LED_PORT); // Wait a little. // The delay function simply does N-number of "empty" loops. _delay_ms(1000); // Set the LED bit to "0" - LED will be "off". PORTB &= ~(1 << LED_PORT); // Wait a little. _delay_ms(1400); // Do it again ... } // Return the mandatory for the "main" function value. return (0); } Reference: I first wrote this code by reading through The Tinusaur Project 's website. I found the way they drew the PIN at the top of their code very helpful, and I plan to do it every time I code something for a microcontroller/circuit board. Then there was an error uploading the code to the board, even when it complied successfully. I asked Craig, and he helped me understand the error message and think of the right search keywords. • Search Keywords that I used: "platform io avr isp" • The webpage I found useful: Click this website. I copied and pasted the code under "AVRISP mkII" on this webpage, to the "platformio.ini" file opened using Visual Studio Code. Before this change, the "platformio.ini" file looked like this: (photo here) After this change: AVRISP mkII code added, and edited -> ; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html [env:attiny85] platform = atmelavr board = attiny85 framework = arduino upload_protocol = stk500v2 ; each flag in a new line upload_flags = -Pusb Then, I have been working on adding the button function to my code written above. Video: Editing in the progress. Source code: AVRISP mkII code added, and edited -> int l = 0; Credits: I found this webpage helpful. 8.4. The Fun Discoveries of The Week, && the Questions of the Week 8: ¶ 1. Fun Discoveries that I encountered throughout the week 8 are: • That you can code in C language, instead of Arduino language, in the Arduino application! • That you are safe to touch the board with 5V going through it, but we can change the design of the board when V gets bigger and the safety becomes a more serious topic. • That my skin reacts with allergetic condition after I solder a circuit board. And that I can use a mask in the lab that covers my whole face. 2. the Questions of the Week 8, I think, are: (note: More interesting questions seem to come up after a hectic, somewhat-unorganized process of experiments. Therefore, I have not researched on these questions written below yet, but I will because they are super duper intriguing! :) ) Q. What is the history behind the evolution of circuit board ever since it was invented/made? Q. Was there any other fuction/pin that we used before the idea of PWM was invented and introduced to the market/world? Q. How did the fab lab instructors managed their time and projects when they were fab lab students? Any tips? (note: the last question was asked to Craig a few days ago :) novertheless, I hope to ask other people who participated in fab academy before and the current students in 2020!) Group work: click here 8.2. Identify relevant information in a microcontroller data sheet:¶ Organized in a table: Definition ATtiny85 Pinout Diagram · A pinout is a reference to the pins or contacts that connect an electrical device or connector. It describes the functions of transmitted signals and the circuit input/output (I/O) requirements. · Each individual pin in a chip, connector or singular wire is defined in text, a table or a diagram. (shown below in a bigger size) Clock Speed · The number of pulses per second · Generated by an oscillator that sets the tempo for the processor. · Usually measured in MHz (megahertz, or millions of pulses per second). Max. 10 MHz Max. 20MHz Internal clock: 8.0 MHz +- 10% External clock: PWM (Pulse Width Modulation) · Used for controlling the amplitude of digital signals in order to control devices and applications requiring power or electricity. · It essentially controls the amount of power. · In the perspective of the voltage component, that is given to a device by cycling the on-and-off phases of a digital signal quickly and varying the width of the “on” phase or duty cycle. · A powerful benefit of PWM: power loss is very minimal. · Not every pin can do PWM · Find out which pin does that-> Pin 1, Pin 0 can do PWM. Absolute Maximum Ratings · Limiting values of operating and environmental conditions applicable to any electronic device of a specified type as defined by its published data · Should not be exceeded under the worst probable conditions. · These values are chosen by the device manufacturer to provide acceptable serviceability of the device · taking no responsibility for equipment variations, environmental variations, and the effects of changes in operating conditions due to variations in the characteristics of the device under consideration and of all other electronic devices in the equipment. · Stresses above those listed under AMR may cause permanent damage to the device. Runtime Runtime = Run time = Execution time · the time when the CPU is executing the machine code. Scratch: approx.. 2.1 seconds Arduino: C: (program size) C < C# << Scratch < Arduino C: 96 bytes Arduino: 684 bytes