Problems getting the pin value.

Good day,

I have the following problem, i got here a class for my temperature sensor, but the pin value gets lost somewhere, if i make the _pin public, and have a Serial.println(_pin) it shows me nothing. while the _pin in my relay class works. because when i click the button on my website the relay does what it supposed to do, yet the value of my sensor is 499, because it has no reading.

but if i place back my old sketch without the classes, it works, and it gives me the correct temperature.

///------ Temperature Sensor Class------------
/// is a class to handle the temperature sensor.
class TempSensor
{
  private:
     uint8_t _pin;
     String _name;  
  public:
     TempSensor(uint8_t pin, String name);
     int getTemp();
     String getName();
};
// Constructor of the object, requires pin number on the Arduino, and a name for the sensor.
TempSensor::TempSensor(uint8_t pin, String name) : _pin(pin), _name(name)
{
  pinMode(_pin, INPUT); 
}
// Gets the temperature, and returns it's value in Degrees Celcius.
int TempSensor::getTemp()
{
  int tempC;
  Serial.println(_pin);
  tempC = ((5.0 * analogRead(_pin) * 100) / 1024); // this is the sum to get degrees celcius in combination with our temperature sensor.
  return tempC;
}
// returns the _name in the object.
String TempSensor::getName()
{
  return _name;
}

///----- Relay Class ------------------------
/// is a class to handle the relay.
class Relay
{
  private:
     boolean _isOff;
     String _name;
     uint8_t _pin;
  public:
     Relay(uint8_t pin, String name);
     boolean SwitchOn();
     boolean SwitchOff();
     boolean Reset(int seconds);
     boolean getStatus();
     String getName();
};
// Constructor of the relay, requires pin number on the arduino, and a name for the relay, relay is standard set to rest-state (off);
Relay::Relay(uint8_t pin, String name) : _pin(pin), _name(name)
{
   pinMode(_pin, OUTPUT);
   _isOff = true; 
}

// Switches the relay on.
boolean Relay::SwitchOn()
{
   digitalWrite(_pin, HIGH);
   _isOff = false;
   return true;
}

// Switches the relay off
boolean Relay::SwitchOff()
{
   digitalWrite(_pin, LOW);
   _isOff = true;
   return true; 
}

// Puts the relay on(or off) waits n seconds, and then turns it off(or on) again;
boolean Relay::Reset(int seconds)
{
   seconds = seconds * 1000;
   if(_isOff == true)
   {
      digitalWrite(_pin, HIGH);
      delay(seconds);
      digitalWrite(_pin, LOW);
   }
   else
   {
      digitalWrite(_pin, LOW);
      delay(seconds);
      digitalWrite(_pin, HIGH); 
   }
   return true;
}

// returns on or off, depending on _relaystatus_off
boolean Relay::getStatus()
{
   return _isOff;
}

// returns the _name in the object
String Relay::getName()
{
   return _name; 
}

///----- Microcontroller Code----------------
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>    // Ethernet library
#include <Udp.h>         // UDP Library

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,177 };
unsigned int localPort = 8888;      // local port to listen on

// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "";                  // a string to send back

// Webserver configuration:
Server server(80);
void showWebsite();
int i=0;
char cbuf[30]; //String that contains received http response
boolean relaystatus_off = true;

TempSensor temp(8, "temp1");
Relay relais(2, "relay1");

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  server.begin();
}

void loop()
{
  int packetSize = Udp.available(); // note that this includes the UDP header
  
  if(packetSize)
  {
    packetSize = packetSize - 8;      // subtract the 8 byte header
    Serial.print("Received packet of size ");
    Serial.println(packetSize);

    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    
    String tempstr;
    tempstr = temp.getTemp();
    tempstr.toCharArray(ReplyBuffer, 4);
    Udp.sendPacket(ReplyBuffer, remoteIp, remotePort);
    
    memset(packetBuffer, 0, sizeof(packetBuffer));
  }
  showWebsite();
  delay(10);
}

