Commit f4276f03 authored by onny's avatar onny

clean up, added rgb led, fixing i2c stuff

parent 9d1af722
#include <Wire.h>
#include <RGBTools.h>
RGBTools rgb(11,10,9); // rgb
#define I2CDEVICE 0x0B
int REDLED =7;
int GREENLED =6;
int BLUELED = 5;
void setup()
{
void setup() {
Serial.begin(9600);
Wire.begin();
}
pinMode(REDLED, OUTPUT);
pinMode(GREENLED, OUTPUT);
pinMode(BLUELED, OUTPUT);
void loop() {
digitalWrite(REDLED, HIGH);
digitalWrite(GREENLED, HIGH);
digitalWrite(BLUELED, HIGH);
if(get_battery_state() == 1) {
int percentage = (int)i2c_smbus_read_word(0x0d);
set_color_from_battery_state(percentage);
delay(600);
rgb.setColor(0,0,0);
delay(600);
Serial.println((int)i2c_smbus_read_word(0x0a)*0.001);
Serial.println(percentage);
}
if(get_battery_state() == 2) {
int percentage = (int)i2c_smbus_read_word(0x0d);
set_color_from_battery_state(percentage);
delay(1000);
}
}
// 90, 150
void get_color_from_range(int range) {
int red = 255;
int green = 0;
int blue = 0;
void loop()
{
if (range <= 255) {
green = green + range;
red = 255;
}
if (get_battery_state()) {
digitalWrite(GREENLED, LOW);
} else {
digitalWrite(GREENLED, HIGH);
if (range >= 255 && range <= 510) {
red = 255 - (range - 255);
green = 255;
}
delay(500);
// 255 , 0 , 0
// 255 , 255 , 0
// 0 , 255 , 0
// 510
rgb.setColor(red,green,blue);
}
int percentage_to_range(int percentage) {
return (510/100) * percentage;
}
void set_color_from_battery_state(int percentage) {
int range;
range = percentage_to_range(percentage);
get_color_from_range(range);
}
unsigned int i2c_smbus_read_word ( uint8_t command ) {
......@@ -46,6 +77,11 @@ unsigned int i2c_smbus_read_word ( uint8_t command ) {
return buffer;
}
/*
* Charging = 1
* Discharging = 2
* Idle = 0
*/
unsigned int get_battery_state () {
if ((int)i2c_smbus_read_word(0x0a) > 0) {
return 1;
......
// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
}
int i2c_read_word(uint8_t dev, uint8_t addr)
{
int rdata = -1;
// First set up the address
Wire.beginTransmission(dev);
Wire.write(addr);
Wire.endTransmission();
// Now, read the data
Wire.requestFrom(dev, (uint8_t)1);
if (Wire.available()) {
rdata = Wire.read();
}
return rdata;
}
void loop()
{
i2c_read_word(0x0B, 0x09);
delay(500);
}
int REDLED =7;
int BLUELED =6;
int GREENLED = 5;
void setup()
{
pinMode(REDLED, OUTPUT);
pinMode(GREENLED, OUTPUT);
pinMode(BLUELED, OUTPUT);
}
void loop()
{
AllOff();
digitalWrite(REDLED, LOW);
delay(500);
AllOff();
digitalWrite(GREENLED, LOW);
delay(500);
AllOff();
digitalWrite(BLUELED, LOW);
delay(500);
}
void AllOff()
{
digitalWrite(REDLED, HIGH);
digitalWrite(GREENLED, HIGH);
digitalWrite(BLUELED, HIGH);
delay(500);
}
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment