__⌂__

Code Library

Here are the code links for contemplative computing. The diagram below gives some idea of how the code pieces work in tandem with one another (click to enlarge)

Code

bootscript.pl This script launches ‘fancontrol.pl’ and fanserver_02.pl and backgrounds both processes

#!/usr/bin/perl

`sudo rm /var/run/fancontrol.pid`;
sleep(1);
`sudo chown -R finalproject /sys/class/hwmon/hwmon1/device/pwm2`;
# change permissions of pwm chips
sleep 2; # this length of sleep time can be reduced as desired

sleep(1);
$server = system 'perl fanserver_02.pl'; # boot server
sleep(1);
$fan = system "perl fancontrol.pl"; # boot fancontrol loop script
print "fan commence\n"

fancontrol.pl This script runs the fancontrol software continuously and reinitiates should a currently running fancontrol script be terminated

## this script works with fanserver.pl
## This script keeps the fancontrol process running and will restart the
## fancontrol process should it be terminated. In conjunction with fanserver
## this allows for modulation of the CPU fan speed

#!/usr/bin/perl
use strict;
use Time::HiRes qw(usleep);

sub fan {
my $initiate_fan = system "fancontrol"; # uses system to trigger an instance of fancontrol;
# system the only appropriate pseudocall to utilise if ceasing this instance via kill calls
# as utilised by the fanserver
}

while (1) { # infinite loop, this script runs in the background, parallel to fanserver
print "start fan\n";
fan(); # initiate subroutine
sleep (5); # if the fancontrol is stopped, wait five seconds then reboot fancontrol
# 5 seconds sleep can be reduced
print "restart fan\n";
}

fanserver_02.pl This server modifies the fancontrol configuration file based on instructions from ‘arduino_server_client07.pl’ and in so doing modulates the speed of the CPU fan

#!/usr/bin/perl

## server script which works with fancontrol.pl, together they modulate the speed of the fan
## this server overwrites the fancontrol parameter file, then ceases the currently running
## fancontrol instance. fancontrol.pl then restarts with the new values and RPM

package FanServer;
{
use Switch;
@ISA = qw(Net::Server::Fork );
use strict;
use Net::Server::Fork;    # any personality will do
use Device::SerialPort;
use Time::HiRes qw(usleep);
my @testvalues = (453, 356, 987, 634, 143, 237, 378);
my %megaval = (
pin_val1 => [],
pin_val2 => [],
);
FanServer->run( conf_file => "FANServer.conf" );
close_port();
exit;

## Globals

my $cmd = "cp -f ";
my $source_slow = " /home/finalproject/fanserver/fanslow/fancontrol "; # neither global nor constant assignment resolved
my $source_med = "/home/finalproject/fanserver/fanmedium/fancontrol ";
my $source_fast = "/home/finalproject/fanserver/fanfast/fancontrol ";
my $dest = " /etc/fancontrol ";

# Demonstrate a Net::Server style hook
sub allow_deny_hook {
my $self = shift;
my $prop = $self->{server};
my $sock = $prop->{client};

# only local connect
return 1 if $prop->{peeraddr} =~ /^127\./;
die "only local connect";
return 0;
}

# Another Net::Server style hook
sub request_denied_hook {
print "Go away!\n";
print STDERR "DEBUG: Client denied!\n";
}

sub run_dequeue {
my $self = shift;

# server calls this repeatedly
$self->log( 1, "received request dequeue search" );

#`find / -iname toot &`;

#my $self->set_property( key1 => 'val1' );
return;
}

sub process_request {
my $self = shift;
eval {
while (<STDIN>) {
s/\r?\n$//;
$_ =~ m/(.*).*/; # TYPE has been removed from server communication in order to assist construction of client
my $type      = $1;
$self->log( 1,	"'$type'");
if($_ =~m/quit/){$self->server_close;}
switch ($type) {
case "PWM2" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM2/fancontrol /etc/fancontrol`;
print "changing speed\n";
print $swap_fan_speed;
}

case "PWM5" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed =  `cp -f /home/finalproject/fanserver/PWM5/fancontrol /etc/fancontrol`;
print "changing speed\n";
print $swap_fan_speed;
}

case "PWM7" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed =  `cp -f /home/finalproject/fanserver/PWM7/fancontrol /etc/fancontrol`;
print "changing speed\n";
print $swap_fan_speed;
}

case "PWM10" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM10/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM13" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM13/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM14" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM14/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM15" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM15/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM17" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM17/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM19" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM19/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM20" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM20/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM22" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM22/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM25" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM25/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM27" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM27/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM30" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM30/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM33" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM33/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM35" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM35/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM40" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM40/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM45" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM45/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM50" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM50/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM60" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM60/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM70" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM70/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM95" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM95/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM125" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM125/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM180" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM180/fancontrol /etc/fancontrol`;
print "changing speed $swap_fan_speed\n";
}

case "PWM254" {
print "halting fan\n";
stop_fan();
usleep (100000);
my $swap_fan_speed = `cp -f /home/finalproject/fanserver/PWM254/fancontrol /etc/fancontrol`;
print "changing speed\n";
print $swap_fan_speed
}

# Close the server
case "quit"        { $self->server_close }
else               {
print "You said \"$_\"\r\n";
#         alarm($timeout);
}
}#end case
#  alarm($previous_alarm);
} ;#while
warn $@ if $@;
} #eval
}

