用户工具

站点工具


learing:examples:graph

Graph(成图)

这个例子使用控制器发送ADC采集后的电位器数据到电脑上,Processing使用这些数据进行绘图。

ALPHA MEGA328-U核心

硬件

搭建电路

  1. ALPHA MEGA328-U模块插入并行扩展版1号槽位。
  2. ALPHA 微动开关模块插入并行扩展板2号槽位。
  3. USB线连接计算机与ALPHA MEGA328-U。

控制器代码

void setup() {
  // 初始化串口通讯
  Serial.begin(9600);
}
 
void loop() {
  // 发送模拟值
  Serial.println(analogRead(A0));
  // 等待模数转换器稳定下来
  delay(2);
}

Processing代码

// 从串口接收ascii编码的字符串,并且成图。这些值得范围是0-1023,值后面是换行符或者换行回车。
 
import processing.serial.*;
 
Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph
 
void setup () {
  // 设置窗口大小
  size(400, 300);        
 
  // 列出可用串口
  println(Serial.list());
  // 选择你使用的串口,这里我的是 Serial.list()[0]
  myPort = new Serial(this, Serial.list()[0], 9600);
  // 收到换行符之后产生串口事件
  myPort.bufferUntil('\n');
  // 初始化set inital background:
  background(0);
}
void draw () {
  //serialEvent()
}
 
void serialEvent (Serial myPort) {
  // 得到acsii字符
  String inString = myPort.readStringUntil('\n');
 
  if (inString != null) {
    // 去除空格
    inString = trim(inString);
    // 转换成整形并绘图
    float inByte = float(inString); 
    inByte = map(inByte, 0, 1023, 0, height);
 
    // 画线
    stroke(127, 34, 255);
    line(xPos, height, xPos, height - inByte);
 
    // 到屏幕边缘之后回到起始点
    if (xPos >= width) {
      xPos = 0;
      background(0);
    } 
    else {
      // 增加水平值
      xPos++;
    }
  }
}

ALPHA 8F328D-U核心

硬件

搭建电路

代码

MangoII

硬件要求

Arduino板
电位计

软件要求

Processing (1.5.1版)

连接电位计到A0口

代码

/*
  Graph
 */
 
void setup() {
  // 初始化串口通讯
  Serial.begin(9600);
}
 
void loop() {
  // 发送模拟值
  Serial.println(analogRead(A0));
  // 等待模数转换器稳定下来
  delay(2);
}

Processing code for this example

// 从串口接收ascii编码的字符串,并且成图。这些值得范围是0-1023,值后面是换行符或者换行回车。
 
 import processing.serial.*;
 
 Serial myPort;        // The serial port
 int xPos = 1;         // horizontal position of the graph
 
 void setup () {
 // 设置窗口大小
 size(400, 300);        
 
 // 列出可用串口
 println(Serial.list());
 // 选择你使用的串口,这里我的是 Serial.list()[0]
 myPort = new Serial(this, Serial.list()[0], 9600);
 // 收到换行符之后产生串口事件
 myPort.bufferUntil('\n');
 // 初始化set inital background:
 background(0);
 }
 void draw () {
 //serialEvent()
 }
 
 void serialEvent (Serial myPort) {
 // 得到acsii字符
 String inString = myPort.readStringUntil('\n');
 
 if (inString != null) {
 // 去除空格
 inString = trim(inString);
 // 转换成整形并绘图
 float inByte = float(inString); 
 inByte = map(inByte, 0, 1023, 0, height);
 
 // 画线
 stroke(127,34,255);
 line(xPos, height, xPos, height - inByte);
 
 // 到屏幕边缘之后回到起始点
 if (xPos >= width) {
 xPos = 0;
 background(0); 
 } 
 else {
 // 增加水平值
 xPos++;
 }
 }
 }
 
 

Processing

processing里可以看到传感器值的图表,改变输入值,可以看到成图变化。

learing/examples/graph.txt · 最后更改: 2023/06/07 04:23 由 127.0.0.1