Pages: 1 2 [3]   Go Down
Author Topic: Project #3 - Love-O-Meter  (Read 10771 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 136
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry don't have as much time today as I though.... I was wrong about using the map function (unless you want to forget about actual temperature and just work on a scale of x to y) here is my code that I used you can compare it to yours and perhaps figure out your issue...
Code:
const int sensorPin = A0;
float baselineTemp = 0.0;
int sensorVal = 0;
float voltage = 0.0;
float  temperature = 0.0;
int buttonPin = 5;
int button = 0;
int oldbutton = 0;


void setup()
{
  Serial.begin(9600); //open serial port
  for(int pinNumber = 2; pinNumber < 5; pinNumber++) //define pins 2,3, and 4 as outputs and make sure they are off
  {
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }
  pinMode(buttonPin, INPUT);
  //callibrate baseline temperature
  calibrate();
}

void loop()
{
  button = digitalRead(buttonPin);
  if ((button == HIGH) && (button != oldbutton))
  {
    calibrate();
  }
  oldbutton = button;
  sensorVal = analogRead(sensorPin); //read value from sensor
  /*Serial.print("Sensor Value: ");
  Serial.print(sensorVal); //send current sensor reading
  voltage = (sensorVal/1024.0) * 5.0; //convert analog reading to voltage
  Serial.print(" , Volts: ");
  Serial.print(voltage); //send output voltage
  temperature = (voltage - .5) * 100; //convert voltage to actual temp.
  Serial.print(" , degrees C: ");
  Serial.println(temperature); //send temp.*/
 
  if(temperature < baselineTemp)
  {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  if(temperature >= baselineTemp+2 && temperature < baselineTemp+4)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }
  if(temperature >= baselineTemp+4 && temperature < baselineTemp+6)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  }
  if(temperature >= baselineTemp+6)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  delay(1);
}

void calibrate()
{
  sensorVal = analogRead(sensorPin); //read sensor
  voltage = (sensorVal/1024.0) * 5; //convert to voltage
  temperature = (voltage - .5) * 100; // convert to temp
  baselineTemp = temperature; //set baseline
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Sorry to dredge up an old topic, but thought it was better than starting a new one for the same project.

I am only able to get the digital 3 output to light up, both other LEDs stay off - no matter what changes I make to the baseline temp and other debugging I've tried. The circuit is set up exactly as shown in the guide, my code is below:

Code:
const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup (){
 Serial.begin(9600); // open a serial port
for(int pinNumber = 2; pinNumber<5; pinNumber++){
 pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop (){
  int sensorVal = analogRead (sensorPin);
  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);
  //convert the ADC reading to voltage
  float voltage = (sensorVal/1024.0) *5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);
  Serial.print(", Degrees C: ");
  // convert voltage to temperature in degrees
  float temperature = (voltage - .5) * 100;
  Serial.println(temperature);
  if(temperature < baselineTemp){
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }else if(temperature >= baselineTemp+2 &&
  temperature < baselineTemp+4) {
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    }else if(temperature >= baselineTemp+4 &&
  temperature < baselineTemp+6) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    }else if(temperature >= baselineTemp+6 &&
  temperature < baselineTemp+8) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  delay(1);
}

I'd really appreciate any tips anyone can provide, or if you can see where I'm going wrong, it would be a great help.

Thanks,

Tai
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Tai,

I don't see anything really wrong (e.g., syntax errors) in the code and I was able to compile it (but not to test it on my Arduino, since I don't have the circuit built at the moment). Some suggestions:

1) Set a larger delay, for example:
Code:
delay(3000);
so that you are actually able to see on the serial monitor if the values of the parameters are really changing. Does the value of "temperature" change? And how much? I ask this, because this brings me to the next point...

2) In your code I see an undefined situation that happens when the "temperature" variable gets larger than "baselineTemp+8".
For example, in my code the last "else...if" condition is like this:
Code:
else if (temperature >= baselineTemp + 6.0)
  {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);   
  }
 
In my experience with coding, aside from Arduino, I learned that is always better to catch all the possible conditions while setting up your "else..if" control expressions, instead of leaving them undefined.

Apart from these tips, if I understood correctly you are in the situation in which output #2 and output #4 are off, while output #3 lights up.
This configuration is not met anywhere in your code, so it's perhaps pointing to some errors/bad connections in wiring the circuit. In this case a photo of the setup might help :-)

Bests,
Fabio
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Fabio,

Thanks so much for the response.

Based upon your confirmation that nothing looks wrong in the code I went ahead and tried to move some things around on the circuit, it turns out that two of the 3 LEDs were bust- I guess this was a rookie error. Thanks again, really appreciate you pointing me in the right direction.
Logged

Pages: 1 2 [3]   Go Up
Jump to: