How do I set different outputs along a Senors values like a to b, c to d, etc

I new to this so sorry if the question is not well constructed. I'm assuming you have to use map function but not sure how to set this up. I was wondering if anyone could provide me with so examples.

I'm trying to change the time delay on a digital output according to specific ranges of a senor input.

Thanks

easterly81: I'm assuming you have to use map function but not sure how to set this up.

Only if there exists an equation that can take the unmapped value and make it into a mapped value. If you had a subset like this:

1 -> -4 2 -> 8 3 -> 1000 4 -> -2345 5 -> 1

then the map function won't work. In cases like this, you would need an if/else if or an array.

You would be better off posting more information, like the code you have currently and a better description (including examples) of what you want to do.

i’m trying to turn a relay on and off with different light intensities, currently it turns on above 650. i would like to be able to change to time the relay is on based on the light intensity range, ie 650-750 and so on.

#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
int LDR = A0;                  //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0;             //that’s a variable to store LDR values
int light_sensitivity = 650;  //This is the approx value of light surrounding your LDR


#include <SdFat.h>
#include <SdFatUtil.h>  // define FreeRam()

#define SD_CHIP_SELECT  SS  // SD chip select pin
#define USE_DS1307       1  // set nonzero to use DS1307 RTC
#define LOG_INTERVAL  1000  // mills between entries
#define SENSOR_COUNT     1  // number of analog pins to log
#define ECHO_TO_SERIAL   1  // echo data to serial port if nonzero
#define WAIT_TO_START    0  // Wait for serial input in setup()
#define ADC_DELAY       10  // ADC delay for high impedence sensors

// file system object
SdFat sd;

// text file for logging
ofstream logfile;

// Serial print stream
ArduinoOutStream cout(Serial);

// buffer to format data - makes it eaiser to echo to Serial
char buf[80];
//------------------------------------------------------------------------------
#if SENSOR_COUNT > 6
#error SENSOR_COUNT too large
#endif  // SENSOR_COUNT
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
#if USE_DS1307
// use RTClib from Adafruit
// https://github.com/adafruit/RTClib

// The Arduino IDE has a bug that causes Wire and RTClib to be loaded even
// if USE_DS1307 is false.


#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;  // define the Real Time Clock object


//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
    DateTime now = RTC.now();

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
// format date/time
ostream& operator << (ostream& os, DateTime& dt) {
  os << dt.year() << '/' << int(dt.month()) << '/' << int(dt.day()) << ',';
  os << int(dt.hour()) << ':' << setfill('0') << setw(2) << int(dt.minute());
  os << ':' << setw(2) << int(dt.second()) << setfill(' ');
  return os;
}
#endif  // USE_DS1307
//------------------------------------------------------------------------------
void setup() {
  {lcd.begin(16,2);
  lcd.clear();
  lcd.print("LDR");}
  
  pinMode(3, OUTPUT);
  Serial.begin(9600);
  while (!Serial){}  // wait for Leonardo
  
  // pstr stores strings in flash to save RAM
  cout << endl << pstr("FreeRam: ") << FreeRam() << endl;

#if WAIT_TO_START
  cout << pstr("Type any character to start\n");
  while (Serial.read() <= 0) {}
  delay(400);  // catch Due reset problem
#endif  // WAIT_TO_START

#if USE_DS1307
  // connect to RTC
  Wire.begin();
  if (!RTC.begin()) error("RTC failed");
  // set date time callback function
  SdFile::dateTimeCallback(dateTime);
  DateTime now = RTC.now();
  cout  << now << endl;

#endif  // USE_DS1307

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) sd.initErrorHalt();

  // create a new file in root, the current working directory
  char name[] = "LOGGER00.CSV";

  for (uint8_t i = 0; i < 100; i++) {
    name[6] = i/10 + '0';
    name[7] = i%10 + '0';
    if (sd.exists(name)) continue;
    logfile.open(name);
    break;
  }
  if (!logfile.is_open()) error("file.open");

  cout << pstr("Logging to: ") << name << endl;
  cout << pstr("Type any character to stop\n\n");
  
  // format header in buffer
  obufstream bout(buf, sizeof(buf));

  bout << pstr("millis");

