Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //myLDR Object - Step 1
- class myLDR {
- public:
- int pin;
- int min_value = 1023;
- int max_value = 0;
- int read_sensor() {
- return analogRead(pin);
- }
- };
- myLDR sensor;
- void setup() {
- sensor.pin = A5;
- Serial.begin(9600);
- }
- void loop() {
- Serial.println(sensor.read_sensor());
- }
- //myLDR Object - Step 2
- class myLDR {
- public:
- int pin;
- int min_value = 1023;
- int max_value = 0;
- int read_sensor() {
- return analogRead(pin);
- }
- void calibrate_sensor() {
- int read_val = read_sensor();
- if (read_val > max_value) max_value = read_val;
- if (read_val < min_value) min_value = read_val;
- }
- int read_calibrated() {
- int readval = read_sensor();
- return map(readval, min_value, max_value, 0, 1023);
- }
- };
- myLDR sensor;
- void setup() {
- sensor.pin = A5;
- Serial.begin(9600);
- int n = 0;
- while (n < 1000) {
- sensor.calibrate_sensor();
- delay(10);
- n++;
- }
- Serial.print("The lowest i found was: ");
- Serial.println(sensor.min_value);
- Serial.print("The highest i found was: ");
- Serial.println(sensor.max_value);
- }
- void loop() {
- Serial.print(sensor.read_sensor());
- Serial.print("\t");
- Serial.println(sensor.read_calibrated());
- }
Advertisement
Add Comment
Please, Sign In to add comment