sub stop_fan {
my $findPID = `ps a | grep /sbin/fancontrol`; # looks for the fancontrol, ps a provides easiest filter
my @PIDs = split(' ', $findPID); # uses split, first array element is now the PID number
my $PID = $PIDs[0];
print "$PID\n"; # to test that the PID killed is the desired one
my $kill = system "kill -2 $PID"; # kills the PID, which will be fancontrol process as per the above
}

}1;

server_relaying_07.pl This server manages the data coming from the piezo monitoring arduino

#!/usr/bin/perl

## server script which is to compliment the client aiming to do lions share of hash heavy lifting

package LungServer;
{
use Switch;
@ISA = qw(Net::Server::Fork );
use strict;
use Net::Server::Fork;    # any personality will do
use Device::SerialPort;
use Time::HiRes qw(usleep);
use constant PIN_0 => 'a';
use constant PIN_1 => 'b';
use constant PIN_2 => 'c';
use constant PIN_3 => 'd';
use constant PIN_4 => 'e';
use constant PIN_5 => 'f';
use constant COMM_PAUSE 	=> 100000;
use constant PAUSE => 1000;
use constant SLOWDOWN => '/home/stephen/slowdown/slowdown';
my $port = init_serial();
my %megaval = (
pin_val1 => [],
pin_val2 => [],
);
LungServer->run( conf_file => "CServer.conf" );
close_port();
exit;

### over-ridden subs below

sub close_port{
$port->close || die "failed to close";
$port = undef;
}
sub init_serial {
my @devs = qw(/dev/ttyUSB0 /dev/ttyUSB1 /dev/ttyUSB2);

$port = undef;
for my $port_dev (@devs) {
$port = Device::SerialPort->new($port_dev);
last if $port;
}
if(!$port) {
die "No known devices found to connect to serial: $!\n";
}
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);
return $port;
}
# Demonstrate a Net::Server style hook
sub allow_deny_hook {
my $self = shift;
my $prop = $self->{server};
my $sock = $prop->{client};

# only local connect
return 1 if $prop->{peeraddr} =~ /^127\./;
die "only local connect";
return 0;
}

# Another Net::Server style hook
sub request_denied_hook {
print "Go away!\n";
print STDERR "DEBUG: Client denied!\n";
}

sub run_dequeue {
my $self = shift;
$self->log( 1, "received request dequeue search" );
return;
}

sub send_prt {
my $val;
my ($self,$port_str) = @_;
$port->write($port_str);
usleep(COMM_PAUSE);
$val = $port->lookfor();
chop $val;
push (@{$megaval{$port_str}}, $val);
$self->log( 1,"$port_str $val\n");
$self->write_to_log_hook;
usleep(COMM_PAUSE);
return $val;
}

sub display_data {
my $st;
my @keys = keys %megaval;
foreach (@keys){
my @arr = @{$megaval{$_}};
foreach my $values (@arr){
$st = $st ." $values";
}
}
return $st;
}

sub process_request {
my $self = shift;
eval {
while () {
s/\r?\n$//;
$_ =~ m/(.*).*/;
my $type      = $1;
$self->log( 1,	"'$type'");
if($_ =~m/quit/){$self->server_close;}
switch ($type) {
case "PIN_0" {
my $val = $self->send_prt(PIN_0);
print STDOUT "$val\n"; # this is here so that the client can access the value
}

case "PIN_1" {
my $val = $self->send_prt(PIN_1);
print STDOUT "$val\n";
}

case "PIN_2" {
my $val = $self->send_prt(PIN_2);
print STDOUT "$val\n";
}

case "PIN_3" {
my $val = $self->send_prt(PIN_3);
print STDOUT "$val\n";
}

case "PIN_4" {
my $val = $self->send_prt(PIN_4);
print STDOUT "$val\n";
}

case "PIN_5" {
my $val = $self->send_prt(PIN_5);
print STDOUT "$val\n";
}
case "print_arr" {
my $st = display_data();
$self->log( 1, "$st" );
print STDOUT "$st";
}
# Close the server
case "quit"        { $self->server_close }
else               {
print "You said \"$_\"\r\n";
#         alarm($timeout);
}
}# end case
} ; # end while
warn $@ if $@;
} # end eval
} # end sub
}1; # end package
#--------------- file test.pl ---------------

arduino_server_client07.pl This client calculates the data received from ‘server_relaying_07.pl’ and based on the outcome of that calculation then sends appropriate instructions to fanserver_02.pl

#!/usr/bin/perl

# this client communicates with arduino (slowdown) server, calculates values extracted and then
# communicates to the fancontrol server

#use warnings;

use Time::HiRes qw(usleep);
use IO::Socket::INET;
use strict;
use Device::SerialPort;
use strict;
use warnings;
use constant ITERA => 1200;
use constant TRUE => 1;
use constant FALSE => 0;
use constant SEARCH_ITERA => 6;
my @pins = qw(PIN_0); #PIN_1 PIN_2 PIN_3 PIN_4 PIN_5); # these are the keys of the hashes of
# hashes of arrays
srand(time() ^($$ + ($$ <<15))); # added to let arduino sample multiple inputs without priveleging  # the first pin polled each time my %megaval = (); # data structure where arduino values are stored my @devs = qw(/dev/ttyUSB0 /dev/ttyUSB1); my $port = undef; #init : connect to arduino port for my $port_dev (@devs) { 	$port = Device::SerialPort->new($port_dev);
last if $port;
}
if(!$port) {
die "No known devices found to connect to serial: $!\n";
}

$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);

my $prev_sum = -1;
my $sum = -1;

##

for ( my $cnt = 0; $cnt < ITERA; $cnt++) {    # this will loop for as many times as desired, specified 	#	via constant ITERA  	foreach(@ports){	 		contact_server($_,$cnt); #sends this to the server, which will trigger appropriate arduino  		# data polling 		usleep(500000 + rand(500000)); #random seed, necessary when using multiple piezos with  		# arduino 		if ($cnt >= SEARCH_ITERA){ # this allows a build up, so that the algorithm only begins
#	when a sufficient no. of values have been stored
$sum = search_algo();
print "$sum\n";
if($prev_sum != $sum){ change_fan_speed($sum)}
usleep (100000);
$prev_sum = $sum;
}
}
}

##

