Yunjo's notes    
      Classes    
          Home About Final project Various notes  
      Week 12: Interface and Application Programming    
    • Learning Objectives (Week 12):
    • 12.0. Notes during the class and workshops:
    • 12.1. This week, I've been up to:
    • 12.2. Process of writing an application that interfaces a user with an output device that I made
    • 12.3. Explanation of how the UI that I made works, and how I did it
    • 12.4. Creating and breaking problems ~~ and how I fixed them
    • 12.5. Source code && Hero shot/video
    • 12.6. Week 12 Files
    • 12.7. The Fun Discoveries of The Week, && the Questions of the Week 9
    • 12.8. Hi Neil -- I watched Neil's talks and interviews
    • L.O. (Learning Objectives) Week 12:¶

      ¶

          
            •	Document while learning from writing an application that interfaces a user with an output device that I made
            •	Explain the UI that I made and how I did it
            •	Create and break problems ~~ and how I fixed them
            •	Journal the Fun Discoveries of The Week, && the Questions of the Week 12
            •	Be myself.
          
            
        

      12.0. Notes during the class and workshops:FABLAB¶

       
              •	IoT-related device interfaces:
                  o	MQTT, XMPP, IFTTT, UPnP, Wemo
              •	Relationship between IoT and UI(User Interface)
                  o	IoT: distributed sensors
                  o	UI: for display
              •	Data visualization vs. UI
                  o	Data visualization: one-way communication
                  o	UI: mutual, both way communication
                        	There should be 
              •	WebGL vs. Three.js ?
              •	The difference is “optimization”
              •	Options for the week:
                  o	App inventor
                        	E.g. heart rate 
                  o	Node-RED
                        	E.g. 
                  o	Python
                        	TKinter library
                  o	Processing
              
              •	Bantam-related tips
                  o	Clearance 0.42 – 1.2
                  o	Trace width 0.4 – 1.2
                        	Fatter traces are useful when you need more power(P)
              •	E.g. for LED, Motor, Heaters
              •	Pinouts
                  o	Search word: “Minimal circuit”
                  o	ISP
                        	Is used for 44, 45, 85, 128, 328, 2506, mega
                  o	Jtag
                        	SAMD
                  o	UPDI
                        	412, 1614, 3216
                  o	FTDI
                        	TX, RX communication (USB -> Serial)
              •	Capacitor(s) is always for one particular component(chip)
                   o	Therefore, it is important/better to put a capacitor right next to the particular component that the capacitor supports/is for. 
              
            
        

      12.1. This week, I've been up to:¶

      There are three things that I have been working on:
      (In chronological order)
      1. getting help for soldering my speaker board (My skin shows alergetic reation when/after soldering)


      2. getting ready with an oven to use "Reflow Method" for soldering



      3. setting up an serial communication in my interface (Python used)




      Group Work:
      wk12_group_work_webpage

      12.2. Process of writing an application that interfaces a user with an output device that I made:¶

      At first, I tried NODE-RED with a LED-and-Switch board that I made:

      (This photo arduino reference related to serial communication.)
      After using NODE-RED during the workshop, I thought it would work okay with my output device board (speaker board) and my final project.
      So I took a few steps to understand how node-red works (a.k.a. "Stack Development"). And this is the result (mix of my handwriting and Craig's):

      (Useful tutorial for getting started with NODE-RED)
      (How to run NODE-RED locally)
      Then I realized I should use NODE-RED after setting up a workflow for "Reflow Method" to re-solder my heartbeat sensor board (input device board).
      To me, NODE-RED seemed to be more interesting with input devices.
      I also thought of using Processing, but I have already used Processing for UI-related stuff before.
      (Processing Reference Page)

      Therefore, I switched to Python.
      I found this tutorial helpful to get started with GUI in Python.
      After installing pip3 and pyserial libraries, a practice code that I wrote worked.

      Then I wrote this code, shown below, to make "Play" and "Stop" buttons in my interface for the speaker board.

      **Trouble-shooting here -- which i will talk through in 12.4 part of this webpage.



      12.3. Explanation of how the UI that I made works, and how I did it:¶

      12.4. Creating and breaking problems ~~ and how I fixed them:¶

      Problem 1 (Serial communication across Python, Arduino, and the boards)
      The buttons worked:


      However, their output did not connect with the board well.
      It seemed to send something as there's a blink on the UNO board.

      Then I tested on a new, plain? UNO board with nothing attached to it.

      I modified the example code found in the Arduino platform. ("communication >> Serial Event")
      This example code worked, and I used this understanding to add this part:

      to the Speaker_Code.
      It seemed to send something as the speaker makes a noise right after I upload the code, but it didn't seem to connect with the python interface.

      Later, I discovered that I should not use "Digital Write" because I'm doing serial communication using python for which it is better to use hardware pins, not software pins. (I forgot the detailed reason.)

      Problem 1 -- Solution:

      I decided to call a function rather than using digital-write.


      Problem 2:
      There were no errors in the Arduino code, but the speaker kept making a weird noise that may be caused by loose wires.



      Problem 2 -- Solution:
      And in the process, I asked a few questions and learned these things:


      Then I realized I forgot to plug in TX and RX of my Speaker_Board to the arduino UNO that I used.
      So I correctly pluged them in to the right pin numbers.


      Problem 3:
      The speaker kept making the same noise. (The Speaker_Board works when I upload the version of the code that does not communicates with the interface python code.)


      Problem 3 -- Solution:
      I changed a separte function in Arduino and made the loop section empty.
      It made the overall arduino code more organized and easier to understand.
      And I hoped it would solve the half-unknown-half-known issue that I have been facing.



      Problem 4:
      I had to check in detail whether or not all the data types used both in python and arduino are the same (e.g. byte, string, or character).


      Problem 4 -- Solution:
      Therefore I looked into the main parts that I had to check more carefully.
      And I re-read the datatype parts in the arduino reference page.




      And now I am currently working on "TYPE CASTING".
      I'm currently trying character, but might change to integer later depending on the results of experiments.

      12.5. Source code && Hero shot/video:¶

      Python working -- Video:


      Python Code:

       
            
      from tkinter import * import serial WINDOW = 800 # window size eps = 0.5 # filter time constant port = '/dev/cu.usbmodem141201' baud = 9600 # check command line arguments #if (len(sys.argv) != 2): # print("command line: hello.load.45.py serial_port") # sys.exit() #port = sys.argv[1] # open serial port ser = serial.Serial(port, baud) ser.setDTR() # define functions def myPlay(): ser.write(b'1') myLabel = Label(root, text="Music is playing...") myLabel.pack() def myStop(): ser.write(b'0') myLabel = Label(root, text="No more music; silence!") myLabel.pack() root = Tk() #Button = tkinter.Button #text = tkinter.Text Play = Button(root, text="PLAY", command=myPlay, height=7, width=11) Play.pack(side=LEFT) Stop = Button(root, text="STOP", command=myStop, height=7, width=11) Stop.pack(side=RIGHT) # set up GUI #root = Tk() #root.title('listen.to.my.heartbeat.py (q to exit)') #root.bind ('q', 'exit') #canvas = Canvas(root, width=WINDOW, heigh=.75*WINDOW, background='magenta') # start idle loop #root.after(100, idle, root, canvas) #Play.pack(side=tkinter.LEFT) #Stop.pack(side=tkinter.RIGHT) root.mainloop()
      Arduino code - for the python interface code:
       
            
      int speakerPin = 0; int sLength = 15; // the number of notes char notes[] = "eeeeeeegcde fffffeeeeddedg eeeeeeegcde fffffeeeggfdc "; // a space represents a rest int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4 }; int tempo = 300; void setup() { // initialize serial: Serial.begin(9600); // reserve 200 bytes for the input String: //inputString.reserve(200); pinMode(speakerPin, OUTPUT); } void playSong(/*int note, int tone, int duration*/) { for (int i = 0; i < sLength; i++) { if (notes[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } } void playTone(int pTone, int duration) { for (long i = 0; i < duration * 1000L; i += pTone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(pTone); digitalWrite(speakerPin, LOW); delayMicroseconds(pTone); } } void serialEvent() { while (Serial.available()) { byte inByte = (byte)Serial.read(); if (inByte == 1) { //Serial.print("1"); //digitalWrite(speakerPin, HIGH); Serial.println("PlayingSong"); playSong(); } else if (inByte == '0') { //Serial.print("0"); //digitalWrite(speakerPin, LOW); } } } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void loop() { playSong(); }
      Twinkle Little Star (not for interface-related usage):
       
            
      char notes[] = "ccggaagffeeddc "; // a space represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };



      Jingle Bell (not for interface-related usage):
       
            
      int speakerPin = 0; int length = 15; // the number of notes char notes[] = "eeeeeeegcdefffffeeeeddedgeeeeeeegcdefffffeeeggfdc "; // a space represents a rest int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4 }; int tempo = 300; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void setup() { pinMode(speakerPin, OUTPUT); } void loop() { for (int i = 0; i < length; i++) { if (notes[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } }

      Node-RED Screenshot:


      Node-RED Working -- Video:

      12.6. Week 12 Files:¶



      12.7. The Fun Discoveries of The Week, && the Questions of the Week 11:¶

       
            
      1.

      12.8. Hi Neil -- I watched Neil's talks and interviews:¶

      Video 1:


      Video 2:

      Video 3:


Creative Commons Attribution Non Commercial
powered by MkDocs and Material for MkDocs