Go Down

Topic: Reading multiple sensors (Read 384 times) previous topic - next topic

mustafaozguner

Hi everybody.I'm new to Arduino.In my project i have to read multipe sensor values and send them to serial port.I had no problem when i use the codes seperate. But i need all of values at the same time.i cant read light sensor values. searched on forums but didnt work for me.Im using light sensor, tilt sensor and LM35.I need your help.

Here is my code:

Code: [Select]

int TempPin = 0;
int TempVal;
float Sicaklik;
int inPin = 7;
int value = 0;
int led = 13;
const int sensorPin = A1;
int sensorValue = 0;
int sensorMin = 1023;
int sensorMax = 0;
void setup()
{
  Serial.begin(9600);
  analogReference(INTERNAL);
  pinMode(led, OUTPUT);
  pinMode(inPin, INPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  while (millis() < 5000) {
     sensorValue = analogRead(sensorPin);
     if (sensorValue > sensorMax) {
       sensorMax = sensorValue;
     }
     if (sensorValue < sensorMin) {
       sensorMin = sensorValue;
     }
   }
   digitalWrite(13, LOW);
}
void loop()
{
  value = digitalRead(inPin);
  TempRead = analogRead(TempPin);
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
  sensorValue = constrain(sensorValue, 0, 255);
  Temp = TempRead/9.31;
  Serial.println(Temp);
  delay(1000);
  if ( value == 1 )
  {
    digitalWrite(led, value);
    Serial.println("shaked");
    delay(1500);
  }
  if ( sensorValue > 200 )
  {
    Serial.println("darkside");
    delay(1000);
  }
}


thanks in advance.

johnwasser

You are calibrating the MAX and MIN of your light sensor in the first five seconds after power-up.  Are you sure you are rotating the device so it can get valid MAX and MIN values?  Could your LDR have gotten disconnected?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

mustafaozguner


You are calibrating the MAX and MIN of your light sensor in the first five seconds after power-up.  Are you sure you are rotating the device so it can get valid MAX and MIN values?  Could your LDR have gotten disconnected?


LDR is okay.
Ldr works great with this code and can send values to serial port and i can read values. But With the previous codes I cant read values of it.
Code: [Select]

const int sensorPin = A1;    // pin that the sensor is attached to
const int ledPin = 9;        // pin that the LED is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value


void setup() {
    Serial.begin(9600);
   // turn on LED to signal the start of the calibration period:
   pinMode(13, OUTPUT);
   digitalWrite(13, HIGH);

   // calibrate during the first five seconds
   while (millis() < 5000) {
     sensorValue = analogRead(sensorPin);

     // record the maximum sensor value
     if (sensorValue > sensorMax) {
       sensorMax = sensorValue;
     }

     // record the minimum sensor value
     if (sensorValue < sensorMin) {
       sensorMin = sensorValue;
     }
   }

   // signal the end of the calibration period
   digitalWrite(13, LOW);
}

void loop() {
   // read the sensor:
   sensorValue = analogRead(sensorPin);

   // apply the calibration to the sensor reading
   sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

   // in case the sensor value is outside the range seen during calibration
   sensorValue = constrain(sensorValue, 0, 255);

   // fade the LED using the calibrated value:
   analogWrite(ledPin, sensorValue);
   Serial.println(sensorValue);
   delay(1000);
}

johnwasser

The only different part of the code is here:
Code: [Select]

  if ( sensorValue > 200 )
  {
    Serial.println("darkside");
    delay(1000);
  }


Maybe your threshold is set too high?  Have you tried displaying the value to see if you reach 200?

I would try something other than arbitrary delays to keep the output rate down.  Perhaps only display output when something changes.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

mustafaozguner


The only different part of the code is here:
Code: [Select]

  if ( sensorValue > 200 )
  {
    Serial.println("darkside");
    delay(1000);
  }


Maybe your threshold is set too high?  Have you tried displaying the value to see if you reach 200?

I would try something other than arbitrary delays to keep the output rate down.  Perhaps only display output when something changes.


Yes i did. I can see the values. But when i use all of the sensors at the same time in codes. I cant see temperature .

mustafaozguner

Hi folks. I still work on this problem but i cant read values.Please help.

Thanks :)

johnwasser

In your first message you say: ".i cant read light sensor values. ".  In a recent reply you say " I cant see temperature ."

Run your program and cut and paste the output you see in the Serial Monitor window.  Then show your current sketch if it has changed any.  Point out the places in your output where you expected to see something and don't see it.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

mustafaozguner


In your first message you say: ".i cant read light sensor values. ".  In a recent reply you say " I cant see temperature ."

Run your program and cut and paste the output you see in the Serial Monitor window.  Then show your current sketch if it has changed any.  Point out the places in your output where you expected to see something and don't see it.


It is my mistake. I said i can send light sensor values  to serial port and can read them with serial monitor(only with this code)
Code: [Select]

const int sensorPin = A1;    // pin that the sensor is attached to
const int ledPin = 9;        // pin that the LED is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value


void setup() {
    Serial.begin(9600);
   // turn on LED to signal the start of the calibration period:
   pinMode(13, OUTPUT);
   digitalWrite(13, HIGH);

   // calibrate during the first five seconds
   while (millis() < 5000) {
     sensorValue = analogRead(sensorPin);

     // record the maximum sensor value
     if (sensorValue > sensorMax) {
       sensorMax = sensorValue;
     }

     // record the minimum sensor value
     if (sensorValue < sensorMin) {
       sensorMin = sensorValue;
     }
   }

   // signal the end of the calibration period
   digitalWrite(13, LOW);
}

void loop() {
   // read the sensor:
   sensorValue = analogRead(sensorPin);

   // apply the calibration to the sensor reading
   sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

   // in case the sensor value is outside the range seen during calibration
   sensorValue = constrain(sensorValue, 0, 255);

   // fade the LED using the calibrated value:
   analogWrite(ledPin, sensorValue);
   Serial.println(sensorValue);
   delay(1000);
}

. But i cant read only light sensor values with the codes in my first post. I cant solve that problem.

johnwasser

I say again:

Run your program and cut and paste the output you see in the Serial Monitor window.  Then show your current sketch if it has changed any.  Point out the places in your output where you expected to see something and don't see it.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up