点亮led灯

来自Microduino Wikipedia
跳转至: 导航搜索
Language English

目的

本教程将教大家如何用Processing来点亮LED灯。

设备

  • 其他硬件设备
    • USB数据连接线 一根
    • LED灯 一个
    • 330欧电阻 一个
    • 面包板 一个
    • 跳线 一盒

原理图

ProcessingControlLED原理图.jpg


程序

LEDButtonByOver

LEDButtonByClick

注意:代码里包含图片请不要丢掉

调试

步骤一:按着原理图搭建硬件环境,像这样:

ProcessingControlLED连接图.jpg


步骤二:解释一下代码:

 // Declaring two variable of type PImage, a class available to us from the Processing core library.
 PImage LEDON, LEDOFF; 
 //load two img
 LEDON = loadImage("LEDON.png");
 LEDOFF = loadImage("LEDOFF.png");

在LEDButtonByOver的代码中实现的是鼠标经过点亮小灯

Mouse over:

 // When the mouse is moved, the state of the button is toggled. 
 void mouseMoved() {
   if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
     button = true;
   }  
   else {
     button = false;
   }
 }

在LEDButtonByClick的代码中实现的是鼠标点击点亮小灯

Mouse Click:

 // When the mouse is pressed, the state of the button is toggled.   
 void mousePressed() {
   if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
     button = !button;
   }  
 }


步骤三:下载代码并编译通过。


步骤四:运行后鼠标滑过灯泡上方或者点击灯泡,看看有什么反应。

结果

鼠标滑过灯泡上方,灯泡会亮起,离开会熄灭,Processing的动画效果是这样的:

鼠标滑过或点击灯泡:

ProcessingOverLEDON.jpg

鼠标离开或点击灯泡:

ProcessingOverLEDOFF.jpg

视频