Arduino Elevator Control with Processing
import processing.serial.*; Serial port;
void setup(){ port=new Serial(this,"COM3",9600); //Arduino板的端口号 size(200,500); textSize(150); }
void draw(){ background(225);
String floors = 'F9 F8 F7 F6 F5 F4 F3 F2 F1'; textSize(24); fill(255,0,0); textLeading(50); text(floors,20,25,40,500);
fill(100); rect(50,0,100,50);
fill(100); rect(50,50,100,50);
fill(100); rect(50,100,100,50);
fill(100); rect(50,150,100,50);
fill(100); rect(50,200,100,50);
fill(100); rect(50,250,100,50);
fill(100); rect(50,300,100,50);
fill(100); rect(50,350,100,50);
fill(100); rect(50,400,100,50); }
void mouseClicked(){ if((mouseX>=50)&(mouseX<=150)&(mouseY>=0)&(mouseY<=50)) { println('9floor'); port.write(0x09); } else if ((mouseX>=50)&(mouseX<=150)&(mouseY>=50)&(mouseY<=100)) { println('8floor'); port.write(0x08); } else if ((mouseX>=50)&(mouseX<=150)&(mouseY>=100)&(mouseY<=150)) { println('7floor'); port.write(0x07); } else if ((mouseX>=50)&(mouseX<=150)&(mouseY>=150)&(mouseY<=200)) { println('6floor'); port.write(0x06); } else if ((mouseX>=50)&(mouseX<=150)&(mouseY>=200)&(mouseY<=250)) { println('5floor'); port.write(0xfe); } else if ((mouseX>=50)&(mouseX<=150)&(mouseY>=250)&(mouseY<=300)) { println('4floor'); port.write(0xfd); } else if ((mouseX>=50)&(mouseX<=150)&(mouseY>=300)&(mouseY<=350)) { println('3floor'); port.write(0xfb); } else if ((mouseX>=50)&(mouseX<=150)&(mouseY>=350)&(mouseY<=400)) { println('2floor'); port.write(0xf7); } else if ((mouseX>=50)&(mouseX<=150)&(mouseY>=400)&(mouseY<=450)) { println('1floor'); port.write(0xef); }
} This code demonstrates a simple elevator control system using Processing and Arduino. It allows you to simulate an elevator by clicking on different floors and sending control signals to the Arduino board.
The code establishes a serial connection to the Arduino board using the 'Serial' class. It then sets up the graphical interface, displaying nine floors with corresponding labels.
When you click on a floor, the code sends a specific hex value to the Arduino board via the serial port. These hex values correspond to different floor levels.
For example, clicking on the 9th floor will send the hex value 0x09 to the Arduino. The Arduino code would then process this signal and control the elevator to move to the corresponding floor.
This is a simplified example and a real elevator control system would be much more complex, requiring safety measures, error handling, and multi-user interactions.
You can expand on this code by adding features such as displaying the current floor, adding visual effects to simulate elevator movement, and incorporating more advanced control logic.
原文地址: http://www.cveoy.top/t/topic/la2v 著作权归作者所有。请勿转载和采集!