Reading Sensors

Good to know: This software is currently in Beta.

//LightSensor.h

#pragma once

#include "Arduino.h"
#include <Wire.h>

class LightSensor 
{
    public:
        LightSensor();
        bool Any();
        
        void Read();

        bool Front_Sensor();
        bool Rear_Sensor();
        bool Left_Sensor();
        bool Right_Sensor();
        bool Right_Front_Sensor();
        bool Left_Front_Sensor();
        bool Right_Rear_Sensor();
        bool Left_Rear_Sensor();

        bool Left_Centre_Sensor();
        bool Right_Centre_Sensor();

    private:
        char buffer[3];
         
};
//LightSensor.cpp
#include "LightSensor.h"


LightSensor::LightSensor()
{
    //TODO: CHANGE LS_1, LS_2, LS_3, LS_4 to pins that map to your microcontroller
    pinMode(LS_1, INPUT_PULLDOWN);
    pinMode(LS_2, INPUT_PULLDOWN);
    pinMode(LS_3, INPUT_PULLDOWN);
    pinMode(LS_4, INPUT_PULLDOWN);
    
}


bool LightSensor::Any()
{
    //single pin should be enough, but you could look at all of them
    if(digitalRead(LS_4)){//} || digitalRead(LS_4) || digitalRead(LS_1) || digitalRead(LS_2)){
        return true;
    }
    
    return false;
}

// Need to call Read before using the values of the sensors.
void LightSensor::Read(){
    Wire.requestFrom(0x13, 3);    // request 6 bytes from peripheral device #8
    int counter = 0;
    while (Wire.available()) { // peripheral may send less than requested
        char c = Wire.read(); // receive a byte as character
        buffer[counter] = c;
        counter++;
    }
    
}

bool LightSensor::Front_Sensor(){

    return buffer[0] & 1;
}

bool LightSensor::Rear_Sensor(){

    return buffer[0] & 2;
}

bool LightSensor::Left_Sensor(){

    return buffer[0] & 4;
}

bool LightSensor::Right_Sensor(){

    return buffer[0] & 8;
}

bool  LightSensor::Right_Front_Sensor(){
    return buffer[1] & 2;
}
bool  LightSensor::Left_Front_Sensor(){
    return buffer[1] & 1;
}
bool  LightSensor::Right_Rear_Sensor(){
    return buffer[1] & 8;
}
bool  LightSensor::Left_Rear_Sensor(){
    return buffer[1] & 4;
}

bool LightSensor::Left_Centre_Sensor(){
    return buffer[2] & 1;
}

bool LightSensor::Right_Centre_Sensor(){
    return buffer[2] & 2;
}

Last updated