Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: If Statement Not Executing on: April 25, 2013, 05:58:47 pm

There is a bit of ambiguity here. Is it blinking at a half a second rate when it's above that temperature? Does it turn off after a half a second after the first time it hits the threshold and never turn back on? Should it turn on for half a second every time it hits the threshold?

It turns off after a half-second after the first time it hits the threshold and never turns back on. So once the temperature hits 89 and above for example it will beep and light once and that will occur whenever the temperature reaches >=89.

It does something. What that is, and how that differs from what you are expecting is a mystery to us right now.
It doesn't activate at the temperature I've put in for the last if statement. Nothing occurs.
2  Using Arduino / Programming Questions / If Statement Not Executing on: April 25, 2013, 05:44:16 pm
Hello all, I have a program setup that controls a system based on temperature. Everything seems to be working so far however there's one thing that isn't. I want an LED/buzzer to activate for half a second when the temperature reaches a certain level. I have tested the light/beep code in a separate sketch and it goes for the half-second in the loop structure, shown here:

Code:
const int beepPin =  51;      // the number of the LED pin

// Variables will change:
int beepState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

long interval = 500;           

void setup() {
  // set the digital pin as output:
  pinMode(beepPin, OUTPUT);     
}

void loop()
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis; 
    if (beepState == LOW)
      beepState = HIGH;
    digitalWrite(beepPin, beepState);
  }
}

When I put it into my full program (in another sketch) it doesn't work. Here is the full sketch:

Code:
#include <OneWire.h> //The Temperature Sensor Library
#include <DallasTemperature.h> //The Temperature Sensor Libary (From Manufacturer)
#include <LiquidCrystal.h> //The Liquid Crystal Library
#include <math.h> //Math Library

//Initializations
#define ONE_WIRE_BUS 52 //Sets the temp sensors to read from pin 52
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
double ftemp1;
double ftemp2;
int ledPin = 22; //Initialize the digital port using the LED
int relayPin = 53;  //Initialize the voltage port
const int beepPin = 51;
int beepState = LOW;
long previousMillis = 0;
long interval = 1000;
double tempb = ftemp2;

DeviceAddress  amb = { 0x28, 0xE3, 0xEA, 0x51, 0x04, 0x00, 0x00, 0xC0 }; //Define Address of Ambient Temperature Sensor
DeviceAddress sys  = { 0x28, 0x1D, 0xF3, 0x52, 0x04, 0x00, 0x00, 0xA6 }; //Defne Address of System Temperature Sensor

//Setup
void setup()
{
  Serial.begin(9600); //Start the serial monitor
 
  sensors.setResolution(amb, 9); //Ambient Sensor has 9-Bit Resolution
  sensors.setResolution(sys, 9); //System Sensor has 9-Bit Resolution
 
  //Temp Sensor
  sensors.begin(); //Read from the sensor library
 
  //Pins
  pinMode (22,OUTPUT); //Turns off 33k pullup resistor
  pinMode (53, OUTPUT); //Turns off 33k pullup resistor
  pinMode (beepPin, OUTPUT);
 
  //LCD Settings
  lcd.begin(16, 2); //Position of LCD text in the proper area
  lcd.print("Amb.Temp: "); //Print the first measurement's designation
  lcd.setCursor(14,0); //Position the cursor
  lcd.print(" F"); //Temperature unit designation
  lcd.setCursor(0, 1); //Position the second line of text
  lcd.print("Sys.Temp: "); //Print the second measurement's designation
  lcd.setCursor(14,1); //Position the cursor
  lcd.print(" F"); //Temperarture unit designation
 
 
}//Setup Closure

//Loop
void loop(){
   
  //Ambient Temp Sensor Reading/Writing
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.setCursor(10, 0);
  ftemp1 = ((sensors.getTempCByIndex(1)* 1.8) + 32.0);  //Converts the temp reading F
  lcd.print(ftemp1); //print the first reading from the first sensor
  delay(100);

  //System Temp Sensor Reading/Writing
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.setCursor(10, 1); //position the lcd text
  ftemp2 = ((sensors.getTempCByIndex(0)* 1.8) + 32.0);  //Converts the temp reading F
  lcd.print(ftemp2); //print the second reading from the second sensor
  delay(100);
 
   //Relay Control Structure - LED Indicator
  if (ftemp1 < 82) //Turns the system on less/equal to this value
  {
    digitalWrite(ledPin, HIGH);     // sets the LED to 1 (system on indicator)
    digitalWrite(relayPin, HIGH);    //Sets the V1 pin to high
  }
    else if (ftemp1 > 82)
  {
    digitalWrite(ledPin, LOW); //Turns the system off if above condition is not met
    digitalWrite(relayPin, LOW); //Sets V1 pin to low
  }
 

if (ftemp2 > 82)
{
 unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis; 
    if (beepState == LOW)
      beepState = HIGH;
    digitalWrite(beepPin, beepState);
  }
}


}//Loop Closure


I'm thinking it has to do with the if statement structure but I'm not sure what to do. I appreciate the help. Thank you.
3  Using Arduino / Programming Questions / Re: Writing a pin for less then a second. on: April 19, 2013, 08:44:02 pm
The problem is that I'm an extreme beginner.
4  Using Arduino / Programming Questions / Re: Writing a pin for less then a second. on: April 19, 2013, 08:17:20 pm
What if I just wanted to make a single pin high for a second or half a second?
5  Using Arduino / Programming Questions / Writing a pin for less then a second. on: April 19, 2013, 06:46:14 pm
Hi all,

I have a fairly straight forward question. I want to write a digital pin for about a second or less when a temperature goes below a certain level. This digital pin will send voltage to a 555 timer and activate a Piezo buzzer.  I have no clue of how to go about programming this though. All of the hardware is wired and it works. I'm just having trouble with the programming since this is not my background. Here's a snippet of code so far:

Code:
int inPin = 30;         // the number of the input pin
int outPin = 32;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void loop(){

if (ftemp1 < 82)
{
    reading = digitalRead(outPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == LOW)
      state = HIGH;
    else
      state = LOW;

    time = millis();   
  }

  digitalWrite(outPin, state);

  previous = reading;
}

I suppose it would probably look something like that. The only problem is that the pin is always high and I can't get it to turn off after a certain amount of time. Thank you.
6  Using Arduino / Programming Questions / Re: Program Discrepancy on: March 29, 2013, 06:45:36 pm
Thank you everyone. I'll try some of the suggestions now.
7  Using Arduino / Programming Questions / Re: Program Discrepancy on: March 29, 2013, 06:07:21 pm
You have calls to "delay()", and you wonder about real-time?
I have to delay it.

I just want the values to update right before the screen changed on each iteration of the switch case.
8  Using Arduino / Programming Questions / Program Discrepancy on: March 29, 2013, 05:49:37 pm
Hello all,

Quick background:

I'm new to the Arduino forums as well as the Arduino interface.I have only been using it for about two months so far. I have a very light background in programming but my main area is electronics. So basically I haven't had too many problems so far. I'm working on a project that controls a system based on temperature. I have everything I need and so far everything seems to be working fine. For hardware, I'm using an Arduino Mega 2560 Rev.3, LCD Keypad shield (DFRobot 16 x 2), 2 of the OneWire temperature sensors, an N-Channel MOSFET and an LED. However I have run into a small discrepancy. The code is posted below:

Code:
#include <OneWire.h> //The Temperature Sensor Library
#include <DallasTemperature.h> //The Temperature Sensor Libary (From Manufacturer)
#include <LiquidCrystal.h> //The Liquid Crystal Library

//Initializations
#define ONE_WIRE_BUS 52 //Sets the temp sensors to read from pin 52
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
OneWire oneWire(ONE_WIRE_BUS); //Both Sensors on the same pin
DallasTemperature sensors(&oneWire);
int ledPin = 50; //Initialize the digital port using the LED
int FETPin = 53;  //Initialize the voltage port
int beepPin = 21;
int pin;
int i;
int j;
double ftemp1;
double ftemp2;

unsigned long start, finished, elapsed, over;
float hours, minutes ,seconds, millisec;

long previousLCDMillis = 0;    // for LCD screen update
long lcdInterval = 4000;

// screen to show
int screen = 0;   
int screenMax = 2;
bool screenChanged = true;   // initially we have a new screen,  by definition
// defines of the screens to show
#define TEMPERATURE   0
#define SYSTEM        1
#define kWH           2



