Creating a digital clock that cycles through numbers randomly

I made a clock sketch that displays the time from my computer, but I was wondering if there was a way I could use arrays to make the numbers cycle through at random. (My knowledge of processing is sort of limited at the moment)

My code for my current project is here

DigitalClock digitalClock;

void setup() {
  size(1080, 720);
  digitalClock = new DigitalClock(40, width/2, height/2+15); 
}

void draw() {
  background(0);
  digitalClock.getTime();
  digitalClock.display();
}

class DigitalClock extends Clock {
  int fontSize;
  float x, y;

  DigitalClock(int _fontSize, float _x, float _y) {
    fontSize = _fontSize;
    x = _x;
    y = _y;
  }

  void getTime() {
    super.getTime();
  }

  void display() {
    textSize(100);
    textAlign(CENTER);
    text (h + ":" + nf(m, 2) + ":" + nf(s, 2), x, y);
  } 
}

class Clock {
  int h, m, s;
  Clock() {
  }

  void getTime() {
    h = hour();
    m = minute();
    s = second();
  }
}
Tagged:

Answers

  • Sorry, just to clarify, you want to just have random numbers be put on the clock everytime it updates?

  • Yeah, like I want the format to stay looking like a clock, but I wanted the actual numbers to cycle through 1-9 at random, like the clock is haphazardly running out of control. Maybe somehow I could use an array or the random() coding?

  • edited November 2017 Answer ✓
    DigitalClock digitalClock;
    
    void setup() {
      size(1080, 720);
      digitalClock = new DigitalClock(40, width/2, height/2+15); 
    }
    
    void draw() {
      background(0);
      digitalClock.randomizeTime();
      digitalClock.display();
    }
    
    class DigitalClock extends Clock {
      int fontSize;
      float x, y;
    
      DigitalClock(int _fontSize, float _x, float _y) {
        fontSize = _fontSize;
        x = _x;
        y = _y;
      }
    
      void getTime() {
        super.getTime();
      }
    
      void display() {
        textSize(100);
        textAlign(CENTER);
        text (h + ":" + nf(m, 2) + ":" + nf(s, 2), x, y);
      } 
    }
    
    class Clock {
      int h, m, s;
      Clock() {
      }
    
      void getTime() {
        h = hour();
        m = minute();
        s = second();
      }
    
      void randomizeTime() {
       h = (int) random(24); 
       m = (int) random(60);
       s = (int) random(60);
      }
    }
    

    Code I changed - digitalClock.randomizeTime();
    And I added the randomizeTime() method

    It's probably not the exact effect you want but hopefully it's a starting place!

  • That's pretty darn close! Thank you so much! One last noob question tho: how do I slow down the rate at which the clock randomizes?

  • Never mind, I just adjusted the frame rate! Bless you and thank you for helping me out!

  • Yeah no problem! Additionally, if you wanted to keep the framerate the same, you could say

    if (frameCount % 2 == 0)
      digitalClock.randomizeTime();
    

    in the draw loop, which would make it change every other frame! Just another idea :)

Sign In or Register to comment.