Arduino&处理3:“调光器”内置示例

时间:2016-06-28 01:52:54

标签: arduino processing arduino-uno led

好的,所以我在Windows 10 PC上使用Arduino Uno,以及处理3.我试图按照Arduino网站上的指示,通过在PC上移动来控制LED的亮度屏幕,通过Arduino软件与处理合作。 (这里是我正在谈论的项目的链接:https://www.arduino.cc/en/Tutorial/Dimmer)看起来很简单,但它不太合适。 LED会发光,但其亮度不会随鼠标的X位置而变化,并且其发光会闪烁。我没有错误代码。

关于电路,我有一个连接到PWM引脚9的LED,一个200欧姆的电阻接地;我确定LED的极性已正确设置。该网站目前尚不清楚处理与处理的完整程度。 Arduino软件应该合作以实现这一目标。 (如果可能的话,我将非常感谢其解释。)Arduino&处理代码如下。我不明白我做错了什么?

这是我的Arduino代码:

 
const int ledPin = 9;      // the pin that the LED is attached to

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  byte brightness;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
  }
}

这是我的处理代码:

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;

void setup() {
  size(256, 150);

  arduino = new Arduino(this, "COM3", 9600);

}

void draw() {
  background(constrain(mouseX, 0, 255));

  arduino.analogWrite(9, constrain(mouseX, 0, 255)); // 

}

1 个答案:

答案 0 :(得分:1)

对于那些想知道的人,这是我的功能代码:

Arduino部分:

const int ledPin = 9;      // the pin that the LED is attached to

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int brightness;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(9, brightness);
  }

  delay(10);
}

处理部分(在处理3上):

import processing.serial.*;

import cc.arduino.*;

Serial arduino;

void setup() {
  size(256, 150);

  arduino = new Serial(this, "COM3", 9600);

}

void draw() {
  background(constrain(mouseX, 0, 255));

  arduino.write(constrain(mouseX, 0, 255)); //  
}