Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.serial.*;
- Serial port;
- char buzzer = '1';
- char led = '0';
- color defaultButton = color(255, 255, 255);
- color hoverButton = color(150, 150, 150);
- color defaultText = color(0, 102, 153);
- color hoverText = color(0, 0, 0);
- int buttonH = 50;
- int buttonW = 300;
- color stateOn = color(38, 191, 0);
- color stateOff = color(25, 115, 0);
- color stateOnText = color(47, 235, 0);
- color stateOffText = color(17, 87, 0);
- color ledStateOn = stateOn;
- color ledStateOff = stateOff;
- color ledStateOnText = stateOnText;
- color ledStateOffText = stateOffText;
- color buzzerStateOn = stateOn;
- color buzzerStateOff = stateOff;
- color buzzerStateOnText = stateOnText;
- color buzzerStateOffText = stateOffText;
- int stateW = buttonW;
- int stateH = buttonH;
- int ledAlarmTicks = 10;
- int ledTicks = 0;
- boolean ledOn = false;
- boolean aLedOn = false;
- int ledBlinkX = 10;
- int ledBlinkY = 10;
- int ledBlinkW = buttonW;
- int ledBlinkH = buttonH;
- color ledBlinkTextColor = defaultText;
- color ledBlinkButtonColor = defaultButton;
- int ledToggleX = ledBlinkX+ledBlinkW+10;
- int ledToggleY = 10;
- int ledToggleW = buttonW;
- int ledToggleH = buttonH;
- color ledToggleTextColor = defaultText;
- color ledToggleButtonColor = defaultButton;
- int buzzerAlarmTicks = 30;
- int buzzerTicks = 0;
- boolean buzzerOn = false;
- boolean aBuzzerOn = false;
- int buzzerBlinkX = ledBlinkX;
- int buzzerBlinkY = ledBlinkY+ledBlinkH+10;
- int buzzerBlinkW = buttonW;
- int buzzerBlinkH = buttonH;
- color buzzerBlinkTextColor = defaultText;
- color buzzerBlinkButtonColor = defaultButton;
- int buzzerToggleX = buzzerBlinkX+buzzerBlinkW+10;
- int buzzerToggleY = ledBlinkY+ledBlinkH+10;
- int buzzerToggleW = buttonW;
- int buzzerToggleH = buttonH;
- color buzzerToggleTextColor = defaultText;
- color buzzerToggleButtonColor = defaultButton;
- int bothBlinkX = buzzerBlinkX;
- int bothBlinkY = buzzerBlinkY+buzzerBlinkH+10;
- int bothBlinkW = buttonW;
- int bothBlinkH = buttonH;
- color bothBlinkTextColor = defaultText;
- color bothBlinkButtonColor = defaultButton;
- int bothToggleX = bothBlinkX+bothBlinkW+10;
- int bothToggleY = buzzerBlinkY+buzzerBlinkH+10;
- int bothToggleW = buttonW;
- int bothToggleH = buttonH;
- color bothToggleTextColor = defaultText;
- color bothToggleButtonColor = defaultButton;
- void setup(){
- size(630, 500);
- frame.setTitle("Arduino Alarm");
- port = new Serial(this, "COM3", 9600);
- port.bufferUntil('\n');
- }
- void draw(){
- PFont font = createFont(PFont.list()[0], 32);
- background(0, 0, 0);
- stroke(256);
- // Buttons
- fill(ledBlinkButtonColor);
- rect(ledBlinkX, ledBlinkY, ledBlinkW, ledBlinkH);
- textFont(font);
- fill(ledBlinkTextColor);
- text("BLINK LED", ledBlinkX+10, ledBlinkY+35);
- fill(ledToggleButtonColor);
- rect(ledToggleX, ledToggleY, ledToggleW, ledToggleH);
- textFont(font);
- fill(ledToggleTextColor);
- text("TOGGLE LED", ledToggleX+10, ledToggleY+35);
- fill(buzzerBlinkButtonColor);
- rect(buzzerBlinkX, buzzerBlinkY, buzzerBlinkW, buzzerBlinkH);
- textFont(font);
- fill(buzzerBlinkTextColor);
- text("TIMED BUZZER", buzzerBlinkX+10, buzzerBlinkY+35);
- fill(buzzerToggleButtonColor);
- rect(buzzerToggleX, buzzerToggleY, buzzerToggleW, buzzerToggleH);
- textFont(font);
- fill(buzzerToggleTextColor);
- text("TOGGLE BUZZER", buzzerToggleX+10, buzzerToggleY+35);
- fill(bothBlinkButtonColor);
- rect(bothBlinkX, bothBlinkY, bothBlinkW, bothBlinkH);
- textFont(font);
- fill(bothBlinkTextColor);
- text("PULSE BOTH", bothBlinkX+10, bothBlinkY+35);
- fill(bothToggleButtonColor);
- rect(bothToggleX, bothToggleY, bothToggleW, bothToggleH);
- textFont(font);
- fill(bothToggleTextColor);
- text("TOGGLE BOTH", bothToggleX+10, bothToggleY+35);
- // Status
- if(aLedOn){
- fill(ledStateOn);
- }else{
- fill(ledStateOff);
- }
- rect(bothBlinkX, bothBlinkY+bothBlinkH+10, stateW, stateH);
- textFont(font);
- if(aLedOn){
- fill(ledStateOnText);
- }else{
- fill(ledStateOffText);
- }
- text("LED", bothBlinkX+10, bothBlinkY+bothBlinkH+45);
- if(aBuzzerOn){
- fill(buzzerStateOn);
- }else{
- fill(buzzerStateOff);
- }
- rect(bothToggleX, bothToggleY+bothToggleH+10, stateW, stateH);
- textFont(font);
- if(aBuzzerOn){
- fill(buzzerStateOnText);
- }else{
- fill(buzzerStateOffText);
- }
- text("BUZZER", bothToggleX+10, bothToggleY+bothToggleH+45);
- // Handle loops
- if(ledOn){
- if(ledTicks>=ledAlarmTicks){
- ledTicks = 0;
- port.write(led);
- }
- ledTicks++;
- }else{
- ledTicks = 0;
- }
- if(buzzerOn){
- if(buzzerTicks>=buzzerAlarmTicks){
- buzzerTicks = 0;
- port.write(buzzer);
- }
- buzzerTicks++;
- }else{
- buzzerTicks = 0;
- }
- // Handle hover
- if(overRect(ledBlinkX, ledBlinkY, ledBlinkW, ledBlinkH)){
- resetButtons();
- ledBlinkTextColor = hoverText;
- ledBlinkButtonColor = hoverButton;
- }else if(overRect(ledToggleX, ledToggleY, ledToggleW, ledToggleH)){
- resetButtons();
- ledToggleTextColor = hoverText;
- ledToggleButtonColor = hoverButton;
- }else if(overRect(buzzerBlinkX, buzzerBlinkY, buzzerBlinkW, buzzerBlinkH)){
- resetButtons();
- buzzerBlinkTextColor = hoverText;
- buzzerBlinkButtonColor = hoverButton;
- }else if(overRect(buzzerToggleX, buzzerToggleY, buzzerToggleW, buzzerToggleH)){
- resetButtons();
- buzzerToggleTextColor = hoverText;
- buzzerToggleButtonColor = hoverButton;
- }else if(overRect(bothBlinkX, bothBlinkY, bothBlinkW, bothBlinkH)){
- resetButtons();
- bothBlinkTextColor = hoverText;
- bothBlinkButtonColor = hoverButton;
- }else if(overRect(bothToggleX, bothToggleY, bothToggleW, bothToggleH)){
- resetButtons();
- bothToggleTextColor = hoverText;
- bothToggleButtonColor = hoverButton;
- }else{
- resetButtons();
- }
- }
- void mousePressed(){
- if(overRect(ledBlinkX, ledBlinkY, ledBlinkW, ledBlinkH)){
- ledOn = !ledOn;
- if(!ledOn && aLedOn){
- port.write(led);
- }
- }else if(overRect(ledToggleX, ledToggleY, ledToggleW, ledToggleH)){
- port.write(led);
- }else if(overRect(buzzerBlinkX, buzzerBlinkY, buzzerBlinkW, buzzerBlinkH)){
- buzzerOn = !buzzerOn;
- if(!buzzerOn && aBuzzerOn){
- port.write(buzzer);
- }
- }else if(overRect(buzzerToggleX, buzzerToggleY, buzzerToggleW, buzzerToggleH)){
- port.write(buzzer);
- }else if(overRect(bothBlinkX, bothBlinkY, bothBlinkW, bothBlinkH)){
- ledOn = !ledOn;
- if(!ledOn && aLedOn){
- port.write(led);
- }
- buzzerOn = !buzzerOn;
- if(!buzzerOn && aBuzzerOn){
- port.write(buzzer);
- }
- }else if(overRect(bothToggleX, bothToggleY, bothToggleW, bothToggleH)){
- port.write(buzzer);
- port.write(led);
- }
- }
- void serialEvent(Serial port){
- float in = float(port.readStringUntil('\n'));
- if(in == 1){
- aLedOn = true;
- }else if(in == 0){
- aLedOn = false;
- }else if(in == 2){
- aBuzzerOn = false;
- }else if(in == 3){
- aBuzzerOn = true;
- }
- }
- void resetButtons(){
- ledBlinkTextColor = defaultText;
- ledBlinkButtonColor = defaultButton;
- ledToggleTextColor = defaultText;
- ledToggleButtonColor = defaultButton;
- buzzerBlinkTextColor = defaultText;
- buzzerBlinkButtonColor = defaultButton;
- buzzerToggleTextColor = defaultText;
- buzzerToggleButtonColor = defaultButton;
- bothBlinkTextColor = defaultText;
- bothBlinkButtonColor = defaultButton;
- bothToggleTextColor = defaultText;
- bothToggleButtonColor = defaultButton;
- }
- boolean overRect(int x, int y, int width, int height){
- if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height){
- return true;
- }else{
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment