# Developing a coding to move on a robot

**URL:** <https://discourse.processing.org/t/developing-a-coding-to-move-on-a-robot/5272>\
**Category:** Coding Questions\
**Created:** [November 6, 2018, 7:25pm UTC](https://discourse.processing.org/t/developing-a-coding-to-move-on-a-robot/5272 "2018-11-06T19:25:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveTombs92](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@SteveTombs92](https://discourse.processing.org/u/SteveTombs92)\
**Post date:** [November 6, 2018, 7:25pm UTC](https://discourse.processing.org/t/developing-a-coding-to-move-on-a-robot/5272/1 "2018-11-06T19:25:37Z")

</div>

Hello Processing community. I am learning Processing by myself and I found some interesting exercises to develop coding, for a robot in this case. I have created the code. However, I am still do not know how I can transform this code from mouse functions into commands such as command key or use alt shift command plus or commands with letters such as S, A, W, J, H, to substitute mouse functions.

loat xVal = 50;  
float yVal = 1100;  
int myHeadRadius = 300;  
int myBodyHeight = 1000;  
int myBodyWidth = 500;  
int myNeckHeight = 200;  
// Our text included this without using it…  
float easingVal = 0.1;

void setup() {  
size(1200,1200);  
ellipseMode(RADIUS); // draw ellipse from top  
}

void draw() {  
strokeWeight(90);

background(0, 100, 200); // Blue

translate(mouseX, yVal); // Move all to myNewX

// Toggle the scale of everything  
if (mousePressed) {  
scale(0.6);  
} else {  
scale(0.4);  
}

// Body  
noStroke();  
fill(255, 204, 0); // Orange  
ellipse(0, -99, 99,99); // The leg  
fill(0); // Black  
// Calculate the rect for the body relative to the 0,0 center  
rect(-myBodyWidth/2, -myBodyHeight, myBodyWidth, myBodyHeight-99);

// Neck  
// Set the y location of the neck relative to the size of the  
// other components  
float myNeckY = -1 \* (myBodyHeight + myNeckHeight + myHeadRadius);  
stroke(255); // White  
line(30, -myBodyHeight, 10, myNeckY);

// Hair  
pushMatrix(); // Isolate translation and rotation  
// translate from the top of the head  
translate(44, myNeckY);  
// set myAngle to 1/90th of half way around  
float myAngle = -PI/444.0;  
for (int i = 0; i \<= 444; i++) {  
// do these statement 99 times, rotating a bit each time  
line(444, 0, 0, 0);  
rotate(myAngle);  
}  
popMatrix(); // end isolation of translate and rotate

// Head  
noStroke();  
// Red head circle  
fill(0); // Red  
ellipse(12, myNeckY, myHeadRadius, myHeadRadius);  
// White eyeball  
fill(255); // White  
int eyeSize = 200;  
ellipse(12, myNeckY-12, eyeSize, eyeSize);  
// Green Iris  
fill(0, 200, 200); // Green  
ellipse(24, myNeckY-9, eyeSize_0.66,eyeSize_0.66);  
// Black pupil  
fill(0); // Black  
ellipse(20, myNeckY-2, eyeSize_0.33,eyeSize_0.33);

I want to ask you a big favor. Could you please tell me how to turn this code from mouse function into commandkeys? Do I have to change floats into bolans? I want to be able to build the same robot, but with command keys functions.

Thank you so much.

Sincerely  
Steve

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [November 6, 2018, 7:50pm UTC](https://discourse.processing.org/t/developing-a-coding-to-move-on-a-robot/5272/2 "2018-11-06T19:50:48Z")

</div>

Please don’t post multiple copies of the same question. I’ve closed your other threads. Please stick with one post per question.

Also, please try to format your code. You can edit your post to format your code using the `</>` button.

---

<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:** [November 7, 2018, 1:26am UTC](https://discourse.processing.org/t/developing-a-coding-to-move-on-a-robot/5272/3 "2018-11-07T01:26:11Z")

</div>

hi @SteveTombs92

here i try to help even i must start also to put down your code posting, pls

```auto
// reduce your code down to the minimum 
// just to show what is the question.

```

like

```auto
void setup() {
  size(300, 300);
  rectMode(CENTER);
}

void draw() {
  background(0, 100, 200); // Blue
  translate(width/2, height/2);

  // Toggle the scale of everything
  if (mousePressed) {
    scale(0.6);
  } else {
    scale(0.4);
  }

  fill(255, 204, 0); // Orange
  rect(0, 0, 100, 100);
}

```

so that is you current operation concept?

> mouse click ( keep pressed ) for zoom factor 1.5

and now you would like to use keyboard too?  
try this:

```auto
void setup() {
  size(300, 300);
  rectMode(CENTER);
  setup_zoom_info(); // add
}

void draw() {
  background(0, 100, 200); // Blue
  translate(width/2, height/2);

  scale(Zmag); // add

  // Toggle the scale of everything
  if (mousePressed) {
    scale(0.6);
  } else {
    scale(0.4);
  }

  fill(255, 204, 0); // Orange
  rect(0, 0, 100, 100);
}

// to make a real zoom by mouse wheel and keyboard [page up down] add:

float Zmag = 1.0, Zoomf = 5.0;
// _________________________________________________________________ mouseWheel ZOOM
void mouseWheel(MouseEvent event) {
  Zmag += event.getCount()/Zoomf;
  println("mousewheel: "+Zmag);
}

void keyPressed(){
 if ( keyCode == 33 ) {Zmag += 1/Zoomf ;} // [page up]
 if ( keyCode == 34 ) {Zmag -= 1/Zoomf ;} // [page down]
 println("keypressed: key: "+key+" keyCode: "+keyCode+" zoom: "+Zmag);
}

void setup_zoom_info(){
  println(" operation: "); 
  println(" for zoom use keyboard: [Page Up] or [Page Down] "); 
  println(" or mouse click for toggle "); 
  println(" or mouse wheel "); 
}

```

---

<div class="post-metadata">

**Author:** ![PascalAudet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pascalaudet/32/1903_2.png) [@PascalAudet](https://discourse.processing.org/u/PascalAudet)\
**Post date:** [November 7, 2018, 3:08am UTC](https://discourse.processing.org/t/developing-a-coding-to-move-on-a-robot/5272/4 "2018-11-07T03:08:03Z")

</div>

> [@SteveTombs92](#):
>
> rn this code from mouse function into commandkeys? Do

Here:

I add a keyPressed and the Xpos var

void keyPressed() {  
if (key == CODED) {  
if (keyCode == LEFT) {  
Xpos=Xpos-7;  
} else if (keyCode == RIGHT) {  
Xpos=Xpos+7;  
}  
}  
}

```auto

float xVal = 50;
float yVal = 1100;
int myHeadRadius = 300;
int myBodyHeight = 1000;
int myBodyWidth = 500;
int myNeckHeight = 200;
// Our text included this without using it…
float easingVal = 0.1;

int Xpos=20;

void setup() {
  size(1200, 1200);
  ellipseMode(RADIUS); // draw ellipse from top
}

void draw() {
  strokeWeight(90);

  background(0, 100, 200); // Blue

  translate(Xpos, yVal); // Move all to myNewX

  // Toggle the scale of everything
  if (mousePressed) {
    scale(0.6);
  } else {
    scale(0.4);
  }

  // Body
  noStroke();
  fill(255, 204, 0); // Orange
  ellipse(0, -99, 99, 99); // The leg
  fill(0); // Black
  // Calculate the rect for the body relative to the 0,0 center
  rect(-myBodyWidth/2, -myBodyHeight, myBodyWidth, myBodyHeight-99);

  // Neck
  // Set the y location of the neck relative to the size of the
  // other components
  float myNeckY = -1 * (myBodyHeight + myNeckHeight + myHeadRadius);
  stroke(255); // White
  line(30, -myBodyHeight, 10, myNeckY);

  // Hair
  pushMatrix(); // Isolate translation and rotation
  // translate from the top of the head
  translate(44, myNeckY);
  // set myAngle to 1/90th of half way around
  float myAngle = -PI/444.0;
  for (int i = 0; i <= 444; i++) {
    // do these statement 99 times, rotating a bit each time
    line(444, 0, 0, 0);
    rotate(myAngle);
  }
  popMatrix(); // end isolation of translate and rotate

  // Head
  noStroke();
  // Red head circle
  fill(0); // Red
  ellipse(12, myNeckY, myHeadRadius, myHeadRadius);
  // White eyeball
  fill(255); // White
  int eyeSize = 200;
  ellipse(12, myNeckY-12, eyeSize, eyeSize);
  // Green Iris
  fill(0, 200, 200); // Green
  ellipse(24, myNeckY-9, 66, 66);
  // Black pupil
  fill(0); // Black
  ellipse(20, myNeckY-2, 33, 33);
}

void keyPressed() {

  if (key == CODED) {
    if (keyCode == LEFT) {
      Xpos=Xpos-7;
    } else if (keyCode == RIGHT) {
      Xpos=Xpos+7;
    }
  }
}

```