sub contact_server {
my ($port,$iter) = @_;
my $remote_host = "127.0.0.1";
my $remote_port = "20204";
my	$socket = IO::Socket::INET->new(PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto    => "tcp",
)
or die "Couldn't connect to $remote_host:$remote_port : $@\n";
my $port_no;
my $port_val;
print $socket "$port\n";
my $value = scalar <$socket>; # used to communicate values to data structure
# on server
chop $value;
my @arr;
$megaval{$port}[$#{$megaval{$port}} + 1] =  $value;
close($socket);
}

sub contact_fan_server { # used to relay values to the fanserver controlling fan speed
my ($speed) = @_;
my $remote_host = "127.0.0.1";
my $remote_port = "20206";
my	$socket = IO::Socket::INET->new(PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto    => "tcp",
)
or die "Couldn't connect to $remote_host:$remote_port : $@\n";
print $socket "$speed\n";
close($socket);
}

sub display_data {
my $st;
my @port = keys %megaval;
foreach my $p (@port){
$st .= "'$p:'\n";
foreach my $val ( @{$megaval{$p}} ){
$st .= " \n val $val  \n";
}
}
return $st;
}

sub change_fan_speed { # this subroutine breaks the rule of thumb, however it can be easily
# modularised given enough time. It sends an appropriate value to the
# fanserver based on the outcome of the search_algo
my $sum = shift;
if ($sum < 5) {
contact_fan_server("PWM2");
} elsif ($sum < 10) {
contact_fan_server("PWM5");
} elsif ($sum < 14) {
contact_fan_server("PWM7");
} elsif ($sum < 19) {
contact_fan_server("PWM10");
} elsif ($sum < 25) {
contact_fan_server("PWM13");
} elsif ($sum < 34) {
contact_fan_server("PWM14");
} elsif ($sum < 40) {
contact_fan_server("PWM15");
} elsif ($sum < 46) {
contact_fan_server("PWM17");
} elsif ($sum < 50) {
contact_fan_server("PWM19");
} elsif ($sum < 55) {
contact_fan_server("PWM20");
} elsif ($sum < 59) {
contact_fan_server("PWM22");
} elsif ($sum < 70) {
contact_fan_server("PWM25");
} elsif ($sum < 76) {
contact_fan_server("PWM27");
} elsif ($sum < 82) {
contact_fan_server("PWM30");
} elsif ($sum < 91) {
contact_fan_server("PWM33");
} elsif ($sum < 102) {
contact_fan_server("PWM35");
} elsif ($sum < 107) {
contact_fan_server("PWM40");
} elsif ($sum < 123) {
contact_fan_server("PWM45");
} elsif ($sum < 123) {
contact_fan_server("PWM45");
} elsif ($sum < 132) {
contact_fan_server("PWM50");
} elsif ($sum < 152) {
contact_fan_server("PWM60");
} elsif ($sum < 161) {
contact_fan_server("PWM70");
} elsif ($sum < 190) {
contact_fan_server("PWM95");
} elsif ($sum < 245) { 				contact_fan_server("PWM125"); 			} elsif ($sum > 245) {
contact_fan_server("PWM180");
}
}

sub search_algo {
my @port = keys %megaval;
foreach my $p (@port){	#$p corresponds to the port
my $sum = -1;
my $ssum = '';
my $counter = $#{$megaval{$p}} -1; # using $counter to determine the amount of elements in data structure
die " cannot do search algo - not enough interations" if ! (ITERA - SEARCH_ITERA);
for(my $cnt = $counter; $cnt > ($counter - SEARCH_ITERA); $cnt--){ # this will loop for the last 6 elements
# in the structure
$sum += $megaval{$p}[$cnt]; # $sum = the cumalative sum of the X amount of elements in the structure
}
return $sum;
}
return $sum;
}

piezo_multiple_v6.pde This sketch sends piezo voltage data to a serial port when instructed by server_relaying_07.pl

// Arduino script to work in tandem with Perl Server, or Perl Serial Port
// Communication Script
// Used with Piezos as the sampled analogue input

int ledPin =  13;    // LED connected to digital pin 13
int analoguePina = 0;      // select the input pin for piezo 'a'
int analoguePinb = 1;     // select the input for piezo 'b'
int analoguePinc = 2;
int analoguePind = 3;
int analoguePine = 4;
int analoguePinf = 5;

// int inByte = 0;         // incoming serial byte - default option not used in this script
char inChar = 'x';	// set to x, a value which will not disrupt initial readings
int val = -1;       // variable to store the value coming from the sensor

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}

int quiet_count = 0;

void loop()
{
inChar = Serial.read();	// listen to serial port

if(inChar == 'a') {
val = analogRead(analoguePina);	// make a reading of analogue pin
//digitalWrite(ledPin, HIGH);   // set the LED on for debug
//Serial.print("val a: ");	     // print to serial, debug
val = analogRead(analoguePina); 	     // make a reading on the analoguePin
//delay(1500); // optional delay to solve timings with server
Serial.println(val);	     // print the value from serial port's analoguePin

inChar = 'x'; // reset inChar

} else if(inChar == 'b') {
//Serial.print("val b: ");
val = analogRead(analoguePinb); // make a reading on the analoguePinb
//delay(1500);
//Serial.print ("val b ");
Serial.println(val);            // print the value of analoguePinb
// delay for a 1000 milliseconds
inChar = 'x';

} else if(inChar == 'c') {
//Serial.print("val b: ");
val = analogRead(analoguePinc); // make a reading on the analoguePinb
//delay(1500);
//Serial.print ("val c ");
Serial.println(val);            // print the value of analoguePinb
// delay for a 1000 milliseconds
inChar = 'x';

} else if(inChar == 'd') {
//Serial.print("val b: ");
val = analogRead(analoguePind); // make a reading on the analoguePinb
//delay(1500);
//Serial.print ("val c ");
Serial.println(val);            // print the value of analoguePinb
// delay for a 1000 milliseconds
inChar = 'x';

} else if(inChar == 'e') {
//Serial.print("val b: ");
val = analogRead(analoguePine); // make a reading on the analoguePinb
//delay(1500);
//Serial.print ("val c ");
Serial.println(val);            // print the value of analoguePinb
// delay for a 1000 milliseconds
inChar = 'x';

} else if(inChar == 'f') {
//Serial.print("val b: ");
val = analogRead(analoguePinf); // make a reading on the analoguePinb
//delay(1500);
//Serial.print ("val c ");
Serial.println(val);            // print the value of analoguePinb
// delay for a 1000 milliseconds
inChar = 'x';
}

if(val == -1){
Serial.println(val);
}
val = -1;
delay(1500);
}
//else if (inChar == 'b') {

//delay(1000);                  // wait for a second
// inChar = 'x';
//delay(1000);                  // wait for a second
/*}else {
}

// otherwise echo anything else sent to us
// while(Serial.available()) {
//  inByte = Serial.read();
//  Serial.print(inByte);
// }
delay(100);*/
//}
//}

feedback_fan_gauger_v3.pde This arduino measures the CPU fan voltage in order to drive a motor to correlate the feedback mechanisms fan speed accordingly

/*
fandriver
*/
int ledPin =  13;    // LED connected to digital pin 13 (& internal LED) - for testing & debug
int relayUpPin1 =  4;   // OUTPUT to Relay (500ohm) via LED (digital output) (0 or 5v) - DECREASE RELAY, yellow LED
int relayDownPin2 =  8;    // OUTPUT to Relay (500ohm) via LED (digital output) (0 or 5v) - INCREASE RELAY , red LED
int CPUfanPin = 0;   // INPUT from Linux CPU fan via voltage drop (frm 12v to 5v max) (0-5v analog)
int OUTfanPin = 5;   // INPUT from LARGE fans via voltage drop (frm 20v to 5v max) to help monitor output-fan speeds

int CPUfanVolt = 0;  // variable to store value from CPU fan voltage
int OUTfanVolt = 0;
int sensorCalibrate = 0;

// Values inserted for sake of debug

int sensor2PinValue = 3;

// int OUTfanVolt = 0;  // variable to store value from OUTPUT fans voltage

// int calibrate1 = 0;  // variable to store calibration setting 1
// int calibrate2 = 0;  // variable to store calibration setting 2
int ontime = 0;     // variable to store timing
int offtime = 0;    // variable to store timing
//  ******************************************************** //

void setup()   {      // The setup() section runs once, when the sketch starts

pinMode(ledPin, OUTPUT);     // initialize the digital pins as an output:
pinMode(relayUpPin1, OUTPUT);
pinMode(relayDownPin2, OUTPUT);
Serial.begin(9600);          // start the serial port
Serial.println("beginning script");
delay(2000);
}

