极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12087|回复: 11

做《爱上arduino》书中互动云一章中互动灯遇到问题,求大神指点

[复制链接]
发表于 2013-4-8 23:54:18 | 显示全部楼层 |阅读模式
附上错误提示,这联网的模块有问题吗?、、、、

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-4-9 00:07:58 | 显示全部楼层
{:soso_e103:}贴完整代码出来看看
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-9 00:12:55 | 显示全部楼层
[pre lang="processing" line="1"]// Example 08A: Arduino networked lamp
// parts of the code are inspired
// by a blog post by Tod E. Kurt (todbot.com)
//
// Copy and paste this example into an empty Processing sketch

import processing.serial.*;

String feed = "http://blog.makezine.com/index.xml";

int interval = 10;  // retrieve feed every 60 seconds;
int lastTime;       // the last time we fetched the content

int love    = 0;
int peace   = 0;
int arduino = 0;

int light = 0;  // light level measured by the lamp

Serial port;
color c;
String cs;

String buffer = ""; // Accumulates characters coming from Arduino

PFont font;

void setup() {
  size(640,480);
  frameRate(10);    // we don't need fast updates

  font = loadFont("HelveticaNeue-Bold-32.vlw");  
  fill(255);  
  textFont(font, 32);
  // IMPORTANT NOTE:
  // The first serial port retrieved by Serial.list()
  // should be your Arduino. If not, uncomment the next
  // line by deleting the // before it, and re-run the
  // sketch to see a list of serial ports. Then, change
  // the 0 in between [ and ] to the number of the port
  // that your Arduino is connected to.
  println(Serial.list());
  String arduinoPort = Serial.list()[3];
  port = new Serial(this, arduinoPort, 9600); // connect to Arduino

  lastTime = 0;
  fetchData();
}

void draw() {
  background( c );
  int n = (interval - ((millis()-lastTime)/1000));

  // Build a colour based on the 3 values
  c = color(peace, love, arduino);
  cs = "#" + hex(c,6); // Prepare a string to be sent to Arduino

  text("Arduino Networked Lamp", 10,40);
  text("Reading feed:", 10, 100);
  text(feed, 10, 140);

  text("Next update in "+ n + " seconds",10,450);
  text("peace" ,10,200);
  text(" " + peace, 130, 200);
  rect(200,172, peace, 28);

  text("love ",10,240);
  text(" " + love, 130, 240);
  rect(200,212, love, 28);

  text("arduino ",10,280);
  text(" " + arduino, 130, 280);
  rect(200,252, arduino, 28);

  // write the colour string to the screen
  text("sending", 10, 340);
  text(cs, 200,340);

  text("light level", 10, 380);
  rect(200, 352,light/10.23,28); // this turns 1023 into 100

  if (n <= 0) {
    fetchData();
    lastTime = millis();
  }

  port.write(cs); // send data to Arduino

  if (port.available() > 0) { // check if there is data waiting
    int inByte = port.read(); // read one byte
    if (inByte != 10) { // if byte is not newline
      buffer = buffer + char(inByte); // just add it to the buffer
    }
    else {

      // newline reached, let's process the data
      if (buffer.length() > 1) { // make sure there is enough data

        // chop off the last character, it's a carriage return
        // (a carriage return is the character at the end of a
        // line of text)
        buffer = buffer.substring(0,buffer.length() -1);

        // turn the buffer from string into an integer number
        light = int(buffer);

        // clean the buffer for the next read cycle
        buffer = "";

        // We're likely falling behind in taking readings
        // from Arduino. So let's clear the backlog of
        // incoming sensor readings so the next reading is
        // up-to-date.
        port.clear();
      }
    }
  }

}

