Difference between revisions of "Blog 22"

From BitWizard Wiki
Jump to navigation Jump to search
Line 97: Line 97:
}
}
unsigned long DIOAddress;
unsigned long DIOAddress;
byte CurrentHour = hour();
byte CurrentHour = hour();
int HourMax = 11;
int HourMax = 11;
int HexMax = 255;
int HexMax = 255;
byte Pwm;
byte Pwm;
byte NewHour;
byte NewHour;
Serial.print("\r\nCurrent Hour ");
Serial.print("\r\nCurrent Hour ");
Serial.print(CurrentHour);
Serial.print(CurrentHour);
if (CurrentHour >= HourMax) {
if (CurrentHour >= HourMax) {
NewHour = CurrentHour - 12;
NewHour = CurrentHour - 12;
}
}
// NewHour = NewHour + 2;
// NewHour = NewHour + 2;
Serial.print("\r\nNew Hour ");
Serial.print("\r\nNew Hour ");
Serial.print(NewHour);
Serial.print(NewHour);
Pwm = NewHour * HexMax / HourMax;
Pwm = NewHour * HexMax / HourMax;
Serial.print("\r\n Decimal ");
Serial.print("\r\n Decimal ");
Serial.print(Pwm, DEC);
Serial.print(Pwm, DEC);
Serial.print("\r\n Hexadecimal ");
Serial.print("\r\n Hexadecimal ");
Serial.print(Pwm, HEX);
Serial.print(Pwm, HEX);
set_var(0x42, 0x50, Pwm);
set_var(0x42, 0x50, Pwm);
DIOAddress = get_var(0x50, 0xb);
DIOAddress = get_var(0x50, 0xb);
Serial.print("\r\n PWM: ");
Serial.print("\r\n PWM: ");
Serial.print(DIOAddress, HEX);
Serial.print(DIOAddress, HEX);


== DIO Analog Meter - Timer ==
== DIO Analog Meter - Timer ==

Revision as of 17:18, 24 December 2015

!BETA!

Hardware used on Arduino:

Programmed with:

Simple Example code

In this code I will show how you can make the Analog meter pointer, be at 50% for ten seconds and being off for 10 seconds. It is just easy code to use to check if the connection works between meter, dio and arduino.

#define DIO 0x42
#include <Wire.h> 

void setup()
{
  Wire.begin(); // wake up I2C bus
  Serial.begin(9600);
  set_var(0x42, 0x30, 0x01);  
  set_var(0x42, 0x5f, 0x01);  
}

byte get_var(byte address, byte reg)
{ 
  byte value;   
  Wire.beginTransmission(address);  
  Wire.write(reg);
  Wire.endTransmission(); 
  delayMicroseconds (10);
  Wire.requestFrom(DIO, 1);
  value = Wire.read();
  return value;
} 

void set_var(byte address, byte reg, byte value)
{
  Wire.beginTransmission(address);      
  delayMicroseconds  (10);
  Wire.write(reg);
  delayMicroseconds  (10);
  Wire.write(value);   
  Wire.endTransmission();    
} 

void loop()
{
  unsigned long DIOAddress; 

  char buf[32];
  set_var(0x42, 0x50, 0x80);  

  DIOAddress = get_var(0x50, 0xb);
  sprintf (buf, "PWM80:  A:%d \r\n", DIOAddress);
  Serial.write (buf); 
 
 delay(10000);
 
  set_var(0x42, 0x50, 0x00);   

  DIOAddress = get_var(0x50, 0xb);
  sprintf (buf, "PWM0 :  A:%d \r\n", DIOAddress);
  Serial.write (buf); 
 
 delay(10000);
 }

ArduinoPWMMeter.jpg

DIO Analog Meter - Clock

In this project I made the Arduino version of the clock. What it does is that it read how late it is, and with the time it calculate how much PWM the meter should get to point to the right hour.

The Full script can be found here: Link The file has the name: DIO_CLOCK_ARDUINO2.ino

This script I used for editing is the TimeSerial script( Examples -> Time ), that you get when you download the Time Library. The Time Library is required, when you want to work with time on your Arduino.

Important stuff that is added:

#include <Time.h>  
#define DIO 0x42
#include <Wire.h> 

void setup()  {
Wire.begin();
set_var(0x42, 0x30, 0x01);  
set_var(0x42, 0x5f, 0x01); 
}

unsigned long DIOAddress;
byte CurrentHour = hour();
int HourMax = 11;
int HexMax = 255;
byte Pwm;  
byte NewHour;
 
Serial.print("\r\nCurrent Hour ");
Serial.print(CurrentHour);

if (CurrentHour >= HourMax) {
  NewHour = CurrentHour - 12; 
}   
 
// NewHour = NewHour + 2;
Serial.print("\r\nNew Hour ");
Serial.print(NewHour);

Pwm = NewHour * HexMax / HourMax; 
Serial.print("\r\n Decimal      ");
Serial.print(Pwm, DEC);
Serial.print("\r\n Hexadecimal  ");
Serial.print(Pwm, HEX);
set_var(0x42, 0x50, Pwm); 

DIOAddress = get_var(0x50, 0xb);
Serial.print("\r\n PWM: ");
Serial.print(DIOAddress, HEX);

DIO Analog Meter - Timer

Useful links