#if USE_DS1307
  bout << pstr(",date,time");
#endif  // USE_DS1307

  for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
    bout << pstr(",sens") << int(i);
  }
  logfile << buf << endl;

#if ECHO_TO_SERIAL
  cout << buf << endl;
#endif  // ECHO_TO_SERIAL
}
//------------------------------------------------------------------------------
void loop() {

  {lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(analogRead (A0));}
  delay(200);
  
  LDRValue = analogRead(LDR);          //reads the ldr’s value through LDR which we have set to Analog input 0 “A0?
    Serial.println(LDRValue);                  //prints the LDR values to serial monitor
 
    if (LDRValue > light_sensitivity) 
      {
  digitalWrite(3, HIGH);
  delay(180000); 
  digitalWrite(3, LOW);
  delay(1620000);
        }
    if (LDRValue < light_sensitivity) 
      {
  digitalWrite(3, LOW);
  delay(900000); 

        }
  
  uint32_t m;

  // wait for time to be a multiple of interval
  do {
    m = millis();
  } while (m % LOG_INTERVAL);

  // use buffer stream to format line
  obufstream bout(buf, sizeof(buf));

  // start with time in millis
  bout << m;

#if USE_DS1307
  DateTime now = RTC.now();
  bout << ',' << now;
#endif  // USE_DS1307

  // read analog pins and format data
  for (uint8_t ia = 0; ia < SENSOR_COUNT; ia++) {
#if ADC_DELAY
    analogRead(ia);
    delay(ADC_DELAY);
#endif  // ADC_DELAY
    bout << ',' << analogRead(ia);
  }
  bout << endl;

  // log data and flush to SD
  logfile << buf << flush;

  // check for error
  if (!logfile) error("write data failed");

#if ECHO_TO_SERIAL
  cout << buf;
#endif  // ECHO_TO_SERIAL

  // don't log two points in the same millis
  if (m == millis()) delay(1);
  
  if (!Serial.available()) return;
  logfile.close();
  cout << pstr("Done!");
  while (1);



}

easterly81: i'm trying to turn a relay on and off with different light intensities, currently it turns on above 650. i would like to be able to change to time the relay is on based on the light intensity range, ie 650-750 and so on.

Then a you want a switch ranges as such:

switch (someInput)
{
  case 1 ... 5: // 1 to 5
    output = someValue1;
    break;
  case 6 ... 10: // 6 to 10
    output = someValue2;
    break;
  case 1 ... 15: // 11 to 55
    output = someValue3;
    break;
  default:
    output = someValue4;
    break;
}

So i would want to do this

switch (someInput)
{
  case 650 ... 749: // 650 to 749
    output = someValue1;
    break;
  case 750 ... 849: // 750 to 849
    output = someValue2;
    break;

}

not sure i understand

case 1 ... 15: // 11 to 55 output = someValue3; break;

and what does the default do?

thanks

For what it’s worth, I think the switch/case approach is a poor approach when you are trying to work out which of a series of contiguous ranges a value is in. It means the ranges have to be entered several places with no warning if you define the ranges wrongly. In this situation it would IMO be far better to use a set of if/else statements:

if(value >= 850)
{
  // handle 850 and over
}
else if(value >= 750)
{
  // handle 750 .. 849
}
else if(value >= 650)
{
  // handle 650 .. 749
  ... etc

Notice that I’ve arranged the order of the 'if’s so that each comparison only needs to be done once; by the time you reach “if(value >= 750)” you already know that the value is < 850, and so on.

Depending on the actual ranges and the output value associated with each range, it may be possible to calculate the output value arithmetically from the input value, in which case that would be an even better approach.