[FabNotes33] 作业16:界面和应用编程 | Week 16 - Interface & Application Programming

[FabNotes33] 作业16:界面和应用编程 | Week 16 - Interface & Application Programming_第1张图片

这周的作业是

为输入/输出设备写一个应用程序

为之前做的这个检测光线的板子做个小应用好了。

[FabNotes33] 作业16:界面和应用编程 | Week 16 - Interface & Application Programming_第2张图片

先试着让板子跟串口通讯,看看元件是不是正常传送检测值。

#include "SoftwareSerial.h"
 
const int analogInPin =  A3;  // Analog input pin that the potentiometer is attached to
int sensorValue = 0;        // value read from the pot
 
const int Rx = 2; // this is physical pin 7
const int Tx = 7; // this is physical pin 6
SoftwareSerial mySerial(Rx, Tx);
 
void setup() {
  pinMode(Rx, INPUT);
  pinMode(Tx, OUTPUT);
  mySerial.begin(9600); // send serial data at 9600 bits/sec
}

void loop(){
  sensorValue = analogRead(analogInPin);
  mySerial.println(sensorValue);
  delay(500);
}
[FabNotes33] 作业16:界面和应用编程 | Week 16 - Interface & Application Programming_第3张图片

光电晶体管工作正常。

Video

下一步,在 processing 里面写界面的程序。当检测到光线变暗时,界面提示 “-_- It's dark!” ,如果光线变亮,提示 “it's getting light ”:


[FabNotes33] 作业16:界面和应用编程 | Week 16 - Interface & Application Programming_第4张图片

The code:

import processing.serial.*;
Serial myPort;
int lf = 10;
 
void setup(){
  size(300,300);
  myPort = new Serial(this,"/dev/tty.usbserial-A400gwhT",9600);
  textAlign(CENTER, CENTER);
  fill(255);
  textSize(20);
}
 
void draw(){
  while(myPort.available() > 0){
    String str = myPort.readStringUntil(lf);

    if(str!=null){
      int value = Integer.parseInt(trim(str));
 
       if (value >900) {
        print("it's dark ");
        println(value);
        background(0,0,0);
        text("-_- It's dark! ",150,120);
        text(value,150,200);
       } 
       else{
        print("it's getting light ");
        println(value);
        background(204,153,0);
        text(":P It's getting light ",150,120);
        text(value,150,200);
       }
    }
  }
}

Video

课程资源

  • Fab Academy 2016 课表
  • Week16 课程视频
  • Week16 课程大纲
  • 我的英文笔记 Gitbook
  • 公众号文章列表

你可能感兴趣的:([FabNotes33] 作业16:界面和应用编程 | Week 16 - Interface & Application Programming)