void fetchData() {
  // we use these strings to parse the feed
  String data;
  String chunk;

  // zero the counters
  love    = 0;
  peace   = 0;
  arduino = 0;
  try {
    URL url = new URL(feed);  // An object to represent the URL
    // prepare a connection   
    URLConnection conn = url.openConnection();
    conn.connect(); // now connect to the Website

    // this is a bit of virtual plumbing as we connect
    // the data coming from the connection to a buffered
    // reader that reads the data one line at a time.
    BufferedReader in = new
      BufferedReader(new InputStreamReader(conn.getInputStream()));

    // read each line from the feed
    while ((data = in.readLine()) != null) {

      StringTokenizer st =
        new StringTokenizer(data,"\"<>,.()[] ");// break it down
      while (st.hasMoreTokens()) {
        // each chunk of data is made lowercase
        chunk= st.nextToken().toLowerCase() ;

        if (chunk.indexOf("love") >= 0 ) // found "love"?
          love++;    // increment love by 1
        if (chunk.indexOf("peace") >= 0)   // found "peace"?
          peace++;   // increment peace by 1
        if (chunk.indexOf("arduino") >= 0) // found "arduino"?
          arduino++; // increment arduino by 1
      }
    }

    // Set 64 to be the maximum number of references we care about.
    if (peace > 64)   peace = 64;
    if (love > 64)    love = 64;
    if (arduino > 64) arduino = 64;

    peace = peace * 4;     // multiply by 4 so that the max is 255,
    love = love * 4;       // which comes in handy when building a
    arduino = arduino * 4; // colour that is made of 4 bytes (ARGB)
  }
  catch (Exception ex) { // If there was an error, stop the sketch
    ex.printStackTrace();
    System.out.println("ERROR: "+ex.getMessage());
  }

}[/code]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-9 00:17:31 | 显示全部楼层
是processing和arduino互动的,看不明白、、、还有arduino的代码:
  1. // Example 08B: Arduino Networked Lamp
  2. //
  3. // Copy and paste this example into an empty Arduino sketch

  4. #define SENSOR 0   
  5. #define R_LED 9
  6. #define G_LED 10
  7. #define B_LED 11
  8. #define BUTTON 12

  9. int val = 0; // variable to store the value coming from the sensor

  10. int btn = LOW;
  11. int old_btn = LOW;
  12. int state = 0;
  13. char buffer[7] ;
  14. int pointer = 0;
  15. byte inByte = 0;

  16. byte r = 0;
  17. byte g = 0;
  18. byte b = 0;

  19. void setup() {
  20.   Serial.begin(9600);  // open the serial port
  21.   pinMode(BUTTON, INPUT);
  22. }

  23. void loop() {
  24.   val = analogRead(SENSOR); // read the value from the sensor
  25.   Serial.println(val);      // print the value to
  26.                             // the serial port

  27.   if (Serial.available() >0) {

  28.     // read the incoming byte:
  29.     inByte = Serial.read();

  30.     // If the marker's found, next 6 characters are the colour
  31.     if (inByte == '#') {

  32.       while (pointer < 6) { // accumulate 6 chars
  33.         buffer[pointer] = Serial.read(); // store in the buffer
  34.         pointer++; // move the pointer forward by 1
  35.       }

  36.       // now we have the 3 numbers stored as hex numbers
  37.       // we need to decode them into 3 bytes r, g and b
  38.       r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
  39.       g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
  40.       b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;
  41.       
  42.       pointer = 0; // reset the pointer so we can reuse the buffer
  43.      
  44.     }
  45.   }   

  46.   btn = digitalRead(BUTTON); // read input value and store it

  47.   // Check if there was a transition
  48.   if ((btn == HIGH) && (old_btn == LOW)){
  49.     state = 1 - state;
  50.   }

  51.   old_btn = btn; // val is now old, let's store it

  52.   if (state == 1) { // if the lamp is on

  53.     analogWrite(R_LED, r);  // turn the leds on
  54.     analogWrite(G_LED, g);  // at the colour
  55.     analogWrite(B_LED, b);  // sent by the computer
  56.   } else {

  57.     analogWrite(R_LED, 0);  // otherwise turn off
  58.     analogWrite(G_LED, 0);
  59.     analogWrite(B_LED, 0);
  60.    }

  61.   delay(100);                // wait 100ms between each send
  62. }

  63. int hex2dec(byte c) { // converts one HEX character into a number
  64.     if (c >= '0' && c <= '9') {
  65.       return c - '0';
  66.     } else if (c >= 'A' && c <= 'F') {
  67.       return c - 'A' + 10;
  68.     }
  69. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2013-4-9 10:55:28 | 显示全部楼层
试了一下。。。arduino的代码可以编译。。没问题,processing的代码也没问题可以编译。。只是需要在目录中放入那个字体。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-10 11:53:29 | 显示全部楼层
那个字体我从网上下了,也在Tool里设置好就行了吗?
回复 支持 反对

使用道具 举报

发表于 2013-4-10 16:02:38 | 显示全部楼层
本帖最后由 empc 于 2013-4-10 16:03 编辑

processing 中要显示汉字,就需要添加中文字体文件,在tools菜单中的相关创建字体的命令选项,一般就创建自己电脑里有的宋体或黑体就可以了。 而且所生成的字体文件会自动放到 源文件下的 data  目录中,再源码中所用的字体改为自己创建的字体VLW文件名就可以了。

前面第一张图的错误提示,可能是需要 导入所需的URL库
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-10 18:08:34 | 显示全部楼层
URL库怎么导入?还有怎么下载到arduino啊?
回复 支持 反对

使用道具 举报

发表于 2013-4-10 19:50:05 | 显示全部楼层
processing 可以用java的类库

import java.net.URL;

import java.net.*;
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-4-10 22:41:53 | 显示全部楼层
、、、怎么用?
回复 支持 反对

使用道具 举报

发表于 2013-4-23 20:54:42 | 显示全部楼层
嘿嘿嘿。。{:soso_e112:}
回复 支持 反对

使用道具 举报

发表于 2014-3-30 20:28:26 | 显示全部楼层
同问  ;

代码与上面一致

出现了         BufferedReader(new InputStreamReader(conn.getInputStream()));

cannot  find a class or type named "InputSteamReader"
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-5 16:56 , Processed in 0.049432 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表