void loop()       // the loop() section runs over and over again, as long as the Arduino has power
{
readSensors();  // read analog input from CPU (analog pin=0) and OUTPUT Fans (analog pin=5)
delay (1000);
if (CPUfanVolt > 115) { // this is an alternative based around the voltage spike initiated by the fans switching from the server
Serial.println("beginning recalibration");
delay (5000);
readSensors();
while (CPUfanVolt != OUTfanVolt) {
if (CPUfanVolt > OUTfanVolt) {
ontime = 2000;
increaseRelayOn();
offtime = 100;
increaseRelayOff();
readOUTfanVolt();
} else if (CPUfanVolt < OUTfanVolt) {         ontime = 2000;         decreaseRelayOn();         offtime = 100;         decreaseRelayOff();         readOUTfanVolt();         }       }     } else {            // person NOT entering or leaving space         offtime = 100;         increaseRelayOff();         decreaseRelayOff();      } } // **************************************** void readSensors() {   CPUfanVolt = analogRead(CPUfanPin);  // read value from CPU fan voltage SENSOR 1   CPUfanVolt = map(CPUfanVolt, 0, 1023, 0, 117); // maps to 117 as map function doesn't seem to use decimals      Serial.print("CPU fan speed: ");          //**   Serial.print(CPUfanVolt);             //** show SENSOR 1 VALUE   Serial.println();                          //**      OUTfanVolt = analogRead(OUTfanPin);  // read value from OUTPUT fans - for feedback calibration   OUTfanVolt = map(OUTfanVolt, 0, 1023, 0, 117);   Serial.print("Output fan speed: ");          //**   Serial.print(OUTfanVolt);             //** show SENSOR 1 VALUE   Serial.println();                          //** } //***************************************** void readOUTfanVolt() {      OUTfanVolt = analogRead(OUTfanPin);  // read value from OUTPUT fans - for feedback calibration   OUTfanVolt = map(OUTfanVolt, 0, 1023, 0, 117);   Serial.print("Output Fan speed: ");          //**   Serial.print(OUTfanVolt);             //** show SENSOR 1 VALUE   Serial.println();                          //** } // **************************************** void calibrate() {    sensorCalibrate = analogRead(OUTfanPin);   // read value set by potentiometer   if (sensorCalibrate > 117) {   //
sensorCalibrate = 118;       //  Validate to keep value ok

Serial.print("Calibrate value: "); //**
Serial.print(sensorCalibrate);     //**
Serial.println();                  //**
}
}
// ****************************************

void increaseRelayOn()      // turns ON the relay for certain time
{
digitalWrite(ledPin, HIGH);              // set the LED/relay on
digitalWrite(relayUpPin1, HIGH);

Serial.print("**** INCREASE ELAY ON !! ***");    //**
Serial.println();                        //**

delay(ontime);                           // relay ON time
}
// ****************************************

void increaseRelayOff()      // turns OFF the relay for certain time
{
digitalWrite(ledPin, LOW);                // set the LED/relay off
digitalWrite(relayUpPin1, LOW);

Serial.print("**** INCREASE RELAY OFF !! ***");    //**
Serial.println();                         //**
Serial.println();                         //**

delay(offtime);                           // relay OFF time
}

//*********************************************

void decreaseRelayOn()      // turns ON the relay for certain time
{
digitalWrite(ledPin, HIGH);              // set the LED/relay on
digitalWrite(relayDownPin2, HIGH);

Serial.print("**** DECREASE RELAY ON !! ***");    //**
Serial.println();                        //**
delay(ontime);                           // relay ON time
}
// ****************************************

void decreaseRelayOff()      // turns OFF the relay for certain time
{
digitalWrite(ledPin, LOW);                // set the LED/relay off
digitalWrite(relayDownPin2, LOW);

Serial.print("**** DECREASE RELAY OFF !! ***");    //**
Serial.println();                         //**
Serial.println();                         //**

delay(offtime);                           // relay OFF time
}