How can i use Keypressed instead of mouse position in processing to work with arduino?

edited November 2015 in Arduino

Hi, so this is my code so far and all works well. However i would like to use 3 hotkeys to change the colour than mouse position.

Anyone know how i can do this?

Thanks

Processing code

   import processing.serial.*;
   Serial port;
   int c;

   void setup() {
     size(300, 150);
    // println("Available serial ports:");
    // println(Serial.list());
    port = new Serial(this, "/dev/cu.usbmodem1421", 9600);
    c = 2;
   }

  void draw() {
  background(255);
  line(100, 0, 100, height);
  line(200, 0, 200, height);
   // write the current X-position of the mouse to the serial port as
  // a single byte

 int newc = int(1 + mouseX/100);
 if (newc != c) {
 c = newc; 

println(">> " + c); // DEBUG
port.write("" + c);
 }
}

Arduino code

   #include <Adafruit_GFX.h>    // Core graphics library
   #include <Adafruit_ST7735.h> // Hardware-specific library
   #include <SPI.h>

   #define TFT_CS     10
   #define TFT_RST    9
   #define TFT_DC     8
   #define TFT_SCLK 13
   #define TFT_MOSI 11

   Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

   void setup(void) {
     Serial.begin(9600);
     tft.initR(INITR_144GREENTAB);   
     tft.fillScreen(ST7735_BLACK);
     Serial.println("OK!");
   }

   void loop() {
     tft.invertDisplay(false);
     if (Serial.available()) {
       byte c = Serial.read();
       if (c == '1') {
         testdrawrects(ST7735_GREEN);
       } else if (c == '2') {
         testdrawrects(ST7735_WHITE);      
       } else if (c == '3') {
         testdrawrects(ST7735_RED);
       }
       Serial.println(c); // DEBUG
     }
   }

   void testdrawrects(uint16_t color) {
     // tft.fillScreen(ST7735_BLACK);
     for (int16_t x=0; x < tft.width(); x+=6) {
       tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
     }
   }
Tagged:

Answers

  • I may guess that you need to only change processing part like this (not tested):

     import processing.serial.*;
       Serial port;
       int c;
    
       void setup() {
         size(300, 150);
         port = new Serial(this, "/dev/cu.usbmodem1421", 9600);
         c = 2;
       }
    
      void draw() {
      background(255);
      }
        void keyPressed(){
        switch (key){
            case 'a':
                c = 1;
                break;
            case 's':
                c = 2;
                break;
            case 'd':
                c = 3;
                break;
        }     
            println(">> " + c);
            port.write("" + c);
        }
    
Sign In or Register to comment.