DeviceAddress  amb = { 0x28, 0xE3, 0xEA, 0x51, 0x04, 0x00, 0x00, 0xC0 }; //Define Address of Ambient Temperature Sensor
DeviceAddress sys  = { 0x28, 0x1D, 0xF3, 0x52, 0x04, 0x00, 0x00, 0xA6 }; //Defne Address of System Temperature Sensor


//Setup
void setup()
{
 
  //Temp Sensor
  sensors.begin(); //Read from the sensor library
  sensors.setResolution(amb, 9); //Ambient Sensor has 9-Bit Resolution
  sensors.setResolution(sys, 9); //System Sensor has 9-Bit Resolution
 
  //Pins
  pinMode (50,OUTPUT); //Turns off 33k pullup resistor
  pinMode (53, OUTPUT); //Turns off 33k pullup resistor
  pinMode (21, OUTPUT);

  return initial();
 
}//Setup Closure

//Loop
void loop()
{
 
    switch(screen)
    {
    case TEMPERATURE:
      temp();
      delay(1000);
    case SYSTEM:
      SystemON();
      delay(1000);
    case kWH:
      delay(1000);
      break;
    default:
      // cannot happen -> showError() ?
      break;
    }
}//Loop Closure

//functions

void temp()

 
  lcd.clear();
  lcd.begin(16, 2); //Position of LCD text in the proper area
  lcd.print("Ambient: "); //Print the first measurement's designation
  lcd.setCursor(14,0); //Position the cursor
  lcd.setCursor(0, 1); //Position the second line of text
  lcd.print("System: "); //Print the second measurement's designation
  lcd.setCursor(14,1); //Position the cursor 
  //Ambient Temp Sensor Reading/Writing
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.setCursor(9, 0);
  ftemp1 = ((sensors.getTempCByIndex(1)* 1.8) + 32.0);  //Converts the temp reading F
  lcd.print(ftemp1); //print the first reading from the first sensor
  lcd.print("\337F");
  delay(100); //10ms delay of each measurement

  //System Temp Sensor Reading/Writing
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.setCursor(9, 1); //position the lcd text
  ftemp2 = ((sensors.getTempCByIndex(0)* 1.8) + 32.0);  //Converts the temp reading F
  lcd.print(ftemp2); //print the second reading from the second sensor
  lcd.print("\337F");
  delay(100); //10ms delay of each measurement
 
  //Return control functions
  return ledindicator();
  return fet();
}

void ledindicator()
{
   if (ftemp1 <= 77) //Turns the system on less/equal to this value
  {
    digitalWrite(ledPin, HIGH);     // sets the LED to 1 (system on indicator)
    delay(100);             // waits for half a second
  }
 
   else if (ftemp1 > 77)
  {
    digitalWrite(ledPin, LOW); //Turns the system off if above condition is not met
    delay(100); //100ms delay
  }
 
}

void fet()
{
  if (ftemp1 <= 77)
  {
    digitalWrite(FETPin, HIGH);
    delay(100);
  }
  else if (ftemp1 > 77)
  {
    digitalWrite(FETPin, LOW);
    delay(100);
  }
}

void SystemON()
{
  finished = millis();
  elapsed = finished-start;
  hours = int(elapsed/3600000);
  over = elapsed%3600000;
  minutes = int(over/60000);
  over=over%60000;
  seconds=int(over/1000);
  millisec=over%1000;
  return systemONread();
  delay(1000);
 
}

void systemONread()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Sys.ON Time:");
  lcd.setCursor(0,1);
  lcd.print(hours,0);
  lcd.print("H ");
  lcd.print(minutes,0);
  lcd.print("M ");
  lcd.print(seconds,0);
  lcd.print("S ");
}

void initial()
{
  lcd.clear();
  lcd.print("Initializing...");
  delay(2000); 
}

I get all of the values I need and everything is returned properly. The problem is that the values for the temperature and system counter do not update in real time. They only refresh after each cycle of the LCD. It must be something to do with the loop but I don't know what. Please keep in mind that the code is unfinished and there might be some superfluous things laying about. Any help that I can get would be greatly appreciated. Thank you!
Pages: [1]