Binary Clock with pi4j

80 views
Skip to first unread message

Sören Willrodt

unread,
Aug 19, 2014, 7:13:37 AM8/19/14
to pi...@googlegroups.com
Hello Guys,

I have a question:

I have coded a binary clock in java for the pi, but the LED's are not blinking.
The Pi4J examples let the LED's blink. So why does my code do not blink.

My source code:

import java.text.SimpleDateFormat;
import java.util.Date;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;

public class Main {

   
static Thread thread;
   
static boolean running = false;

   
static Date date;
   
static SimpleDateFormat sdf;

   
static String zeit;
   
static int[] array = new int[2];
   
static int stunden;
   
static int minuten;

   
   
public static void main(String[] args) throws InterruptedException {
       
// create GPIO-Controller
       
final GpioController gpio = GpioFactory.getInstance();
       
// create the Outputpins
       
final GpioPinDigitalOutput out[] = new GpioPinDigitalOutput[12];

       
// Stunden
       
out[0] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_10);
       
out[1] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_13);
       
out[2] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_11);
       
out[3] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_09);

       
// Minuten

       
out[4] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);
       
out[5] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07);
       
out[6] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_08);
       
out[7] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
       
out[8] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05);
       
out[9] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06);

       
// Am und Pm
       
out[10] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00);
       
out[11] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02);

       
if (thread == null) {
            thread
= new Thread();
            running
= true;
            thread
.start();
           
while (running = true) {

                date
= new Date();

                sdf
= new SimpleDateFormat("H:mm");
                zeit
= sdf.format(date);

               
String[] split = zeit.split(":");
                array
[0] = Integer.valueOf(split[0]);
                array
[1] = Integer.valueOf(split[1]);

                stunden
= array[0];
                minuten
= array[1];

               
if (stunden < 12) {

                   
out[10].isHigh();
                    thread
.sleep(10);
                   
out[10].isLow();

               
}

               
if (stunden > 12) {

                    stunden
-= 12;
                   
out[11].isHigh();
                    thread
.sleep(10);
                   
out[11].isLow();

               
}

               
System.out.println(zeit);
               
System.out.println("____________________");
               
System.out.println();
               
System.out.println("Stunden: " + array[0]);
               
System.out.println("Minuten: " + array[1]);
               
System.out.println();

               
for (int i = 0; i < 4; i++) {
                   
boolean bit = isBit(i, stunden);
                   
if (bit == true) {
                       
out[i].isHigh();
                        thread
.sleep(10);
                       
out[i].isLow();

                   
}

               
}
               
for (int a = 4, b = 0; a < 10; a++, b++) {
                   
boolean bit = isBit(b, minuten);
                   
if (bit == true) {
                       
out[a].isHigh();
                        thread
.sleep(10);
                       
out[a].isLow();

                   
}

               
}
           
}

       
}

   
}

   
public static boolean isBit(int pos, int value) {

       
int mask = 1 << pos;
       
return (value & mask) == mask;

   
}

}

I am a german Student, so don't wonder about some German words.

Stunde=hour
Minute = minute

I hope, that somebody can help me

Sören

Peter Skopek

unread,
Aug 19, 2014, 9:51:18 AM8/19/14
to Sören Willrodt, pi...@googlegroups.com
Problem with your code is that Thread's sleep method is taking milliseconds and using 10 as argument is quite a short stop that might make LEDs look like still on. Use 500 or 1000 to see a difference.

Peter

BTW: Thread's sleep is static method, so no need to create new Thread and start it, without adding any Runnable or redefining run method.
Just change all thread.sleep(10); lines to Thread.sleep(something_else); and delete all line with thread variable.


<π/>


--
You received this message because you are subscribed to the Google Groups "Pi4J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pi4j+uns...@googlegroups.com.
Visit this group at http://groups.google.com/group/pi4j.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi4j/915ad154-9885-4f51-8616-8606d69ecdac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Sören Willrodt

unread,
Aug 21, 2014, 3:05:39 AM8/21/14
to pi...@googlegroups.com, soeren....@gmail.com
my new Code is:
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;
 
public class Main {
 
   
static Thread thread;
   
static boolean running = false;
 
   
static Date date;
   
static SimpleDateFormat sdf;
 
   
static String zeit;
   
static int[] array = new int[2];
   
static int stunden;
   
static int minuten;

 
   
/**
     * Create the frame.
     *
     * @throws InterruptedException
     */

   
public static void main(String[] args) throws InterruptedException {
       
// create GPIO-Controller
       
final GpioController gpio = GpioFactory.getInstance();
       
// create the Outputpins
       
final GpioPinDigitalOutput out[] = new GpioPinDigitalOutput[12];
 
       
// Stunden
       
out[0] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_10);
       
out[1] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_13);
       
out[2] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_11);
       
out[3] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_09);
 
       
// Minuten
 
       
out[4] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);
       
out[5] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07);
       
out[6] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_08);
       
out[7] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
       
out[8] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05);
       
out[9] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06);
 
       
// Am und Pm
       
out[10] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00);
       
out[11] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02);

 
   
