Everyone's favorite topic...pointer question

I think I am trying to do something relatively basic, but it doesn't seem to be working correctly.

I have a char array that stores the status of a fan for printing it on a 1602 LCD... char fan_status[] = "off";

In the main program, I have a call to to a function fanOn(); and fanOff(); That turns a relay on or off.... In that function I try to update my fan_status array:

void FanOn(){
  digitalWrite(fan,enable);
  strcpy(fan_status," on");
}

But, it doesn't seem to be working correctly, i suspect because I am not using pointers correctly...I never see the Serial output or the LCD update from on to off.

psychephylax: I have a char array that stores the status of a fan for printing it on a 1602 LCD... char fan_status[] = "off";

In the main program, I have a call to to a function fanOn(); and fanOff(); That turns a relay on or off.... In that function I try to update my fan_status array:

void FanOn(){
  digitalWrite(fan,enable);
  strcpy(fan_status," on");
}

But, it doesn't seem to be working correctly, i suspect because I am not using pointers correctly...I never see the Serial output or the LCD update from on to off.

You haven't presented your entire code. I suspect you have a bug somewhere you did not show the code for. I suspect this because the following code works:

char fan_status[] = "off";

void setup() {
  Serial.begin(115200);
  Serial.println(fan_status);
  strcpy(fan_status," on");
  Serial.println(fan_status);
}

void loop() {
}

http://www.cplusplus.com/reference/cstring/strcpy/

Here’s the full code:

/* 
 
 Program info
 
 */
/* Miscellaneous project notes 
 For fan: http://www.harborfreight.com/264-gph-submersible-fountain-pump-68395.html?utm_campaign=SEO&utm_medium=Inbound_links&utm_source=linking
 
 
 
 */

//
#define FAN_PIN 8
#define PUMP_PIN 9
#define DHT_PIN 6
#define DHTTYPE DHT11
#define enable LOW
#define disable HIGH



//Includes
#include <LiquidCrystal.h>
#include <DHT.h>
#include <stdio.h>

DHT dht(DHT_PIN,DHTTYPE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int fan = FAN_PIN;
int pump = PUMP_PIN;

//Create global adjustable setting place holders
int temp_threshold = 80; //Initial temp threshold (Will be read from EEPROM after configuration)
int humidity_threshold = 90; //initial humidity threshold (Will be read from EEPROM after configuration)
int moisture_threshold = 999; //initial soil moisture threshold (Will be read from EEPROM after configuration)
boolean configuration_mode = false; //This is a flag to set the system into configuration mode
char fan_status[] = "off";
char pump_status[] = "off";
//char *ptr_fan_status = NULL;
//char *ptr_pump_status = NULL;
//ptr_fan_status = &fan_status;
unsigned long last_lcd_update = millis();

void setup()
{
  //Start serial communication
  Serial.begin(9600);
  //Set up the pins
  digitalWrite(fan,disable);
  digitalWrite(pump,disable);
  pinMode(fan,OUTPUT);
  pinMode(pump,OUTPUT);
  //Instantiate sensors and lcd
  dht.begin();
  lcd.begin(16,2);
  //Set up the time for screen update
  last_lcd_update = millis();
}

void loop()
{
  Serial.println(pump_status);
  Serial.println(fan_status);
  //Loop variables
  //We will use this to refresh the screen instead of using delay() so the program can continue execution;
    //Serial.println("Millis:");
    //Serial.println(millis());
    //Serial.println(last_lcd_update);
  //First we check to see if the configuration mode was enabled
  if (configuration_mode) {
    //We're in configuration mode so we update the LCD
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Configuration mode");
    lcd.setCursor(0,1);
    lcd.print("1-Tmp 2-Hum 3-Soil");
    for (int i=0; i < 2; i++){
    lcd.scrollDisplayLeft(); //The bottom doesn't fit so we scroll
    delay(500);
    }
    delay(1000);
    for (int i=2; i > 0; i--){
      lcd.scrollDisplayRight();
      delay(500);
    }
}
  else { //Normal program execution
    //We don't need to loop continuously
    //so we delay for 10 seconds
    //delay(1000);
    //Fetch all the sensor data
    int temperature = getFTemp();
    int humidity = getHumidity();
    int soil_moisture = getMoisture();
     if (millis() - last_lcd_update < 2000) {  
    //Display all the data on LCD
    lcd.setCursor(0,0);
    lcd.print("Tmp:");
    lcd.print(temperature);
    lcd.print("F");
    lcd.print(" Hum:");
    lcd.print(humidity);
    lcd.print("%");
    lcd.setCursor(0,1);
    lcd.print("Soil:");
    lcd.print(soil_moisture);
     } else if (millis() - last_lcd_update > 2500) {
    //Clear the display and show fan/pump status
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Fan: ");
    for (int i = 0; i < 3; i++){
    lcd.print(fan_status[i]);
    }
    lcd.setCursor(0,1);
    lcd.print("Pump: ");
    for (int i = 0; i < 3; i++){
    lcd.print(pump_status[i]);
    }
    if (millis() - last_lcd_update > 3000) {
      last_lcd_update = millis();
     }
     }
    //Compare the sensors to thresholds and turn fan/pump on or off
    if (soil_moisture >= moisture_threshold){
      PumpOff();
    }

    if (soil_moisture < moisture_threshold) {
      PumpOn(pump_status);

    }

    if (temperature < temp_threshold){
      FanOff();
    }

    if (temperature >= temp_threshold){
      FanOn(fan_status);
    }
  }
  //Add a small delay
  
}


/* Read the DHT sensor and return the Fahrenheight temperature */
float getFTemp(){
  return dht.readTemperature() * 9/5 + 32;
}

/* Read the DHT sensor and return the Celcius temperature */
float getCTemp(){
  return dht.readTemperature();
}

/* Read the DHT sensor for humidity */
float getHumidity(){
  return dht.readHumidity();
}

/* Read Soil Moisture sensor */
int getMoisture(){
  return analogRead(A0);
}

/* Turn the pump relay on */
void PumpOn(char *pump_status){
  digitalWrite(pump,enable);
  strcpy(pump_status," on");
  //Serial.println(pump_status);
}

/* Turn the pump relay off */
void PumpOff(){
  digitalWrite(pump,disable);
  strcpy(pump_status,"off");
  //Serial.println(pump_status);
  
}

/* Turn the fan relay on */
void FanOn(char *fan_status){
  digitalWrite(fan,enable);
  strcpy(fan_status," on");
  //Serial.println(fan_status);
}

/* Turn the fan relay off */
void FanOff(){
  digitalWrite(fan, disable);
  strcpy(pump_status,"off");
  //Serial.println(fan_status);
}

I haven’t completely commented it out, and the LCD displaying needs a little work since it doesn’t clear characters off the display correctly. But I am looking at getting the fan status updated correctly.

psychephylax: /* Turn the fan relay off */ void FanOff(){   digitalWrite(fan, disable);   strcpy(pump_status,"off");   //Serial.println(fan_status); }

When the FAN is off, you update the PUMP status to "off". Should be strcpy(fan_status,"off") here, most likely.

Aha, i guess that's what happens when you copy and paste. That fixed it (and I was testing this particular scenario too, if I used the tempoerature check it would have worked!