Pages: [1] 2   Go Down
Author Topic: get data from 3 programs  (Read 840 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello
Let me explain

I have the main program
then it's call to sub-program that call to sub-sub-program.

I can't get the info from the 3rd program.

I will show an example:

Code:

void main ()
{
Serial.print ("hello");
int first();
Serial.print(d);
Serial.println(f);

}

int first ()
{
 if ((digitalRead(in3)==HIGH))
    { d=0;
}
else
{
d=1;
}

int fin();
return d;
}

int fin ()
 if ((digitalRead(in4)==LOW))
{
f=11111;
}
else
{
f=76
}

return f;
}



if I run the code i only get the numbers in d , but when he need to print f I only get f=0.

anyone know what I did wrong ?

Thank you ,
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1257
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please run your code through the auto formatted and you should see your problem.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is a part of my code ,
when I run it - it's all good and and there is no error.

maybe you can be more direct and tell me where to look for the problem?

Thanks,


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when I run it - it's all good and and there is no error.
By "run", I assume you mean "compile", but all that means is that the program is syntactically correct, which it probably is. However, it is possible (as it is in most languages, both natural and machine) to write things that are syntactically correct, but which make no sense.

Quote
tell me where to look for the problem?
Code:
int fin();
There.
Quote
 int first();
And there.
« Last Edit: June 06, 2012, 05:16:26 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't understand what you just told me.
And I don't think you understand my code - let me make it simpler
this is not the full code but it is the main idea of him.
Code:
int f,d;

void loop ()
{
one();
Serial.print(f);
Serial.print(d);
}

int one ()
{
.
.
.
why();
.
d=3
.
return d;
}

int why ()
{
.
.
.
f=453;
.
.
return f;
}

at the print I only see d=3 and f=0(when I should see f=453)

thanks ,
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And I don't think you understand my code
I'm pretty sure I do.

Code:
void main ()
{
Serial.print ("hello");
int first();

There, you've written a prototype for a function called "first", but you haven't called it.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A few notes about your program.

1. Functions which do not return a value should be declared "void", not "int".
2. Calling a function which just modifies a global variable which is essentially used as a return value is bad programming.
3. Functions which are declared as "int" should place their returned value into a variable.
4. Forward definitions of functions (int fin(); for example) inside another function are bad.

You should have something more like:

Code:
// in main():

d = first();

// and first():

int first()
{
  if(digitalRead(in3)==HIGH)
  {
    return 0;
  }
  return 1;
}

and so on.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

o.k.  - sorry, my mistake
but look now at the lest post I have entered - and tell me what is the problem
thanks ,
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The last code is not complete, so to comment on it would be pointless.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Chances are there is something in why() that is stopping it from setting f to 453.  Broken logic, maybe?  Who knows.  Without the full code it's impossible to say.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

o.k.
this is the full code that I have written - I didn't want to upload it because it's long and most of it not important for my question but if you think it will save the time and you will understand better.

the idea of the code is:
run webserver , see the status of the sensor and then print the time.

Code:
#include <SPI.h>
#include <Ethernet.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address of the device
IPAddress ip(10,0,0,25); // IP address of the device
EthernetServer server(80); //server is using port 80
int piry3=4; //the digital input of sensor1
int piro7=7; //the digital input of sensor2
int direct=5;//the digital input of the sensor3
int calibrationTime=15;// setup time
int y,x,i,d;
unsigned long  updateTime;
int t=0;
int  monthDay,month,year,hour,minute,second;


void setup ()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();  
  Wire.begin();
  pinMode(piry3,INPUT);
  pinMode(piro7,INPUT);
  pinMode(direct,INPUT);

  delay(2000);
  Serial.print("Please Wait - calibrating sensor ");
  for(int i = 0; i < calibrationTime; i++)
  {
    Serial.print(".");
    delay(1000);

  }
  Serial.println(" done");
  Serial.println("SENSOR IS NOW ACTIE");
  delay(50);

}