while (true){

 
                date
= new Date();
 
                sdf
= new SimpleDateFormat("H:mm");
                zeit
= sdf.format(date);
 
               
String[] split = zeit.split(":");
                array
[0] = Integer.valueOf(split[0]);
                array
[1] = Integer.valueOf(split[1]);
 
                stunden
= array[0];
                minuten
= array[1];
 
               
if (stunden < 12) {
 
                   
out[10].isHigh();

                   
Thread.sleep(500);

                   
out[10].isLow();
 
               
}
 
               
if (stunden > 12) {
 
                    stunden
-= 12;
                   
out[11].isHigh();

                   
Thread.sleep(500);

                   
out[11].isLow();
 
               
}
 
               
System.out.println(zeit);
               
System.out.println("____________________");
               
System.out.println();
               
System.out.println("Stunden: " + array[0]);
               
System.out.println("Minuten: " + array[1]);
               
System.out.println();
 
               
for (int i = 0; i < 4; i++) {
                   
boolean bit = isBit(i, stunden);
                   
if (bit == true) {
                       
out[i].isHigh();

                       
Thread.sleep(500);

                       
out[i].isLow();
 
                   
}
 
               
}
               
for (int a = 4, b = 0; a < 10; a++, b++) {
                   
boolean bit = isBit(b, minuten);
                   
if (bit == true) {
                       
out[a].isHigh();

                       
Thread.sleep(500);

                       
out[a].isLow();
 
                   
}
 
               
}
}
           
}
 
         
 
   
 
   
public static boolean isBit(int pos, int value) {
 
       
int mask = 1 << pos;
       
return (value & mask) == mask;
 
   
}
 
}

 but the lights are still off...

Do you have another idea, why the program is still not working

Stefan Schildbach

unread,
Aug 21, 2014, 1:05:00 PM8/21/14
to pi...@googlegroups.com
Hey Sören,

the primary problem is, that you're using the wrong methods to trigger the gpio pins.
Using "isHight()" and "isLow()" only returns the current state.
You may want to call one of the mothods: "high()", "low()", "trigger()" or "setPinState(...)" to modify the current pin state.

As an example, you would replace
out[a].isHigh();
Thread.sleep(500);
out[a].isLow();

with

out[a].high();
Thread.sleep(500);
out[a].low();

Stefan

               
System.out<span style="color: #660;" class="styled-by-prett
...

Sören Willrodt

unread,
Aug 21, 2014, 1:56:22 PM8/21/14
to pi...@googlegroups.com
Oh damn,

so i should read the documentation better

thank you so much

Am Dienstag, 19. August 2014 13:13:37 UTC+2 schrieb Sören Willrodt:

               
System.out<span style="color: #660;" class="styled-by-prett
...

Sören Willrodt

unread,
Aug 21, 2014, 2:24:50 PM8/21/14
to pi...@googlegroups.com
But there is an new problem:

the console says:
pi@raspberrypi~$  sudo java -classpath .:classes:/opt/pi4j/lib/'*' Main

wiringPiSetup: mmap failed: Datei oder Verzeichnis nicht gefunden
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Main.main(Main.java:45)

here is the updated source code:

import java.text.SimpleDateFormat;
import java.util.Date;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;

public class Main {

   
final static GpioController gpio = GpioFactory.getInstance();
   
final static GpioPinDigitalOutput out[] = new GpioPinDigitalOutput[12];


   
public static void main(String[] args) throws InterruptedException {

       
// Stunden
       
out[0] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_10);
       
out[1] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_13);
       
out[2] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_11);
       
out[3] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_09);

       
// Minuten

       
out[4] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01);
       
out[5] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07);
       
out[6] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_08);
       
out[7] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
       
out[8] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05);
       
out[9] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06);

       
// Am und Pm
       
out[10] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00);
       
out[11] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02);


       
while (true) {

           
Date date = new Date();

           
SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
           
String zeit = sdf.format(date);
           
int[] array = new int[1];
           
String[] split = new String[1];

                    split
= zeit.split(":");
            array
[0] = Integer.valueOf(split[0]);
            array
[1] = Integer.valueOf(split[1]);


           
int stunden = array[0];
           
int minuten = array[1];

           
if (stunden < 12) {

               
out[10].high();


           
}

           
if (stunden > 12) {

                stunden
-= 12;

               
out[11].high();


           
}
           
for (int i = 0; i < 4; i++) {
               
boolean bit = isBit(i, stunden);
               
if (bit == true) {

                   
out[i].high();

               
}

           
}
           
for (int a = 4, b = 0; a < 10; a++, b++) {
               
boolean bit = isBit(b, minuten);
               
if (bit == true) {

                   
out[a].high();
                   
System.out.println("minuten");
               
}

           
}
           
for (int b = 0; b < out.length; b++) {
               
Thread.sleep(100);
               
out[b].low();
           
}
       
}

   
}

   
private static boolean isBit(int pos, int value) {

       
int mask = 1 << pos;
       
return (value & mask) == mask;
   
}
}

Sören Willrodt

unread,
Aug 22, 2014, 3:26:12 AM8/22/14
to pi...@googlegroups.com
So I need no more help it is running :D
e code:

                   
System.out.println(<span style="color: #080;" class="style
...
Reply all
Reply to author
Forward
0 new messages