用户工具

站点工具


learing:examples:graph

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
learing:examples:graph [2017/10/05 03:38] 弘毅learing:examples:graph [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
 +====== Graph(成图) ======
 +
 +<WRAP left round info 65%>
 +这个例子使用控制器发送ADC采集后的电位器数据到电脑上,Processing使用这些数据进行绘图。
 +</WRAP>
 +
 +
 +<WRAP left round box 100%>
 +===== ALPHA MEGA328-U核心 =====
 +==== 硬件 ====
 +  * [[ocrobot:alpha:parallelexpansion:index|ALPHA 并行扩展板]]
 +  * [[ocrobot:alpha:mega328-u:main|ALPHA MEGA328-U]]
 +  * [[ocrobot:alpha:potentiometer:main|ALPHA 电位器模块]]
 +==== 搭建电路 ====
 +
 +  - ALPHA MEGA328-U模块插入并行扩展版1号槽位。
 +  - ALPHA 微动开关模块插入并行扩展板2号槽位。
 +  - USB线连接计算机与ALPHA MEGA328-U。
 +
 +==== 控制器代码 ====
 +<code cpp>
 +void setup() {
 +  // 初始化串口通讯
 +  Serial.begin(9600);
 +}
 +
 +void loop() {
 +  // 发送模拟值
 +  Serial.println(analogRead(A0));
 +  // 等待模数转换器稳定下来
 +  delay(2);
 +}
 +</code> 
 +==== Processing代码 ====
 +<code cpp>
 +
 +// 从串口接收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++;
 +    }
 +  }
 +}
 +</code>
 +</WRAP>
 +
 +<WRAP left round box 100%>
 +===== ALPHA 8F328D-U核心 =====
 +==== 硬件 ====
 +
 +==== 搭建电路 ====
 +
 +==== 代码 ====
 +</WRAP>
 +
 +<WRAP left round box 100%>
 +===== MangoII =====
 +
 +==== 硬件要求 ====
 +
 +|Arduino板|
 +|电位计|
 +
 +==== 软件要求 ====
 +
 +
 +Processing (1.5.1版)
 +
 +
 +
 +连接电位计到A0口
 +
 +
 +==== 代码 ====
 +
 +
 +<code cpp>/*
 +  Graph
 + */
 +
 +void setup() {
 +  // 初始化串口通讯
 +  Serial.begin(9600);
 +}
 +
 +void loop() {
 +  // 发送模拟值
 +  Serial.println(analogRead(A0));
 +  // 等待模数转换器稳定下来
 +  delay(2);
 +}
 +</code> 
 +Processing code for this example
 +<code cpp>
 + 
 +// 从串口接收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++;
 + }
 + }
 + }
 + 
 + 
 +</code>
 +
 +
 +Processing 
 +
 +
 +processing里可以看到传感器值的图表,改变输入值,可以看到成图变化。
 +</WRAP>