# How to code button 2

**URL:** <https://discourse.processing.org/t/how-to-code-button-2/35167>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [February 14, 2022, 6:31pm UTC](https://discourse.processing.org/t/how-to-code-button-2/35167 "2022-02-14T18:31:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ata](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@ata](https://discourse.processing.org/u/ata)\
**Post date:** [February 14, 2022, 6:31pm UTC](https://discourse.processing.org/t/how-to-code-button-2/35167/1 "2022-02-14T18:31:10Z")

</div>

i cant manage to code button 2. the 3 is done and it stamps the pshape “v”  
i have to code button 2 wich should basically do the same as 3 but instead of the “v” pshape should stamp phsape “u” but i dont know how to make it work

PShape u;  
PShape v;

IntList cx = new IntList();  
IntList cy = new IntList();  
int xpos = 250;  
int ypos = 250;  
int counter=0;

import processing.serial._;  
import cc.arduino._;  
Arduino arduino;

int count = 0;

void setup() {  
size(1500, 760);  
smooth();  
// Prints out the available serial ports.  
println(Arduino.list());  
arduino = new Arduino(this, Arduino.list()[0], 57600);

// Set the Arduino digital pins as inputs.

arduino.pinMode(3, Arduino.INPUT);  
arduino.pinMode(2, Arduino.INPUT);

u = loadShape(“u.svg”);  
v = loadShape(“v.svg”);  
}

void draw() {

if (arduino.digitalRead(2)==Arduino.HIGH) {  
counter++;  
}  
if (counter == 1) {

```
//draw shape1

```

} else if (counter == 2) {

```
//draw shape2

```

} else if (counter == 3) {

```
//draw shape3

```

} else if (counter == 4) {

```
//draw shape1

```

} else if (counter == 5) {

```
counter=1;

```

}

// println(cx.size());  
// println(arduino.analogRead(0));

if (arduino.analogRead(0)\<300) {  
xpos-=5;  
} else if (arduino.analogRead(0)\>700) {  
xpos+=5;  
}  
if (arduino.analogRead(1)\>700) {  
ypos-=5;  
} else if (arduino.analogRead(1)\<300) {  
ypos+=5;  
}

//resize = map(arduino.analogRead(2),0,1023,0.1,2);

background(255);  
fill(127);  
for (int i = 0; i \< cx.size(); i++) {  
int px = cx.get(i);  
int py = cy.get(i);

```
fill(px, py, 200);
//this one is the stamp
shape(v, px, py, 150, 150);

// shape(u, px, py, 150, 150);

```

}

fill(mouseX, mouseY, 200);

//this one is the cursor  
shape(v, xpos, ypos, 50, 50);

// shape(u, xpos, ypos, 50, 50);

if (arduino.digitalRead(3)==Arduino.HIGH) {  
cx.append(xpos);  
cy.append(ypos);  
}  
}

THANK YOU

---

<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:** [February 17, 2022, 8:50am UTC](https://discourse.processing.org/t/how-to-code-button-2/35167/2 "2022-02-17T08:50:09Z")

</div>

> [@ata](#):
>
> if (arduino.digitalRead(2)==Arduino.HIGH) {  
> counter++;  
> }

Is this your button 2 ? Or which one is it?

The variable counter is increased here.

The variable counter is telling you which stamp to use.

So you need to check counter with “if” - this part is already there.

You can for example use counter to show different cursors (shapes) and when placing a stamp use counter and place different stamps depending on counter.

---

<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:** [February 17, 2022, 9:15am UTC](https://discourse.processing.org/t/how-to-code-button-2/35167/3 "2022-02-17T09:15:44Z")

</div>

here is an example/ simulation without Arduino

the way it works:

- button 2 changes counter (in mousePressed part 1)
- counter tells us which cursor to show
- when we click data is added to 3 intLists (in mousePressed part 2)
- we display the stamps from the 3 intLists (in the for-loop in `draw()`)

```auto

IntList cx = new IntList();
IntList cy = new IntList();
IntList cShapeType = new IntList();

int xpos = 250;
int ypos = 250;
int counter=1; // changed this to 1 !!!!!!!!!!!!!!!!!!!!!!!!!

// int count = 0;

void setup() {
  size(1500, 760);
  smooth();
}

void draw() {
  background(255);

  // draw button (see function mousePressed below)
  fill(255, 0, 0); 
  ellipse(0, 0, 210, 200); 
  fill(0); 
  text("Button 2 - click HERE !", 19, 19);

  //---------------------------------------------------------------
  //this one is the cursor
  fill(0); 
  if (counter == 1) {
    //draw shape1
    text("E", mouseX, mouseY);
  } else if (counter == 2) {
    //draw shape2
    text("F", mouseX, mouseY);
  } else if (counter == 3) {
    //draw shape3
    text("G", mouseX, mouseY);
  } else if (counter == 4) {
    //draw shape1
    text("H", mouseX, mouseY);
  } else if (counter == 5) {
    counter=1;
  } else {
    counter=1;
  }

  //this one is the stamp
  fill(0); 
  for (int i = 0; i < cx.size(); i++) {
    int xpos = cx.get(i);
    int ypos = cy.get(i);

    if (cShapeType.get(i) == 1) {
      //draw shape1
      text("E", xpos, ypos);
    } else if (cShapeType.get(i)== 2) {
      //draw shape2
      text("F", xpos, ypos);
    } else if (cShapeType.get(i)== 3) {
      //draw shape3
      text("G", xpos, ypos);
    } else if (cShapeType.get(i)== 4) {
      //draw shape 4
      text("H", xpos, ypos);
    }
  }//for
  //
}

void mousePressed() {
  if (dist(0, 0, mouseX, mouseY)<210) {
    counter++; 
    return;
  }

  // --------------------------------
  // draw stamp (add new stamp to list)

  cx.append(mouseX);
  cy.append(mouseY);
  cShapeType.append(counter); // !!!!!!!!!!!!!!!
}
//

```
