Pages: [1]   Go Down
Author Topic: if statement and telnet combined  (Read 1122 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok i have two pieces of code that i want to put together, a if statement for if a analouge value get to a certain point it will switch on a LED, but i want the LED to come on and also send the command line in  telnet. i have both codes that run perfect seperatly so i tried to combine them but dosnt really do what i want. it might be down to a if/else statement within another if/else statement. here is my code please can you help me

if state ment code


 */
 
// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 9;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
 
  int analogValue = analogRead(analogPin);// read the value of the potentiometer:

 
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);// if the analog value is high enough, turn on the LED:
  }
  else {
    digitalWrite(ledPin,LOW);
  }


}


telnet code

#include <SPI.h>


#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x28, 0x67 }; //mac address of arduino
byte ip[] = { 172, 18, 10, 52 }; //ip address of arduino
byte subnet[] = {255,255,0,0};
byte gateway[] = {172, 18, x, x};
byte server[] = { 172, 18, xx, xx}; //email server of penny and giles
int time = 2000;
int wait = 250;


Client client (server, 25);

void setup()
{

  Ethernet.begin(mac, ip, gateway, subnet);
 Serial.begin(4800);
 
 delay(time);
 
 Serial.println("connecting...");
 
 if (client.connect())
 {
                        Serial.println("connected");
                       
                        client.println("helo pgxexch01.pag.cw.local" );
                          delay(wait);

                        client.println("MAIL From: joe.winstanley@pennyandgiles.com");
                          delay(wait);

                        client.println("RCPT To: joe.winstanley@pennyandgiles.com");
                        delay(wait);

                        client.println("DATA");
                          delay(wait);
 
 
                        client.println("Please let me know it worked");
                        client.println(".");
                 
                        client.println("QUIT");
                         delay(wait);
 
 
   
 } else {
   Serial.println("did not connect connection failed");
 }
}

void loop()
{
 while (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;smiley-wink
     ;
 }
}


my combined code

#include <SPI.h>


#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x28, 0x67 }; //mac address of arduino
byte ip[] = { 172, 18, 10, 52 }; //ip address of arduino
byte subnet[] = {255,255,0,0};
byte gateway[] = {172, 18, x, x};
byte server[] = { 172, 18, x, xx}; //email server of penny and giles
int time = 2000;
int wait = 250;

const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 9;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

int analogValue = analogRead(analogPin);// read the value of the potentiometer:

Client client (server, 25);

void setup()
{
 
 
  pinMode(ledPin, OUTPUT);  // initialize the LED pin as an output:

  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(4800);
 
 delay(time);
 
 Serial.println("connecting...");
 
 if (client.connect())


{
  label:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);// if the analog value is high enough, turn on the LED:
 
 
                        Serial.println("connected");
                       
                        client.println("helo pgxexch01.pag.cw.local" );
                          delay(wait);

                        client.println("MAIL From: joe.winstanley@pennyandgiles.com");
                          delay(wait);

                        client.println("RCPT To: joe.winstanley@pennyandgiles.com");
                        delay(wait);

                        client.println("DATA");
                          delay(wait);
 
 
                        client.println("Please let me know it worked");
                        client.println(".");
                 
                        client.println("QUIT");
                         delay(wait);
    }else{
    digitalWrite(ledPin,LOW);
    goto label;
 
}
}else{
   Serial.println("did not connect connection failed");
 }
}

