# Difference between revisions of "Blog 21"

Jump to: navigation, search

## !BETA!

This is used for The project:

Hardware used on Raspberry Pi:

Programmed with:

## Connecting the analog meters

While making the analog meter clock, I encountered the problem. That I had one Alternating current voltage. For this I had to

// blabla checked it out by putting the power resource after the diode.

To check the analog meter I connected it with a power resource to find out if at which voltage it went to it's maximum. Fast I found out that It went to it's maximum. To change that it went so fast to it's maximum I added two resistors. And removed the connection with the normal resistor. // blabla

To check if the meter you have work on the DIO. Put the minus part of your meter on the GND(Pin 1), and the positive side on the VCC(Pin 2). The pointer will directly go to it's to the right. ( If he doesn't you have to remove some resistors )

To connect the meters on the DIO: I did my positive cable on IO0, what is pin 3. ( The other cable has to go to GND Pin 1 ) On the wiki page of DIO you can see, which pin is what.

### Change analog meter value through the command line

Recommended to use the DIO protocol.

I have in my example the analog meter connected with pin 3(IO0).

Pin 3 is IO0, so it will get the first value and that is one.

To set pin 3 as output:

```bw_tool -I -D /dev/i2c-1 -a 84 -W 30:01
```

To enable the PWM:

```bw_tool -I -D /dev/i2c-1 -a 84 -W 5f:01
```

To let the pointer go to 50% of the analog meter:

```bw_tool -I -D /dev/i2c-1 -a 84 -W 50:80
```

The value is in hexadecimals so that is why 80 is 50%. If you did the commands above you can keep changing values if you still use the same pin(s).

If you are going to use a pin like pin 10(IO6). With the value 40 for register 30 and 5f. The reason it is 40 is, because the bits are in hexadecimals. So, 64 decimal bits gets calculated to 40 hexadecimals.

The reason why this is getting used, is because it is bit masked. With that you can add multiple pins in the command. So, if you you want pin 4(IO1) and pin 6(IO3) on:

```IO1 + IO3 -> 02 + 08 = 0A
```

For the full Bit Mask list of values you have to go to the DIO protocol

### Making the sticker

For making the sticker I first measured what the distance are from the the maximum and the minimum(0). I looked at the distance from horizontal and vertical. After that I made a picture of the meter. I used the program to make the lines an put the image in the background. So, I know where to put where. I had of course had to resize some part. After that I printed it out on a sticker paper, what I then put on my meter. It can be tricky work to do a sticker on the meter, so it is recommended to use a tweezers. ( Or something else where you can hold it good ) I used my fingers and that had the problem that my picture was not precisely placed.

## DIO Analog meter - Clock

With this script, I am going to make on the analog meter visible how late it it. For now I only have one analoge meter that works, so I will only show how late it's in hours. The script is pretty easy to understand, but I will still give some parts explanation.

This is the onexit script, What it does is when the script crashes or this function get named by the script. I will do the given commands and then exit the running script. So, in my case it turns the PWM back to zero on pin 3 IO0, which makes the pointer go back to the left. If you use a different Pin and don't know which one to use you have to look at the DIO Protocol page.

```trap onexit 1 2 3 15 ERR

function onexit() {
\$DIO -W 50:00 #Turns PWM back to zero
\$UI -W 10:00  #Clears screen
exit
}
```

This obviously looks at which hour it's and puts it in the directory name Hour.

```   Hour=`date +%H`
```

What then gets printed on the display.

```  \$UI -t "Clock=" \$Hour
```

The if statement looks at which hour there is given and looks if it is greater than the given value in \$Twelve. If it is 12 o’clock or later it will subtract 12 of it. So, for example 18 will be 6.

```  if [ "\$Hour" -gt "\$MAX" ]; then
Hour=\$(( \$Hour - \$MAX ))
fi
```

In the Array stands the amount of steps with the value it should give in hexadecimals.

```  array=(   00 17 2E 45 5C 73 8A A1 B8 CF E6 FF )
# Element 0  1  2  3  4  5  6  7  8  9  10 11

```

After that one the second row of the LCD display the value will be printed of the new hour ( if it is above 12 ) and the hexadecimal value in the array.