void showWebsite()
{
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {
      delay(5);
      if (client.available()) {
        char c = client.read();
        
        // Read only the first chars in wep response
        if (i<30){
          cbuf[i]=c;
          i++;
        }
              
        // if we've gotten to the end of the line (received a newline character)
        // and the line is blank, the http request has ended, so we can send a reply
        if (c == '\n' && current_line_is_blank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("
");
          client.println("<html>");
          client.println("<head>");
          client.println("<script language=\"javascript\">");
          client.println("function refreshPage()");
          client.println("{ window.location.reload(); }");
          client.println("</script>");
          
          client.println("</head><body><form action="" method="" name="" onsubmit=\"refreshpage();\">");
          break;
        }
        if (c == '\n') {
          // we're starting a new line
          current_line_is_blank = true;
        }
        else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }
    }    
    
    //Read enough of wep response
    if (i==30){
       cbuf[i]='\0'; //terminating the string with \0
       i=0; // Reset so we only read the http respones one and first time.
    }
    if ((((int)strstr(cbuf, "?0"))!=0) && (relais.getStatus() == true)) 
    {
       relais.Reset(5);    // Switch OFF relay.
    }
    client.print(temp.getTemp());
    client.print(" Celcius \n");
    client.println("
");
    //client.println("<input type=\"submit\" value=\"Off\" onClick=\"parent.location='?1'\">");
    client.println("<input type=\"submit\" value=\"Reset\" onClick=\"parent.location='?0'\">");
    client.println("
");

    client.println();
    
    client.println("</form></body></html>");

    // give the web browser time to receive the data
    delay(1);
    client.stop();
  } 
}

Good day,

Back at ya!

Sounds as though your Sketch is out of memory (SRAM).

Start here... http://www.arduino.cc/en/Tutorial/Memory

thanks for the quick response, i did not think of the SRAM before...might indeed be the problem, we are using a MEGA though, I don't know if it has more SRAM, but i'll check the memory management first then, thanks.

I responded too soon! This is a problem…

char  ReplyBuffer[] = "";                  // a string to send back

And maybe the reason for your trouble. ReplyBuffer is an array of one character which is only enough space to store the terminating null. Try this…

char  ReplyBuffer[21];                  // a string to send back

the last post, i tried, but it still would give me back 499, so i guess that would not be the solution, i will have to look deeper into the memory management, i'm afraid :)

Another possible problem is that when constructors are called is not defined. You can't set pinMode before the init() function (hidden from you) has been called.

So, setting pinMode() in your constructor may be a waste of time, and therefore, effectively not done.

Things like that should be done in a begin() method that is invoked for the object during setup().

You print _pin in TempSensor::getTemp(), although you don't identify, on the output, what the number is. Does that print the correct value?

Scratch what I said about setting pinMode in the constructor. You are reading from an analog pin. By definition, analog pins are input, so the pinMode command has no affect on analog pins. As a result, the pin number is assumed to refer to a digital pin. Setting the mode of a digital pin has no affect when you then read from an analog pin.

  tempC = ((5.0 * analogRead(_pin) * 100) / 1024); // this is the sum to get degrees celcius in combination with our temperature sensor.

Mixed mode arithmetic is the cause of untold amounts of grief. 100, 1024, and the value returned by analogRead are all integers, as is the variable to store the resutl in. 5.0 is the only float. Add .0 to the end of 100 and 1024, and cast the analogRead value to a float.

  tempC = ((5.0 * (float)analogRead(_pin) * 100.0) / 1024.0);

the _pin value is empty, it doesn't return anything, strange thing is, that the pin of the relay works, but the pin of the sensor does not...i could probably try to make a setPin(); and call that in the setup() from the arduino itself.

thanks for the replies so far people :)

  tempC = ((5.0 * analogRead(_pin) * 100) / 1024); // this is the sum to get degrees celcius in combination with our temperature sensor.
TempSensor temp(8, "temp1");

Analog pin #8, huh?

I might need to add that we are using the Arduino MEGA in combination with the ethernetshield, which requires the SPI library