void loop()
{
 while (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;smiley-wink
     ;
 }
 { int analogValue = analogRead(analogPin);// read the value of the potentiometer:
}
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49086
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The combined code looks like you only want to accept connections if the analog value is above some threshold. Is that what you really want to do?

The (shudder) goto loop will never end, since analogValue and threshold never change in the loop.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i just want the commands to be sent once a certain level has been reached so ive changed my code but it dosnt send the commands though with the if statement please look below for new code

#include <SPI.h>


#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x28, 0x67 }; //mac address of arduino
byte ip[] = { 172, 18, xx, xx }; //ip address of arduino
byte subnet[] = {255,255,0,0};
byte gateway[] = {172, 18, 1, 1};
byte server[] = { 172, 18, x, xx}; //email server of penny and giles
int time = 2000;
int wait = 250;

const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 9;       // pin that the LED is attached to
const int threshold = 0;   // an arbitrary threshold level that's in the range of the analog input

int analogValue = analogRead(analogPin);// read the value of the potentiometer:

Client client (server, 25);

void setup()
{
 
 
 

  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(4800);
 
 delay(time);
 
 Serial.println("connecting...");
 
 if (client.connect()){

Serial.println("connected");
}
else{
   Serial.println("did not connect connection failed");
 }
 
{
  label:
  if (analogValue > threshold) {
                       
 
 
                        Serial.println("connected");
                       
                        client.println("helo pgxexch01.pag.cw.local" );
                          delay(wait);

                        client.println("MAIL From: joe.winstanley@pennyandgiles.com");
                          delay(wait);

                        client.println("RCPT To: joe.winstanley@pennyandgiles.com");
                        delay(wait);

                        client.println("DATA");
                          delay(wait);
 
 
                        client.println("Please let me know it worked");
                        client.println(".");
                 
                        client.println("QUIT");
                         delay(wait);
}
else{goto label;}}
}

void loop()
{
 
 while (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;smiley-wink
     ;
 }
 
}
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have all your code in setup().  That function only runs once, when the unit powers up to initialize.

You need to move most of your code into loop(), which runs continuously.  The only stuff that should be in setup() is your Ethernet, serial, and client setup.  You also need to move your analogRead() from your variable declaration into loop() as well, as that only runs when the variable is declared (as in just once as well, for a global variable)
Logged


0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok so i changed my code to how you said, it now will send the telnet command when its below a certain value, and if its not below it just stays connected waiting for something to happen. but if the value is above then i make the value drop it dosnt send the command i have to press reset and allow it to run the code then it recongizes that its below a certain value and sends. how do i get a timed constant loop in place so it refreshes every 15seconds to see if anything has happened where i have written (bailout) thanks heres the code again.

#include <SPI.h>


#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x28, 0x67 }; //mac address of arduino
byte ip[] = { 172, 18, xx, xx }; //ip address of arduino
byte subnet[] = {255,255,0,0};
byte gateway[] = {172, 18, 1, 1};
byte server[] = { 172, 18, x, xx}; //email server of penny and giles
int time = 2000;
int wait = 250;

const int analogPin = A5;    // pin that the sensor is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input


Client client (server, 25);

void setup()
{
 
 
 

  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(4800);
 
 delay(time);
 
 Serial.println("connecting...");
 
 if (client.connect()){

Serial.println("connected");
}
else{
   Serial.println("did not connect connection failed");
 }
 
}
void loop(){
     
      int analogValue = analogRead(analogPin);// read the value of the potentiometer
      bailout:
      if (analogValue < threshold) {
                           
                        client.println("helo pgxexch01.pag.cw.local" );
                          delay(wait);

                        client.println("MAIL From: joe.winstanley@pennyandgiles.com");
                          delay(wait);

                        client.println("RCPT To: joe.winstanley@pennyandgiles.com");
                        delay(wait);

                        client.println("DATA");
                          delay(wait);
 
 
                        client.println("Please let me know it worked");
                        client.println(".");
                 
                        client.println("QUIT");
                         delay(wait);
                         

                        }
                        else {goto bailout;} //if tolerance does not excedd repeat

                        {
 
 while (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 if (!client.connected())
 {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;smiley-wink;
 }
      }
    }


 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49086
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You really, and I mean REALLY, need to completely remove goto from your toolkit. Forget it even exists, until you learn to use it properly.

I've been writing C code for 27 years now, and I have never needed to use it. Not once.

Code:
int analogValue = analogRead(analogPin);// read the value of the potentiometer
bailout:
if (analogValue < threshold)
{
    client.println("helo pgxexch01.pag.cw.local" );
    delay(wait);

    // snipped the rest of the code
}
else
{
   goto bailout;
} //if tolerance does not excedd repeat
If the value read from the sensor is not less than threshold, go back and check again. Since analogValue and threshold have not changed, if the condition was not true the first time, it will never be true, and the loop will never end. Until you push the reset button, that is.

The loop function is already called in an endless loop. There is no reason for you to being trying to define another loop inside loop.

If you think you must define another loop, use the proper looping statements, like while, instead of goto, and make absolutely certain that the loop can end.

For the life of me, though, I can't really understand why you want to get any email every time through loop if the sensor is low.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok thanks for the comment the reason i want a email sent to me everytime the sensor drops low is because this is being used in my company as  alarm system for pressure chambers for units. if the pressure drops the units dont get tested and end up being late for delivery costing the company 1000's of pounds/dollars. so thats why i need a email to notify to go and restart the chambers and see why the pressure has dropped below usable level. basically i have a pressure transducer with a output of 4-20ma that i will put through a resistor to get a value of 0-2V this i will then calculate the tolerance level between 0-1000and something the tolerance level in the code. then this will then trigger the code to send the email.

how do i make a end to the loop and then to restart the loop again after say 15minuites thanks so much for the help. and because the loop is continuous i dont understand why it wont check to see if the value has dropped. i am using a potentiometer as a simulator of the pressure transducer while testing the code
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49086
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so thats why i need a email to notify to go and restart the chambers and see why the pressure has dropped below usable level.
If you correct the looping issue, you will be sending an e-mail on each pass through loop, if the pressure drops. I'd think some kind of delay after sending the e-mail, to allow time for the e-mailee to respond might be a good thing.

Quote
basically i have a pressure transducer with a output of 4-20ma that i will put through a resistor to get a value of 0-2V this i will then calculate the tolerance level between 0-1000and something the tolerance level in the code.
4 to 20 mA at what voltage? 0 to 2V will correspond to readings of 0 to 400 or slightly higher, not 0 to 1000, unless you provide a 2V Vref input, and set the ADC to measure with respect to the external Vref.

Quote
how do i make a end to the loop and then to restart the loop again after say 15minuites
Which loop? Your loop or the loop() function?

You should be getting rid of your loop, so the question must apply to the loop() function. The short answer is that you can't end loop() and restart it later.

You could add a call to delay() to wait for 15 minutes, but it would be better to do something like this:
Code:
unsigned long lastMail = 0;
unsigned long waitTime  = 900000UL; // 15 * 60 * 1000

void loop()
{
   unsigned long currTime = millis();
   int sensVal = analogRead(sensPin);
   if(sensVal < errorVal)
   {
      if(currTime - lastMail > waitTime)
      {
         sendEmail();
         lastMail = currTime;
      }
   }
}
One change you might want to consider is allowing the pressure to be low for some period of time before panicking. Presumably, loading parts into the pressurized chamber is not done under pressure, so the pressure will drop when the parts are loaded. You probably don't want an e-mail under those conditions.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok thanks for the help i got it sorted in the end i put a delay of an hour in after the code for the email, so that it would send the email then stop for an hour. It would then re-run the loop and check again and if the pressure was still below then it would send another email.

im now working on another system little bit more complex that involves using two loops, the first loop will have the ifstatement for the pressure and send the email saying pressures drops, then this feeds into another if statement that will say if email has sent goto loop two which will monitor to see if the pressure has returned to its original value. if it has then send a conformation email to say pressure returned and goto loop 1.  these two loops then will be running side by side more or less.

if i get stuck dont worry i will be asking.

ps the pressure for the chambers does go down, but im monitoring the pressure that is in the tubes that feed to the chambers and these are always pressurized. thanks
Logged

Pages: [1]   Go Up
Jump to: