Interface and application programming

In this week we have to make a program with GUI that can interface with input and/or output device that we made

Selecting the program and the board

To clear this assigment I need to choose two things. The first thing is the program I'm going to use to make the user interface and communicate with the board. The second thing is the board I'm going to control.

For the program I selected Processing.py. Becauase this will be a chance for me to learn about this program and also a chance to try programming with python. One more thing that made the decision easy for me is that we have a book named Make: Getting Started with Processing.py, I took look into it and I saw that it has many good examples that can guide me to make the program.

For the board I selected my output devices board, to contorl a DC motor. I already programmed it to receive serial data in the networking assigment to toggle the LED. But now I need to change the program a little bit to toggle the DC motor when the button is pressed in the program I'm going to make with processing.

Processing.py

The first step was deciding what I want the program to look like and what I want it to do. I wanted to make a program that can toggle on and toggle off the DC motor. The program will have one button, the button color is yellow, and the background color is blue. Toggle will be written in the button with red color, and the hole button will turn red when it is pressed.

I started learing Processing.py with the book I mentioned before Make: Getting Started with Processing.py. It is a very good book and I found almost everything I needed to make the program.

I opened the book's index and then I chose the examples I needed to understand to make my program:

I applied all these examples, and then I tried to implement them in my program.

Firstly, I made the background with the button and this is a picture and the program:





def setup():
    size(300, 120)
    strokeWeight(9)
    strokeJoin(ROUND)

def draw():
    background(0, 102, 204)
    stroke(102)
    rect(100, 30, 100, 60)
    fill(255, 204, 0)

Then, I added an if statement for the button if pressed, here is a picture and the program:



x= 100
y= 30
w= 100
h= 60

def setup():

    size(300, 120)
    strokeWeight(9)
    strokeJoin(ROUND)

def draw():
    background(0, 102, 204)
    stroke(102)
    rect(100, 30, 100, 60)
    fill(255, 0, 0)
    if mousePressed == True and mouseX > x and mouseX < x+w and mouseY > y and mouseY < y+h:
        rect(100, 30, 100, 60)
    fill(255, 204, 0)

Then, I added the text:



x= 100
y= 30
w= 100
h= 60
font = None

def setup():
    global font
    font = createFont("SourceCodePro-Regualr.ttf", 32)
    size(300, 120)
    strokeWeight(9)
    strokeJoin(ROUND)

def draw():
    background(0, 102, 204)
    stroke(102)
    rect(100, 30, 100, 60)
    fill(255, 0, 0)
    textSize(20)
    text("Toggle", 117, 65)
    if mousePressed == True and mouseX > x and mouseX < x+w and mouseY > y and mouseY < y+h:
        rect(100, 30, 100, 60)
    fill(255, 204, 0)

Finally, I just wanted to send the letter F when the button is pressed. In the book, there was an example of how to read from serial port, but not how to send. I followed the example and instead of writing port.read(), I wrote port.send() and here is the final code.

add_library('serial')
port = None
x= 100
y= 30
w= 100
h= 60
font = None

def setup():
    global font
    global port
    font = createFont("SourceCodePro-Regualr.ttf", 32)
    size(300, 120)
    strokeWeight(9)
    strokeJoin(ROUND)
    port = Serial(this, "/dev/ttyUSB0", 9600)

def draw():
    background(0, 102, 204)
    stroke(102)
    rect(100, 30, 100, 60)
    fill(255, 0, 0)
    textSize(20)
    text("Toggle", 117, 65)
    if mousePressed == True and mouseX > x and mouseX < x+w and mouseY > y and mouseY < y+h:
        rect(100, 30, 100, 60)
        port.write('F')
    fill( 255, 204, 0)

Board code

After finishing the program, I connected my output devices board to the FTDI. Then I uploaded the same program I made for the networking week. I was waiting the LED to turn on and off whenever I press the toggle button in the program I made.

I don't know what the reason is, but the button wasn't working. I tried many things to solve the problem but nothing worked, changing the FTDI, chaging the cable, switching Rx and Tx. I looked into the program many times, and I found no problem. With the same program I can toggle the LED button using the networking board that sends F. But when I connect the board to the computer directly it didn't work, even when I tried to send the character directly from the serial monitor in Arduino IDE.

Then, I tried to send the character to a board that I programmed while I was learning in the networking week, and it worked! So, I decided to for get the program I made in the networking week and start writing a new program to receive the code from the computer. I didn't know how this would solve the problem, but this was the only thing that I didn't try at that time.

So, I started writing the program. And as I did in the last week, I started with Neil's code hello.bus.45.c. Then, I started modifying it. I defined #define serial_pin_in (1 << PB0) for Rx, #define serial_pin_out (1 << PB1) for Tx, and changed #define led_pin (1 << PA2) because H-bridge In2 is connected to PA2.

Here is the main function:

int main(void) {

   static char chr;
   output(led_direction, led_pin);

   while (1) {

      get_char(&serial_pins, serial_pin_in, &chr);

      if (chr == node_id) {
           PORTA ^= (1 << PA2);
       }
    }
}

When I uploaded the program it worked!!! And here is the final result:

Files