Go Down

Topic: Increase a value, stupid question. (Read 2 times) previous topic - next topic


I'm new to Programming. After a hour of searching i didn't find a answer.
How can I raise a value on a key press?
I like to add (and on another button subtract) a 1 to the "steer"-value, to calibrate my steering for my RC-car which is attached via XBEE to Arduino.

I hope someone can help me.

btw: sorry for my bad english

Code: [Select]

/*  RC car project | Arduino UNO | zH32
import processing.serial.*;
import controlP5.*;
import procontroll.*;
import net.java.games.input.*;
import java.io.*;

ControlP5 controlP5;
ControllIO controll;
ControllDevice device;
ControllStick stick;
ControllStick stick1;
ControllButton button1;
ControllButton button;

Serial myPort;
void setup()
  myPort = new Serial(this, "COM32", 57600);

  PFont myFont;
  myFont = createFont("Arial", 20);
  controlP5 = new ControlP5(this);
  controlP5.addSlider("Lenkung in %",-100,100,50,10,50,400,40);
  controlP5.addSlider("Power in %",-100,100,100,30,150,40,400);
  controll = ControllIO.getInstance(this);
  device = controll.getDevice("Logitech Cordless RumblePad 2");
  ControllSlider sliderX = device.getSlider("Z-Achse");
  ControllSlider sliderY = device.getSlider("Z-Rotation");
  ControllSlider sliderX1 = device.getSlider("X-Achse");
  ControllSlider sliderY1 = device.getSlider("Y-Achse");
  stick = new ControllStick(sliderX,sliderY);
  stick1 = new ControllStick(sliderX1,sliderY1);

          // get right value for servo
  button = device.getButton("Taste 0");
  button1 = device.getButton("Taste 2");

void draw()

  String n1;
  String n2;
  String sending;
  int speed = int(stick.getY()*-255+255); // get right value for motor controller
  int steer = int((stick1.getX()*45)+90);
  n1 = Integer.toString(steer);
  n2 = Integer.toString(speed);
  // if n1 or n2 is less than 4 digits add a zero to the front of them
  while (n1.length() < 4){
    n1 = '0' + n1;
  while (n2.length() < 4){
    n2 = '0' + n2;
  //combine the two 4 digit long strings into one 8 digit long one
  sending = n1 + n2;
  //write this to the serial port.
  controlP5.controller("Lenkung in %").setValue((steer-90)*2.5);
  controlP5.controller("Power in %").setValue(map(speed, 0, 510, -100, 100));
  text("Sendestring: "+ sending, 800, 580);



How can I raise a value on a key press?

There are callback methods for key press events. You need to override the callback method OnKeyPress() to check for the specific keys of interest.

steer++; // increment the value
steer--; // decrement the value


this solves the problem:

Code: [Select]
void keyPressed() {
  if (keyCode == RIGHT){
  if (value == 0) {
    value ++;
  } else {
    value ++;
  if (keyCode == LEFT){
  if (value == 0) {
    value --;
  } else {
    value --;


Code: [Select]
if (value == 0) {
    value ++;
  } else {
    value ++;

What is the purpose of the if test when you do the same thing regardless of whether the condition is true or false?

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

shortcuts: alt+s submit/post or alt+p preview