Help for a beginner please (asking for input a multi-digit variable)

Hello everyone,
I’m quite new with coding and I would like to create a prime number detector using arduino.
I have a pretty good idea how to do it using a divider variable and modulos.

My problem is : how can I create a type in for a several digits number I can convert to a variable to be checked by the code ?

I tried to create a code with keypressed and tried to play a little with variable but I can’t get a typed-in multi digits variable. how can I do this ??

thanks a lot for your help

Fred

First Code below

void draw(){
background(255);
println(num);

text(“CHIFFRE ENTRE 1-9”,80,100);

fill(255,0,0);

if (keyPressed) {text(“NOMBRE “+num,100,180); text(num,150,250); text(” X “,200,250);text(multi,250,250);text(”=”,300,250);text(multi*num,350,250);

multi++;frameRate(1);}

else {multi=1;}

}

void keyPressed(){
int keyNum = Character.getNumericValue(key);
num = keyNum;
}

You need to do a few things;

  • 1st: allow only numbers to be entered
  • 2nd: add a function (like if enter is pressed) to return the current text and reset the input
  • 3rd: add a backspace function (press backspace to erase the last number)

here is my code for reference

Code
char acceptable[] = ("0123456789").toCharArray();
String text = "";
void setup() {
  size(600,600);
  textAlign(3,3);
}
void draw() {
  background(0);
  text(text,300,300);
}
void keyPressed() {
  if(keyCode == ENTER) {
    println(text);
    text = "";
  } else if(keyCode == BACKSPACE) {
    String newTxt = "";
    for(int i = 0; i < text.length()-1; i++) {
      newTxt += text.charAt(i);
    }
    text = newTxt;
  } else {
    for(int i = 0; i < acceptable.length; i++) if(acceptable[i] == key) text+=key;
  }
}

here is a version which makes the default input 0

V2
char acceptable[] = ("0123456789").toCharArray();
String text = "0";
void setup() {
  size(600,600);
  textAlign(3,3);
}
void draw() {
  background(0);
  text(text,300,300);
}
void keyPressed() {
  if(keyCode == ENTER) {
    println(text);
    text = "0";
  } else if(keyCode == BACKSPACE) {
    String newTxt = "";
    for(int i = 0; i < text.length()-1; i++) {
      newTxt += text.charAt(i);
    }
    text = ((newTxt.length() == 0)? "0" : newTxt);
  } else {
    for(int i = 0; i < acceptable.length; i++) if(acceptable[i] == key) text = (((text.equals("0"))? key+"" : (text + key)));
  }
}

Oh bad, I was working on something too…

:wink:


final int INPUT=0;
final int CALC=1; 
int state = INPUT;

String numInput=""; 
int num=0, multi=3;

void setup() {
  size(660, 660);
}

void draw() {
  background(255);
  //println(num);

  fill( 255, 0, 0);
  text("CHIFFRE ENTRE, submit with Enter: ", 80, 100);

  fill(2, 0, 255);
  text(numInput+caret(), 280, 100); 

  if (! numInput.equals("")&&state == CALC) {

    text("NOMBRE "+num, 100, 180); 
    text(num, 150, 250); 
    text(" X ", 200, 250);
    text(multi, 250, 250);
    text("=", 300, 250);
    text(multi*num, 350, 250);

    // multi++;
    //  frameRate(1);
    //} else {
    //  multi=1;
    //}
  } else {
  }
}

void keyPressed() {
  if (key==ENTER||key==RETURN) {
    //submit
    num = 
      int(numInput);
    state = CALC; 
    return;//leave
  }

  if (key==ESC) {
    state=INPUT; 
    key=0;
    return;
  }

  if (key==BACKSPACE) {
    if (numInput.length()>0) 
      numInput=numInput.substring(0, numInput.length()-1); 
    return;
  }

  if (key==CODED)
    return; // leave 

  // int keyNum = Character.getNumericValue(key);
  numInput += key;
}

String caret() {
  // Blinking cursor: return | or blank
  if (millis() % 1000 < 500) 
    return "|";
  else 
  return " ";
}

1 Like

Wow that’s really heavy codes for me, I need to take my time reading and translate each line.

I will work on theses nex concepts

Thanks a lot for your help !!

1 Like

Hello again,
I designed this code with your ideas… I think it’s working but it’s still very ugly for now.
I wonder there’s a lot of unuseful stuff inside this code but I didn’t understand everything yet.

here’s the code, could be fun or useful