void loop()
{
  pir_cheak();
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //meta-refresh page every 1 seconds
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"1\">");
          client.print("<TITLE /> Test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("autorefresh test ");
          client.println("<br />");

          // printing the message
          if
            (d==0)
          {
            client.print("All Good - No  Movement");
            client.print("<br />");
            client.print (d);
          }
else
{
  if (d==1)
  {
    for (i=0;i<5;i++)
    {


      client.print(" <span style=color:red>");
      //<style=font-size:20pt;>");//color of message
      client.print("<blink> warning -Someone Is Entering The Secure Zone!!!! </blink>");// blink the message
      client.print("<br />");  
      client.print(d);
      client.print("<br />");  
      client.print(monthDay);
      client.print("/");
      client.print(month);
      client.print("/");
      client.print(year);
      client.print("    ");
      client.print(hour);
      client.print(":");
      client.print(minute);
      client.print(":");
      client.println(second);  
      client.print (millis()/1000);
      client.print("<br />");

    }
    client.print("<table width=25% border=2>");
    client.print("<td align=center>");
    client.print("LOG");
    client.print("<br />");
    client.print("msg number - ");
    client.print(t);
    client.print("<br />");
    client.print("msg time - ");
    client.print (millis()/1000);
    client.print("<br />");
    t++;  
  }

  else
  {
    if (d==2)
    {
      for (i=0;i<5;i++)
      {
        client.print(" <span style=color:red>");
        client.print("<blink> warning -Someone Is Leaving The Secure Zone!!!! </blink>");// blink the message
        client.print("<br />");
        client.print(d);
        client.print("<br />");      
        client.print(monthDay);
        client.print("/");
        client.print(month);
        client.print("/");
        client.print(year);
        client.print("   ");
        client.print(hour);
        client.print(":");
        client.print(minute);
        client.print(":");
        client.println(second);  
        client.print (millis()/1000);
        client.print("<br />");

      }
      client.print("<table width=25% border=2>");
      client.print("<td align=center>");
      client.print("LOG");
      client.print("<br />");
      client.print("msg number - ");
      client.print(t);
      client.print("<br />");
      client.print("msg time - ");
      client.print (millis()/1000);
      client.print("<br />");
      t++;  

    }
  }



}
Serial.println ("END OF LOOP !!!");

break;

}      
if (c == '\n') {
  // you're starting a new line

  currentLineIsBlank = true;
}
else if (c != '\r') {
  // you've gotten a character on the current line
  currentLineIsBlank = false;
}
}
}

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



}

int pir_cheak()  /// The Code of the cheaking the pir+switch status
{

  if (digitalRead(direct)==LOW)
  {
    y=36;

    if ((digitalRead(piry3)==HIGH)&&(digitalRead(piro7)==LOW))
    {
      d=1;
      Serial.println(y);    
      Serial.print("WARNING!");
      printDate();
      delay(4000);
      Serial.println("System is now Active");
    }    

    else
    {
      if ((digitalRead(piry3)==LOW)&&(digitalRead(piro7)==HIGH))
      {
        d=2;
        Serial.println(y);
        Serial.print("WARNING !");
        printDate();
        delay(4000);
        Serial.println("Active");
      }
      else
      {
        Serial.println(y);
        d=0;
        Serial.println("No Movement!");
        Serial.println("wait for more");
      }
    }

  }
  else  
    if (digitalRead(direct)==HIGH)
  {
    x=2321;
    if ((digitalRead(piry3)==HIGH)&&(digitalRead(piro7)==LOW))
    {
      Serial.println(x);
      d=2;
      Serial.println("WARNING !!!!!");
      printDate();
      delay(4000);
      Serial.println("System is now Active");
    }    

    else

    {
      if ((digitalRead(piry3)==LOW)&&(digitalRead(piro7)==HIGH))
      {  
        Serial.println(x);
        d=1;
        Serial.println("WARNING!");
        printDate();
        delay(4000);
        Serial.println("Active");
      }
      else
      {
        Serial.println(x);
        d=0;
        Serial.println("No Movement!");
        Serial.println("wait for more");
      }
    }

  }
  return d;

}


int printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());


  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print("    ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

  return monthDay,month,year,hour,minute,second;
}

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}



when I open the web in 10.0.0.25
I can see the value of d - 0\1\2
 in this line of the code : "client.print(d); "
but when I need to see the time I only get  0/0/0 0:0:0:0
(bye the way in the serial.print I can see the correct time , so it's not a problem of the clock )
every serial.print is good (this is the reason I put so many - to try to understand where is the problem)

thanks

Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
return monthDay,month,year,hour,minute,second;

A function can return one and only one value.

If you want to return more values then either use a "struct" to store them in, or pass variables "by reference" (i.e., as pointers) to the function and get the function to modify those variables.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You declare a set of global variables at the top of your sketch, which are initialised to zero.
Code:
int  monthDay,month,year,hour,minute,second;
but put the date into local variables in your printDate() functions
Code:
  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

Guess which ones get client.print()ed?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess the 0???   smiley

so what do I need to do to change it?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use the global variables by removing the 'int's:

Code:
  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  monthDay = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());

And get rid of the return statement at the end of printDate().

Make it a void function, as you aren't returning a value.
Code:
void printDate(){
Logged

Pages: [1] 2   Go Up
Jump to: