Barigadam Color Sensor - Arduino library

Default address: 0x40. Valid addresses: 0x40-0x60.
The class is named BarigadamColorSensor, following BarigadamMotionSensor.

Getting started:

  #include <Wire.h>
  #include <Barigadam_ColorSensor.h>

  BarigadamColorSensor sensor;         // Address 0x40
  // BarigadamColorSensor sensor(0x41); // A different address

  void setup() {
    Serial.begin(9600);
    Wire.begin();
    delay(20);
  }

  void loop() {
    Serial.println(sensor.readColor());
    delay(200);
  }

Creating an object does not change the physical sensor's address. It only
specifies the address the library should use to communicate with that sensor.

Main functions:

  sensor.getAddress()
    Returns the address used by this object.

  sensor.readRGBC(channel)
    Channel: 1 = R, 2 = G, 3 = B, 4 = C (clear channel / overall light level).
    Returns uint16_t. Returns 0 on an error or an invalid channel number.

  sensor.readRGBC(r, g, b, c)
    Reads all four channels in one transaction. Use uint16_t variables.
    Returns true on success. On failure, returns false and sets all four to 0.
    Replaces Barigadam_readRGBC_All(address, r, g, b, c).

  sensor.readColor()
    1 = black, 2 = blue, 3 = green, 4 = yellow, 5 = red, 6 = white.
    -1 = no color (response 0xFF), -2 = communication error.

  sensor.readHSV(channel)
    1 = H, hue in degrees [0, 360); 2 = S, saturation from 0 to 1;
    3 = V, brightness from 0 to 1. Returns 0 on an error or invalid channel.
    The calculation is unchanged: V = C / 1024, limited to a maximum of 1.

  sensor.readHSV(h, s, v)
    Calculates all three values from one RGBC sample. Use float variables.
    Returns true on success. On failure, returns false and sets all three to 0.
    When C = 0, returns true and sets all three to 0.

  sensor.readRegister(reg)
    Reads one byte from a register. Returns 0 on failure.

  sensor.readRegister(reg, value)
    Stores the result in a uint8_t variable named value and returns true.
    On failure, returns false and sets value to 0. This lets you distinguish
    an actual zero in the register from a communication error.

  sensor.setMode(mode)
    Mode: 0 to 5. Sends the command 0x20 + mode once, then returns.
    true means the I2C write was acknowledged, not that the setting was saved.
    You can read the current mode from register 0x09.

  sensor.changeAddress(newAddress)
    The new address must be from 0x40 to 0x60, different from the current
    address, and not used by another device. Sends the command and checks
    the new address, allowing up to 100 ms of delays plus I2C transaction time.
    If the new address responds, returns true and stores it in the object.
    If the change cannot be confirmed, returns false and keeps the object's
    old address. After an error, use the scanner to check the actual address.

  sensor.sendToSensor(value)
    Kept as a method: 0-5 calls setMode(); 0x40-0x60 calls changeAddress().
    Returns the result of that method. It no longer contains an infinite loop.

  BarigadamColorSensor::scanI2C()
    Like Motion Sensor: continuously scans the bus every 5 seconds.
    This diagnostic function does not return. The old name is also available
    as BarigadamColorSensor::I2Cscan().

Multiple sensors:

  BarigadamColorSensor leftSensor(0x40);
  BarigadamColorSensor rightSensor(0x41);
  int leftColor = leftSensor.readColor();
  int rightColor = rightSensor.readColor();

Each object stores its own address. Assign different addresses to the physical
sensors beforehand. When assigning an address, connect only the sensor being
configured. All objects use Wire; call Wire.begin() once in setup().

Changing the mode and address:

The mode and address can be changed at any time while the sensor is running.
There is no need to switch the sensor off or restart it.
The ChangeMode and ChangeAddress examples call the command once in setup();
in your own sketch, you can call it wherever needed.

Updating older sketches:

  readColor(0x40)            -> sensor.readColor()
  readRGBC(0x40, 1)          -> sensor.readRGBC(1)
  readHSV(0x40, 2)           -> sensor.readHSV(2)
  readRegister(0x40, 0x09)   -> sensor.readRegister(0x09)
  sendToSensor(0x40, 3)      -> sensor.setMode(3)
  sendToSensor(0x40, 0x41)   -> sensor.changeAddress(0x41)

Global functions have been replaced with class methods. Update older sketches
using the examples above. All examples in this archive already use the new
class. setMode(), changeAddress(), and sendToSensor() no longer contain
infinite loops; repeat the call in your own loop() if needed.

Installation: add this ZIP using the Arduino IDE option for installing a
library from a ZIP archive. Then open the Barigadam_ColorSensor examples.