Thanks again

final int INPUT=0;
final int CALC=1;
int state = INPUT;
int modulo;

String numInput="";
int num=0;

void setup() {
size(660, 660);
}

void draw() {
background(255);
//println(num);

fill( 255, 0, 0);
text("CHIFFRE ENTRE, submit with Enter: ", 80, 100);

fill(2, 0, 255);
text(numInput+caret(), 280, 100);

if (! numInput.equals("")&&state == CALC) {

text(num, 100, 180);for (int diviseur = 2; diviseur <num; diviseur++) {frameRate(1);modulo = num%diviseur;print(diviseur);print("   ");println(modulo);

                                                                            if (modulo==0) {background(255);text(num, 100, 180);text("pas premier/not a prime",200,300);text(diviseur,400,300);
                                                                                            text("x",450,300);text((num/diviseur),500,300);noLoop();}

} }}

void keyPressed() {
if (key==ENTER||key==RETURN) {
//submit
num =
int(numInput);
state = CALC;
return;//leave
}

if (key==ESC) {
state=INPUT;
key=0;
return;
}

if (key==BACKSPACE) {
if (numInput.length()>0)
numInput=numInput.substring(0, numInput.length()-1);
return;
}

if (key==CODED)
return; // leave

// int keyNum = Character.getNumericValue(key);
numInput += key;
}

String caret() {
// Blinking cursor: return | or blank
if (millis() % 1000 < 500)
return “|”;
else
return " ";
}

1 Like

hello again,

I found a quite strange problem.

I found a way to create a prime numbers detectors

my problem is i succeeded to create a condition for a non prime number but after many attemps I can’t find the right condition to give to the code for recognize a prime number only.the code freeze.

do you have any idea ?? thanks a lot

final int INPUT=0;
final int CALC=1;
int state = INPUT;
int modulo;

String numInput="";
int num=0;

void setup() {
size(800, 500);
}

void draw() {
background(0);textSize(50);textAlign(CENTER);

fill( 0, 255, 0);
text(“Entrez un nombre”, 400, 170);

fill(255, 130, 0);
textSize(80); text(numInput+caret(),400, 250);

if (! numInput.equals("")&&state == CALC) {

for (int diviseur = 2; diviseur <num; diviseur=diviseur+1) {modulo = num%diviseur;print(num);print(" “);print(diviseur);print(” ");println(modulo);

                                                                            if (modulo==0) {background(0);
                                                                           
                                                                                 textSize(180);fill(0, 255, 0);text(num,400,200 );
                                                                                 textSize(80);fill(255, 130, 0);text("n'est pas premier",400 ,290);
                                                                                 textSize(120);fill(0, 255, 0);text(diviseur+"x"+(num/diviseur),400,450);
                                                                                 textSize(20);text(diviseur+"x"+(num/diviseur)+"="+num,700,400);
                                                                                 textSize(20);fill(100);text((diviseur+1)+"x"+(num/diviseur)+"="+((diviseur+1)*(num/diviseur)),700,420);
                                                                                                        text((diviseur+2)+"x"+(num/diviseur)+"="+((diviseur+2)*(num/diviseur)),700,440);
                                                                                                        text((diviseur+3)+"x"+(num/diviseur)+"="+((diviseur+3)*(num/diviseur)),700,460);
                                                                                                        text((diviseur+4)+"x"+(num/diviseur)+"="+((diviseur+4)*(num/diviseur)),700,480);
                                                                                                        text((diviseur-1)+"x"+(num/diviseur)+"="+((diviseur-1)*(num/diviseur)),700,380);
                                                                                                        text((diviseur-2)+"x"+(num/diviseur)+"="+((diviseur-2)*(num/diviseur)),700,360);
                                                                                                        text((diviseur-3)+"x"+(num/diviseur)+"="+((diviseur-3)*(num/diviseur)),700,340); }

} }}

void keyPressed() {
if (key==ENTER||key==RETURN) {
//submit
num =
int(numInput);
state = CALC;
return;//leave
}

if (key==ESC) {
state=INPUT;
key=0;
return;
}

if (key==BACKSPACE) {
if (numInput.length()>0)
numInput=numInput.substring(0, numInput.length()-1);
return;
}

if (key==CODED)
return; // leave

// int keyNum = Character.getNumericValue(key);
numInput += key;
}

String caret() {
// Blinking cursor: return | or blank
if (millis() % 1000 < 500)
return “|”;
else
return " ";
}