Feedback on my code

Hi im new to processing and i have made this program (Its a timer/alarm)
here is the code (probably doesnt work because of the sound thing but to make it work remember to have the sound and put a file next to the code called ding.wav)

import processing.sound.*;

SoundFile file;
String audioName = "ding.wav";
String path;
String SS="S:";
String MM="M:";
String HH="H:";
int exit = 0;
int sound = 0;
int S = 0;
int M = 0;
int H = -1;
int SE = 1;
int ME = 0;
int HE = 0;
float NIn;

void setup(){
  size (400, 212);
  path = sketchPath(audioName);
  file = new SoundFile(this, path);
  NIn = 0;
}

void draw(){ 
  background(255,100,122);
  if (NIn == 1){
  } else {
  if (keyPressed){
  if (key == 't'){
  SE=61;
  ME=61;
  HE=2147483647;
  }
  }
  }
  if (SE==60){
  SE=1;
  }
  if (ME==60){
  ME=0;
  }
  if (HE==60){
  HE=0;
  }
  if (keyPressed) {
    if (key == 's' || key == 's') {
      SE+=1;
      delay(250);
    }
  }
  if (keyPressed) {
    if (key == 'm' || key == 'm') {
      ME+=1;
      delay(250);
    }
  }
  if (keyPressed) {
    if (key == 'h' || key == 'h') {
      HE+=1;
      delay(250);
    }
  }
  if (keyPressed) {
    if (key == ' ') {
      NIn+=1;
      H+=1;
    }
  }
  //5 min. = 300000
  if (H==HE){
    if (M==ME){
      if (S==SE){
        sound+=1;
        exit+=1;
      }
    }
  }
  if (keyPressed){
  if (key == ' '||key == ' '){
  Exit();
  }
  }
  
  if (sound==1){
  file.play();
  file.play();
  }
  if (S==60){
  S=0;
  M+=NIn;
  }
  if (M==60){
  M=0;
  H+=NIn;
  }
  if (NIn==1){
  S+=NIn;
  delay(1000);
  }

  {
  smooth();
  if (NIn==1){
  textSize(64);
  text(S,0,84);
  text(M,0,148);
  text(H,0,212);}
  textSize(20);
  text(SS,0,20);
  text(MM,45,20);
  text(HH,90,20);
  text(SE,17,20);
  text(ME,67,20);
  text(HE,112,20);}
}

void Exit(){
if (exit==1){
exit();}
}

And i really want some constructive criticisme
ps.
s to set how many seconds in the alarm
m to set how many minuets in the alarm
h to set how many hoursin the alarm
t to switch to timer
space to start when your done
hold space to close alarm when its done.

1 Like

First constructive criticism: make sure if you format your code when posting to forums (any forum for that matter). If you don’t know how to do it spend some time reading the pinned posts and check other examples.

Most people will not put to put time aside to help you if they get the impression that you are not making the effort yourself.

1 Like

Ok thx is there a way i can edit :slight_smile:

edit: Found it.

It seems you like you can invest some time learning about different data types. Read the following:

Based on that… a few tips:

  • If you need a variable to hold two values only, like to represent only “yes” or “no”, you can use the type boolean =>>>> boolean sound;, instead of int.

  • You don’t need to have all those variables. Instead of having separate variables for minutes, hours and seconds, you can use one variable to represent the time.

2 Likes

Ok Thx a bunch i really appreciate it :grin:

you’re welcome! let us know about your progress and others can give more tips.

@jonaspower – Some other suggestions:

  1. key == ' '||key == ' ' is checking the same thing twice

  2. If you are doing long groups of repetitive textual operations you can either combine the strings before printing them or wrap them up in a function. The string approach is “combine my letter, colon, and value into a single string, then print it with one text call.” The function approach is to write your own void textData(String label, int value, float x, float y) which combines those repeated offsets, font sizes, etc. Then call it like this:

textData(S, SS, 0, 84);
textData(M, MM, 0, 148);
textData(H, HH, 0, 212);
  1. Think about ways that you could use Processing’s built-in timing information, under the Time & Date section of the Processing reference – in particular, millis.
  1. This is more esoteric, but you might find it useful. Rather than creating your own Exit with a similar name (which can be confusing because methods are not normally capitalized – perhaps call it beforeExit?) you can simply redefine exit and call the original with super, like this:
void exit(){
  if (exit==1){
    super.exit();
  }
}

I see. Thanks a lot :grin: