# Sketch set-up - time of actions

**URL:** <https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [May 28, 2019, 8:00am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651 "2019-05-28T08:00:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aleandrosinatra](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@aleandrosinatra](https://discourse.processing.org/u/aleandrosinatra)\
**Post date:** [May 28, 2019, 8:00am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/1 "2019-05-28T08:00:11Z")

</div>

Hello guys, i need to help to set a project. I’d like to know how can i write the code by Processing. I don’t understand to set a counter to make some operations.  
I explain my project; there is a beamer will project a black screen with a string of carachters made with Processing, if the sharp sensor is triggered for 30 seconds i want to show just a black screen without the string of character. Is the sensor is triggered for 2 minutes, Processing will show an image.

I copy my sketch so you can see my project.

```auto
import codeanticode.syphon.*;

import processing.serial.*;

Serial myPort; // Create object from Serial class
int val; // Data received from the serial port

SyphonServer server;
float x = 0;
ArrayList<TEXTBOX> textboxes = new ArrayList<TEXTBOX>();

boolean send = false;
String msg = "";
String msg2 = "";

int pressed = 0;

Timer startTimer;

void setup() {
  size(1200, 700, P3D);
  printArray(Serial.list());
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 115200);

  server = new SyphonServer(this, "Processing Syphon");
  startTimer = new Timer(10);

  InitLayout();
}

void draw() { 
  if ( myPort.available() > 0) { // If data is available,
    val = myPort.read(); // read it and store it in val
  }

  if (val == 1) { // sensor triggered
    background(0);
    PFont f = createFont ("Georgia", 40);
    textFont(f);
    textSize(20);

    String s = " Try to sit in two chairs between a table ";
    text(s, 415, 100);
    String l = " Choose a shot of alcohol or juice, water or nothing too and stay in silence mode ";
    text(l, 232, 600);

    // start timer
    startTimer.countDown();
    if (startTimer.getTime() <= 0) {
      println("Do something");
    }
  } else {
    background(200);

    PFont f = createFont ("Georgia", 40);
    PFont z = createFont ("Georgia", 30);
    PFont d = createFont ("Georgia", 80);
    if (pressed !=1) {

      String c = "Try to write in the Text boxes the words ";
   // String s = " sit in two chairs between a table. They will get a shot of alcohol or juice, water or nothing too and ";

   //textFont(f);
     // textSize(20);
    // text(s, 100, 100);

      textFont(d);
      textSize(20);
      text(c, 100, 400);
      color(20);

      for (TEXTBOX t : textboxes) {
        t.DRAW();
      }
    }
  }
  if (send) {
    text(msg, (width/2 - textWidth(msg)), 260);
    text(msg2, width/2, 260);
    startTimer.countDown();
    color(240);
    fill(0);
    text(startTimer.getTime(), width/2-40, 290);
  }
  server.sendScreen();
}

void InitLayout() {
  TEXTBOX receiver = new TEXTBOX();
  receiver.W = 500;
  receiver.H = 50;
  receiver.X = (width - receiver.W) / 13;
  receiver.Y = 200;

  textboxes.add(receiver);

  TEXTBOX message = new TEXTBOX((width - receiver.W) / 13, 265, 500, 50);
  textboxes.add(message);
}

void mousePressed() {
  for (TEXTBOX t : textboxes) {
    t.PRESSED(mouseX, mouseY);
  }
}

void keyPressed() {
  for (TEXTBOX t : textboxes) {
    if (t.KEYPRESSED(key, keyCode)) {
      send = true;
      msg = textboxes.get(0).Text + " << ";
      msg2 = " >> " + textboxes.get(1).Text;
      pressed = 1;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 29, 2019, 1:17am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/2 "2019-05-29T01:17:34Z")

</div>

sorry, is that the same code you posted  
[Time of actions](https://discourse.processing.org/t/time-of-actions/11629) here?

and why make a new topic after  
in the old one you answer 2 times with the ?same? text?

* * *

anyhow you confirmed that the arduino trigger works already  
( and so the arduino category is misleading )

but the timeout for the text display NOT WORK?  
inhowfar? it not stop display that text?  
where in your code you try that?

* * *

so can you please delete your code here and  
invest the 15min to make a code ONLY with the timer issue ( and a test text )

BUT NOT use any library / arduino communication / operation …  
just a 10 line RUNable code so we can play with it and try to help  
with the timer question only.

---

<div class="post-metadata">

**Author:** ![aleandrosinatra](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@aleandrosinatra](https://discourse.processing.org/u/aleandrosinatra)\
**Post date:** [May 29, 2019, 8:48am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/3 "2019-05-29T08:48:38Z")

</div>

thanks, sorry for this but i didm’t know more of the coding. Arduino is working and Processing too. Anyway i try to copy my code to display only the image.  
( the action could be: count for 2 minutes and read an image for 5 second then will be display black background ). Why it is not working ?

float count;  
PImage fragment;  
int rand; \ READ AN IMAGE RANDOM  
float count2;

void setup() {  
size(1200, 700);  
count = 0;

rand = int (random(0, 3 ));  
count2= 0;  
}

void draw() {

```
count ++;

if (count > 200) { \\ AFTER 2 SECOND DISPLAY A BLACK BACKGROUND
  fill(0);
  rect(0, 0, 1200, 700);

  count = 0;
}

```

count ++;

```
if (count2 > 300) { \\ AFTER 3 SECOND DISPLAY A RANDOM IMAGE
 takerandomimage("image" + nf(rand, 3) + ".jpg");

  count = 0;
}

```

}

void takerandomimage(String fn) {  
fragment = loadImage(fn);  
image(fragment, 0, 0);  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 29, 2019, 9:34am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/4 "2019-05-29T09:34:20Z")

</div>

draw runs 60 times per second, so your count won’t work

In fact draw runs not reliable fast but has slower and faster moments

Look at millis () and make a timer

---

<div class="post-metadata">

**Author:** ![aleandrosinatra](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@aleandrosinatra](https://discourse.processing.org/u/aleandrosinatra)\
**Post date:** [May 29, 2019, 9:37am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/5 "2019-05-29T09:37:47Z")

</div>

can you write the right sketch ? please because i don’t know the millis funciont ()

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 29, 2019, 9:48am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/6 "2019-05-29T09:48:44Z")

</div>

the last code you posted has nothing to do with the timer from your first code,  
and again it uses some ( in respect of the question ) useless

```auto
loadimage

```

function what requires files we not have so we can not run that code.

also why suddenly you can not post code anymore?

```auto
</> code tag
type or paste code here

```

for other timer can also search here  
[https://discourse.processing.org/search?q=millis%20timer](https://discourse.processing.org/search?q=millis%20timer)  
but i am mainly interested in your timer code part  
you have in your original code, and why it is not working?  
how you code and test it first time?

---

<div class="post-metadata">

**Author:** ![aleandrosinatra](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@aleandrosinatra](https://discourse.processing.org/u/aleandrosinatra)\
**Post date:** [May 29, 2019, 9:53am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/7 "2019-05-29T09:53:11Z")

</div>

i can copy all sketch

import codeanticode.syphon.\*;

import processing.serial.\*;

Serial myPort; // Create object from Serial class  
int val; // Data received from the serial port

SyphonServer server;  
float x = 0;  
ArrayList textboxes = new ArrayList();

boolean send = false;  
String msg = “”;  
String msg2 = “”;

int pressed = 0;

Timer startTimer;

float count;  
PImage fragment;  
int rand;

float count2;

void setup() {  
size(1200, 700, P3D);  
printArray(Serial.list());  
String portName = Serial.list()[2];  
myPort = new Serial(this, portName, 115200);

server = new SyphonServer(this, “Processing Syphon”);  
startTimer = new Timer(10);

InitLayout();

count = 0;

rand = int (random(0, 3 ));  
}

void draw() {  
if ( myPort.available() \> 0) { // If data is available,  
val = myPort.read(); // read it and store it in val  
}

if (val == 1) { // sensor triggered  
background(0);  
PFont f = createFont (“Georgia”, 40);  
textFont(f);  
textSize(20);

```
String s = " Try to sit in two chairs between a table ";
text(s, 415, 100);
String l = " Choose a shot of alcohol or juice, water or nothing too and stay in silence mode ";
text(l, 232, 600);

count ++;

if (count > 200) { 
  fill(0);
  rect(0, 0, 1200, 700);

  count = 0;
}

// start timer
startTimer.countDown();
if (startTimer.getTime() <= 0) {
  println("Do something");
}

```

} else {  
background(200);

count ++;  
if (count \> 300) {  
takerandomimage(“image” + nf(rand, 3) + “.jpg”);

```
  count = 0;
}

PFont f = createFont ("Georgia", 40);
PFont z = createFont ("Georgia", 30);
PFont d = createFont ("Georgia", 80);
if (pressed !=1) {

  String c = "Try to write in the Text boxes the words ";
  // String s = " sit in two chairs between a table. They will get a shot of alcohol or juice, water or nothing too and ";

  //textFont(f);
  // textSize(20);
  // text(s, 100, 100);

  textFont(d);
  textSize(20);
  text(c, 100, 400);
  color(20);

  for (TEXTBOX t : textboxes) {
    t.DRAW();
  }
}

```

}  
if (send) {  
text(msg, (width/2 - textWidth(msg)), 260);  
text(msg2, width/2, 260);  
startTimer.countDown();  
color(240);  
fill(0);  
text(startTimer.getTime(), width/2-40, 290);  
}  
server.sendScreen();  
}

void InitLayout() {  
TEXTBOX receiver = new TEXTBOX();  
receiver.W = 500;  
receiver.H = 50;  
receiver.X = (width - receiver.W) / 13;  
receiver.Y = 200;

textboxes.add(receiver);

TEXTBOX message = new TEXTBOX((width - receiver.W) / 13, 265, 500, 50);  
textboxes.add(message);  
}

void mousePressed() {  
for (TEXTBOX t : textboxes) {  
t.PRESSED(mouseX, mouseY);  
}  
}

void keyPressed() {  
for (TEXTBOX t : textboxes) {  
if (t.KEYPRESSED(key, keyCode)) {  
send = true;  
msg = textboxes.get(0).Text + " \<\< ";  
msg2 = " \>\> " + textboxes.get(1).Text;  
pressed = 1;  
}  
}  
}

void takerandomimage(String fn) {  
fragment = loadImage(fn);  
image(fragment, 0, 0);  
}

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 29, 2019, 10:00am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/8 "2019-05-29T10:00:50Z")

</div>

no, i was referring to your

```auto
   // start timer
    startTimer.countDown();
    if (startTimer.getTime() <= 0) {
      println("Do something");
    }

```

tell us more about this please.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 29, 2019, 10:29am UTC](https://discourse.processing.org/t/sketch-set-up-time-of-actions/11651/9 "2019-05-29T10:29:45Z")

</div>

just a remark, not interfering

```auto

int timer; 
int i;

void setup() {
  size(555, 555);
  background(0);
  timer = millis();
}

void draw() {
  background(0);

  if (millis()-timer > 1000) {
    timer = millis();
    i++;
  }
  text(i, 33, 33);
}//
//

```