```  \$UI -W 11:20
\$UI -t "Hour=\$Hour Array="\${array[\$Hour]}
```

The full script:

```#!/bin/bash

DIO="bw_tool -I -D /dev/i2c-1 -a 84"
UI="bw_tool -I -D /dev/i2c-1 -a 94"
MAX="12"

trap onexit 1 2 3 15 ERR

function onexit() {
\$DIO -W 50:00 #Turns PWM back to zero
\$UI -W 10:00  #Clears screen
exit
}

while true; do
Hour=`date +%H`
#  Hour=\$((Hour + 6)) #I made this at 12:00, so I added 6 so I can see if the meter works

\$UI -W 10:00
\$UI -t "Clock=" \$Hour

if [ "\$Hour" -gt "\$MAX" ]; then
Hour=\$(( \$Hour - \$MAX ))
fi

array=(   00 17 2E 45 5C 73 8A A1 B8 CF E6 FD )
# Element 0  1  2  3  4  5  6  7  8  9  10 11

\$UI -W 11:20
\$UI -t "Hour=\$Hour Array="\${array[\$Hour]}

\$DIO -W 50:\${array[\$Hour]}

sleep 5
done
```

## DIO Analog meter - Adjustable Timer

In this script I made an adjustable timer. That will let the meter go from maximum PWM to zero. ( Pointer from right to left ) That you just use in the command line, with the given value after it. With the value you say how many seconds the script should be running. So, if you give it the value 30 it should look like this:

```./Scriptname 30
```

Here it will give the amount of seconds the date program knows and give it to the variable Start. The variable Seconds will get the value put after the script name (as in the example above with the value 30 ) (Idea: You can also change the Seconds value "\$1" with the amount of seconds you want. ) The end variable counts the seconds and start variable together. I use this later in the script, so that it then knows when it reached it's maximum value.

```Start=`date +%s`
Seconds="\$1"
End=\$((\$Seconds + \$Start))
```

The now variable gets in the while true list used to read every time to look at which second it is now. In progress the end(where the time ends) and now get counted of each other. The pwm value then gets calculated. What gives the percentage of 255 is used.

```Now=`date +%s`
Progress=\$((\$End - \$Now))
PWMValue=\$((\$Progress * \$MAX / \$Seconds))

```

Here is gives the hexadecimal output of the PWMValue. So the maximum value 255 would be FF.

```PWMValueHex=`(echo obase=16; echo \$PWMValue ) | bc`
```

Here the the result gets send to the DIO and to the user interface. What makes that the analog meter is going to move and that on the display is visible what hexadecimal value it is. ( The user interface part can be removed if not needed )

```\$DIO -W 50:\$PWMValueHex

\$UI -W 11:20
\$UI -t "Hex Value: " \$PWMValueHex

```

In the onexit it looks if the now value has gotten equal of higher then the given end value, where it should have stopped. When the end value is getting passed the onexit function is going to run. Otherwise it will just sleep 1 second, and after that look again if it reached it's point yet.

```if [ "\$Now" -ge "\$End" ]; then
onexit
fi

sleep 1
```

The full script:

```#!/bin/bash

DIO="bw_tool -I -D /dev/i2c-1 -a 84"
UI="bw_tool -I -D /dev/i2c-1 -a 94"
MAX="255"

Start=`date +%s`
Seconds="\$1"
End=\$((\$Seconds + \$Start))

trap onexit 1 2 3 15 ERR

function onexit() {
\$DIO -W 50:00 #Turns PWM back to zero
\$UI -W 10:00  #Clears screen
#mplayer Song.mp3
exit
}

while true; do
Now=`date +%s`
Progress=\$((\$End - \$Now))
PWMValue=\$((\$Progress * \$MAX / \$Seconds))

PWMValueHex=`(echo obase=16; echo \$PWMValue ) | bc`

\$DIO -W 50:\$PWMValueHex

\$UI -W 11:20
\$UI -t "Hex Value: " \$PWMValueHex

if [ "\$Now" -ge "\$End" ]; then
onexit
fi

sleep 1

done
onexit
```