Difference between revisions of "Blog 14"

From BitWizard WIKI
Jump to: navigation, search
(Arduino version)
(Raspberry Pi version)
Line 44: Line 44:
 
   
 
   
 
  done
 
  done
 +
 +
 +
OnTime=1
 +
OffTime=0
 +
 +
With OnTime and OffTime you can easily change the time of the relay being on or off. In this way you don't have to be searching in the script, where to change it.
 +
 +
bigrelay="bw_tool -s 50000 -a 9c"
 +
 +
This is for saying that with 'bigrelay' it has to do a command to the bigrelay.
 +
 +
trap onexit 1 2 3 15 ERR
 +
 +
function onexit() {
 +
  $bigrelay -W 10:0:b
 +
  exit
 +
}
 +
 
 +
This is an onexit script, which makes it happen that when you leave the script with ctrl + c or an error that it will directly put all the relays in the off-statement. 
 +
 +
function dorelay() {
 +
  $bigrelay -W "$1":01:b
 +
  sleep $OnTime
 +
  $bigrelay -W "$1":00:b
 +
  sleep $OffTime
 +
 +
Here it will read the turn on and off times, the last given relay number and will perform them.
 +
 +
  while true; do
 +
 +
  for i in `seq 20 25` ; do
 +
    dorelay $i
 +
  done
 +
 +
Here it will give to the variable i +1 up and will run activate the doreplay function.
  
 
== Arduino version ==
 
== Arduino version ==

Revision as of 14:08, 23 October 2015

The BigRelay board

In this post I will show my scripts for Raspberry Pi and Arduino, where I will let one after another Relay turn on and the previous turn off. This can be a handy script, if you want to give a quick look if all the relays work.

Hardware I used for Raspberry Pi:

Hardware I used for Arduino:

  • SPI_LCD board(LCD)

Raspberry Pi version

The script of the bash program:

#!/bin/bash

OnTime=1
OffTime=0 

bigrelay="bw_tool -s 50000 -a 9c" 

trap onexit 1 2 3 15 ERR 

function onexit() {
  $bigrelay -W 10:0:b
  exit
} 

function dorelay() {
  $bigrelay -W "$1":01:b
  sleep $OnTime
  $bigrelay -W "$1":00:b
  sleep $OffTime
} 

 while true; do

  for i in `seq 20 25` ; do 
    dorelay $i 
  done

done


OnTime=1
OffTime=0 

With OnTime and OffTime you can easily change the time of the relay being on or off. In this way you don't have to be searching in the script, where to change it.

bigrelay="bw_tool -s 50000 -a 9c" 

This is for saying that with 'bigrelay' it has to do a command to the bigrelay.

trap onexit 1 2 3 15 ERR 

function onexit() {
  $bigrelay -W 10:0:b
  exit
} 
 

This is an onexit script, which makes it happen that when you leave the script with ctrl + c or an error that it will directly put all the relays in the off-statement.

function dorelay() {
  $bigrelay -W "$1":01:b
  sleep $OnTime
  $bigrelay -W "$1":00:b
  sleep $OffTime

Here it will read the turn on and off times, the last given relay number and will perform them.

 while true; do

  for i in `seq 20 25` ; do 
    dorelay $i 
  done

Here it will give to the variable i +1 up and will run activate the doreplay function.

Arduino version

The script from the arduino program:

int offms = 500;
int onms = 1000;

void loop (void) 
{
  
  static int num = 0x20;
  static unsigned char s;
  char buf[17];

  set_var (0x9c, 0x10, 0x00);
  delay(offms);
  set_var (0x9c, num, 0x01);
  delay(onms);

  if ( num < 0x25) num++;
  else {
   num = 0x20; s++;
  }   

  sprintf (buf, "Times: %02d Relay: %02x", s, num);
  write_at_lcd (0, 1, buf);
  Serial.write (buf);
  Serial.write ("\r\n"); 
}

This is not the full script! I used a script named ardemo_lcd.pde, but I only replaced the void loop part. ( that is the part you are seeing on this page )

int offms = 500;
int onms = 1000;

Here you can set the time in milliseconds how long you the relay to turn on and off. You coud also change the numbers in delay, but this makes it easier to find.

static int num = 0x20;

The above line will make it happen that when you upload the code it will directly start with relay 1(0x20).

set_var (0x9c, 0x10, 0x00);
delay(offms);
set_var (0x9c, num, 0x01);
delay(onms);

It says to BigRelay (0x9c) that register 10(0x10) has to be out (0x00). So in other words all the relays on the BigRelay have to be turned off. ( You can also change register 10 into num, and put it under the turn on command. )

offms, onms and num will read the last values that were given to them. So at num when counting up it will become 21.

if ( num < 0x25) num++;
else {
 num = 0x20; s++;
}   

Here I let num couting up with 1 every time a relay has been activated. After it reached above 25(0x25) it will start over again at 20(0x20). After it begins again with 20(0x20) I also count up with 1 s, this will be send to the display to say how many times it finished a full round clicking.

sprintf (buf, "Times: %02d Relay: %02x", s, num);
write_at_lcd (0, 1, buf);
Serial.write (buf);
Serial.write ("\r\n");

Here it says to the display to print the text "Times:" ( for showing how many times it did all the relays ) and "relay:" ( for showing which relay is turned on ). The %02d is for showing the numbers in decimals and %02x is for showing in hexadecimals. ( without 0x before it )