Turning on relays via serial port,building the GUI

edited October 2016 in Arduino

I've made a small program to turn on and off relays via ardiuno and it works. Now I would like to make a proper interface so basically have rectangle on screen and show which button/light is turned on. So when I press 1 the rectangle labled light 1 should turn red and go back to the when I release the key. the problem i'm having is that when the rectangle turns red it stays red without going back even when key is release. I could use the keyReleased function but then how would I include that into my current program?

import processing.serial.*;

Serial myPort;  // Create object from Serial class

void setup() 
{
  size(200, 200); 
  String portName = Serial.list()[5]; 
  myPort = new Serial(this, portName, 9600);
  printArray(Serial.list());
}
void draw() {
  if ((keyPressed == true)&&(key == '1'))
  { 
    myPort.write('a');         
    println("1");
  } else if ((keyPressed == true)&&(key == '2')) {
    myPort.write('b');
    println("2");
  } else if ((keyPressed == true)&&(key == '3')) {
    myPort.write('c');
    println("3");
  } else if ((keyPressed == true)&&(key == '4')) {
    myPort.write('d');
    println("4");
  } else if ((keyPressed == true)&&(key == '5')) {
    myPort.write('e');
    println("5");
  } else 
  {                          
    myPort.write('0');
  }
}

Here is the code showing the idea about the rectangle turning red I've used ellipse just to show you the idea.

void setup(){
          size(400,400);
          ellipse(20,20,20,20);



        }
        void draw(){
        if((keyPressed == true)&&(key == '1')){
          ellipse(20,20,20,20);
          fill(255,0,0);


        }





        }                       
Tagged:

Answers

  • edited October 2016 Answer ✓

    Fairly simple actually, all you have to do is add an else statement after your if statement, and inside it draw an ellipse with the other color.
    BTW, your first program may not work properly when pressing more than one key at a time, though I could be wrong.

  • Answer ✓

    One way to do things....

    Kf

    P.S. I am not fan of managing keyPressed in draw. I have seen some unwanted behavior before.

    final color buttonON = color(0, 255, 0);  //Green
    final color buttonOFF = color(0, 0, 0);    //Black
    
    color button1;
    int posB1x=20;
    int posB1y=20;
    
    void setup() {
      size(400, 400);
      ellipse(20, 20, 20, 20);
    }
    void draw() {
    
      fill(button1);
      ellipse(posB1x,posB1y, 20, 20);
    } 
    
    void keyPressed() {
      if (key == '1') 
        button1=buttonON;
    }
    
    void keyReleased() {
      if (key == '1') 
        button1=buttonOFF;
    }
    
  • @kfrajer it is a good example.
    More complex version, here you just need the ASCII code of a character to know if it is pressed on the keyBoard(will add support for special, i.e. coded, characters later):

    boolean[] pressed = new boolean[256];
    
    void setup(){
      size(400, 400);
      //REQUIRED
      for(int i = 0; i < pressed.length; i++){
        pressed[i] = false;//required to prevent NullPointerAccess
      }
      //NOT REQUIRED - just an example
      noStroke();
      fill(255);
      textSize(25);
    }
    
    void draw(){
      //NOT REQUIRED - just an example
      for(int x = 0; x < 16; x++){
        for(int y = 0; y < 16; y++){
          //not very wise of me to do it in a hardcoded way
          if(pressed[x + y*16]){
            fill(0);
            rect(x*25, y*25, 25, 25);
            fill(255);
            text((char)(x + y*16), x*25, (y+1)*25);
          }else{
            rect(x*25, y*25, 25, 25);
          }
        }
      }
      //READ ME
      //To find whether a key is pressed - pressed[key] will store whether it is pressed, where key is the ASCII code of the letter
      //Example - pressed['1'] will be true only when the no 1 key is pressed
    }
    
    //NOTE: Even this fails if two keys are pressed(or released) at the exact same time
    //NOTE 2: It also does not work(I don't know why) when I press more than 5 buttons at a time 
    void keyPressed(){
      if(key < 256)pressed[key] = true;//ignore most non-ASCII characters
    }
    
    void keyReleased(){
      if(key < 256)pressed[key] = false;//ignore most non-ASCII characters
    }  
    
Sign In or Register to comment.