# Digital Clock Made from image

Hi!
I am very very new to processing. I did a few classes with the basics but I cannot wrap my head around it.

I am trying to make a clock from pictures of numbers that I have taken.
I have pictures of 0,1, 2, 3 … 9 and I would like to make a functioning clock where the images change according to the actual time. Kind of like four slide shows next to each other changing independently.

So, let’s say the time is 13:10 I would have an image of 1, 3, 1 and 0.

I think I can figure out how to place the images and make a slide show but I am not sure how to code it to the actual time, or assign an image to a minute or hour. I would appreciate any help or direction.

Thanks!

you could store you images in an array and address them by using the hour and minutes functions built into processing i’ll only include the hour function as it contains an example of minutes and seconds as well (i know you’re not using seconds but still nice to have the option)

hour

so basically you would take the hour and

``````  /this is terrible but gives you an idea of what to do for each digit
//to get an index into your image array

//get the hour
String h = str(hour());
//get the first digit of the hour string as an int to use as an index into the image array
int h1 = int(str(h.charAt(0)));
//get the second digit of the hour string as an int to use as an index into the image array
int h2 = int(str(h.charAt(1)));

//test we have the correct values
println("h1: " + h1);
println("h2: " + h2);
``````

This is the Array I have, I don’t understand how to combine this array with your suggestion. could you please explain me a bit more ho to get from the array to the string?

I thought I will attach a screenshot from a mock-up I made in photoshop to illustrate what I mean.

PImage p0, p1, p2, p3, p4, p5, p6, p7, p8, p9;
PImage [] picArray = new PImage;

void setup (){
size(900,600);
picArray = p0;
picArray = p1;
picArray = p2;
picArray = p3;
picArray = p4;
picArray = p5;
picArray = p6;
picArray = p7;
picArray = p8;
picArray = p9;

}

Assuming you have your images in the data folder of your sketch you can just load the images as shown below

``````PImage[] digits;

void setup() {
digits = new PImage;
for (int i = 0; i < 10; i++) {
//making the assumption that the images are just called 0-9.png
digits[i] = loadImage("data/" + i + ".png");
}
}

//and just to demonstrate drawing the hour. i haven't tested this but i'm fairly sure it's correct
void draw() {
background(255);
//get the hour
String h = str(hour());
//get the first digit of the hour string as an int to use as an index into the image array
int h1 = int(str(h.charAt(0)));
//get the second digit of the hour string as an int to use as an index into the image array
int h2 = int(str(h.charAt(1)));

image(digits[h1], 0, 0);
image(digits[h2], digits[h1].width, 0);
}

``````

Cool I will dig into that. Thank you!

just a note if you are going with 24hr time you might need to pad the digits you get in the am as I think the hour function returns 0-23 or something along those lines so when it returns 9 as the hour the string will only contain one character and you would end up with an out of bounds error it’s probably best just to replace

this

``````String h = str(hour());
``````

with this

``````String h = nf(hour(), 2);
``````

it’s just to ensure there is always 2 digits to reference.

thank you, I will replace that. I am trying to do the minutes now. Ok fantastic! its working.
I have one more question. What if I have two sets of numbers: one green and one white and I would like to alternate them like on the image above should I do a new
“digits” set?

Solved ! @hotfooted Thank you again!

no worries. there are heaps of ways you could do all of this and the demo code above is far from great. just a note with wanting to swap between sets of images it might be easier to do something like this

``````PImage digitsStrip;
int digitWidth;
int digitHeight;
void setup() {
size(640, 480, P2D);
digitWidth = 52;
digitHeight = 64;
}

void draw() {
background(0);
//get the hour
String h = nf(hour(), 2);
//get the first digit of the hour string as an int to use as an index into the image array
int h1 = int(str(h.charAt(0)));
//get the second digit of the hour string as an int to use as an index into the image array
int h2 = int(str(h.charAt(1)));

//now instead of using h1 and h2 as references into an array you use them as offsets
//into the digitsStrip image just note that .get() is slow when you are doing many things
//and there are alternatives to this method it's just simple and clean with the trade off
//of performance
image(digitsStrip.get(h1 * digitWidth, 0, digitWidth, digitHeight), 0, 0);
image(digitsStrip.get(h2 * digitWidth, digitHeight, digitWidth, digitHeight), digitWidth, 0);
}
``````

and this is the digitsStrip image used in the example anyways best of luck with it all