My pinMode output 5 and 6 don't works with this code but will work when they are stand alone.

What is causing the Arduino to not output anything from these outputs within my code?

#include <SoftwareSerial.h>                    // use the software serial library
SoftwareSerial mySerial(3,4); // receive data at pin 3; transmit data at pin 2
int setpoint;
float val;
int roomtemp;
int UCL;
int LCL;

float salinity;
void setup()
{
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(5,OUTPUT);
    pinMode(6,OUTPUT);
    pinMode(3,OUTPUT);
    pinMode(2, OUTPUT);
    roomtemp=25;
    setpoint=7.0291*roomtemp+334.73;
    UCL=setpoint+6.12;
    LCL=setpoint-6.12;
    Serial.print("Setpoint =");
    Serial.print(setpoint);
    Serial.print("Degrees Celsius");
    Serial.print("UCL =");
    Serial.print(UCL);
    Serial.print("Degrees Celsius");
    Serial.print("LCL =");
    Serial.print(LCL);
    Serial.print("Degrees Celsius");
    mySerial.begin(9600);
    delay(500);           // set data rate to 9600 baud; wait for bootup
    mySerial.write(254);
    mySerial.write(128);
    mySerial.write(254);
    mySerial.write(1);    // clear screen & move to top left position
    mySerial.write(254);
    mySerial.write(132);
    mySerial.print("LCL");
    mySerial.write(254);
    mySerial.write(139);
    mySerial.print("SP");
    mySerial.write(254);
    mySerial.write(144);
    mySerial.print("UCL");
    mySerial.write(254);
    mySerial.write(192);
    mySerial.print("S:");
    mySerial.write(254);
    mySerial.write(195);
    mySerial.print(0.072,3);
    mySerial.write(254);
    mySerial.write(201);
    mySerial.print(0.100,3);
    mySerial.write(254);
    mySerial.write(207);
    mySerial.print(0.108,3);
    mySerial.write(254);
    mySerial.write(148);
    mySerial.print("T:");
    mySerial.write(254);
    mySerial.write(152);
    mySerial.print("24.1");
    mySerial.write(254);
    mySerial.write(158);
    mySerial.print("25.0");
    mySerial.write(254);
    mySerial.write(164);
    mySerial.print("25.9");
    mySerial.write(254);
    mySerial.write(212);
    mySerial.write(254);
    mySerial.write(212);
    mySerial.print("S=");
    mySerial.write(254);
    mySerial.write(220);
    mySerial.print("T=");
}

void loop()
{
    Serial.println();
    int val=analogRead(5);
    Serial.print("Analog Value =");
    Serial.print(val);
    Serial.print("Temperature =");
    Serial.println((0.14227*val)-47.621);

    if(val<LCL) { digitalWrite(3,HIGH); }
    else if(val>UCL) { digitalWrite(3,LOW); }

    mySerial.write(254);
    mySerial.write(227);

    if(val<LCL) { mySerial.write("H= ON"); }

    mySerial.write(254);
    mySerial.write(227);

    if(val>UCL) { mySerial.write("H=OFF"); }

    digitalWrite(2, HIGH);
    delay(100);
    int analogS=analogRead(4);
    digitalWrite(2, LOW);
    Serial.println(analogS);
    delay(1000);
    salinity = 2.0955*pow(10,-19)*pow(analogS,5.8106)*100.000;
    Serial.print("Salinity =");
    Serial.println(salinity);
    mySerial.write(254);
    mySerial.write(214);
    mySerial.print(salinity);
    mySerial.write(254);
    mySerial.write(222);
    mySerial.print((0.14227*val)-47.621,1);
    Serial.println("analog value");
    Serial.print(analogRead(4));
    float target= (salinity-(salinity-.1)*.60);
    float numer=((((target)*.08585-(salinity)*.08585)/(.1-(.1*.15+salinity*.85)))*60.0);
    float denom= ((.1));
    float valve= ((numer/denom)/10.00);

    if(.096<salinity>.104) {
        delay(5000);
    }

    if(salinity>.104) {
        Serial.print("Entering DIon Function");
        digitalWrite(6,HIGH);
        delay(valve);
        digitalWrite(6,LOW);
        delay(5000);
    }

    if(salinity<.096) {
        Serial.print("Entering Saltyon Function");
        digitalWrite(5,HIGH);
        delay(valve);
        digitalWrite(5,LOW);
        delay(5000);
    }

    Serial.print("time");
    Serial.println(valve);
}
  • 2
    So you are sure that the code parts in which a digitalWrite with 5 or 6 pin is executed, but you don't see the pin values chaning? Can you print out the value of valve, too, or put more delay after setting it HIGH? – Maximilian Gerhardt Feb 15 at 18:20
  • And what is printed out to serial monitor? – gre_gor Feb 15 at 18:25
  • 3
    What does if(.096<salinity>.104) evaluate (in plain english)? – user31481 Feb 15 at 18:26
  • @Juraj analogRead(5) selects the same ADC pin as analogRead(A5), which is the same as analogRead(19). (Even analogRead(13) will work). As it will subtract 14 if the value is bigger or equal to 14, and only use the lower 3 bits. – Gerben Feb 15 at 19:47
  • @Gerben, yes I remembered and deleted the comment (somehow wrong, because it is still there :-) ) – Juraj Feb 15 at 19:51

I think the problem is not in the output pin (which you say works elsewhere) but in your calculations. I made a Minimal, Complete, and Verifiable example of the part of the code that does the calculations, testing every possible reading you might have got from analogRead(4) (mainly because I found the calculations code rather obscure). Here it is:

float salinity;
void setup()
  {
  Serial.begin(9600);
  }

void loop()
  {
  for (int analogS=0; analogS < 1024; analogS++)
    {
    Serial.print ("analogS = ");
    Serial.print (analogS);
    salinity = 2.0955*pow(10,-19)*pow(analogS,5.8106)*100.000;
    Serial.print (", Salinity = ");
    Serial.print (salinity);
    float target= (salinity-(salinity-.1)*.60);
    float numer=((((target)*.08585-(salinity)*.08585)/(.1-(.1*.15+salinity*.85)))*60.0);
    float denom= ((.1));
    float valve= ((numer/denom)/10.00);
    Serial.print(",  valve = ");
    Serial.println(valve);
    }
   Serial.flush ();
   exit (1);
  }

Testing that reveals:

analogS = 0, Salinity = 0.00,  valve = 3.64
analogS = 1, Salinity = 0.00,  valve = 3.64
analogS = 2, Salinity = 0.00,  valve = 3.64
analogS = 3, Salinity = 0.00,  valve = 3.64
... some lines omitted ...
analogS = 1016, Salinity = 6.21,  valve = 3.64
analogS = 1017, Salinity = 6.25,  valve = 3.64
analogS = 1018, Salinity = 6.28,  valve = 3.64
analogS = 1019, Salinity = 6.32,  valve = 3.64
analogS = 1020, Salinity = 6.35,  valve = 3.64
analogS = 1021, Salinity = 6.39,  valve = 3.64
analogS = 1022, Salinity = 6.43,  valve = 3.64
analogS = 1023, Salinity = 6.46,  valve = 3.64

As you can see, valve is always 3.64, so you will be doing this:

    digitalWrite(6,HIGH);
    delay(3);
    digitalWrite(6,LOW);

In other words, port 6 will be HIGH for 3 milliseconds, probably too short a time for you to notice. Ditto for port 5.


Also this doesn't do what I suspect you think it does:

if(.096<salinity>.104) {
    delay(5000);
}

That will evaluate .096 < salinity to be 0 or 1. Then it will evaluate:

0 > 0.104
1 > 0.104

This is not what you are intending, I think. Try:

if(salinity < .096 || salinity > .104) {
    delay(5000);
}

If that is what you mean, I can't be sure - maybe you mean the opposite:

if(salinity > .096 && salinity < .104) {
    delay(5000);
}

If you write things out clearly, without trying to do shortcuts, it will be much more obvious what the test really is.

  • When I first saw .096<salinity>.104 I thought it was a oscure new C++ feature, like a range validator operand. So neat: is salinity in this range? But no, ii means another thing. BTW, Java doesn't accept such expression, because it compares a boolean with a float. The problem with C/C++ is too much power for the unexperienced guy. Think of you trying to debug a program like these. C/C++ is full of traps. – user31481 Feb 16 at 1:21
  • I don't mean to demean the OP, whom is just a beginner, but the many C/C++ pitfalls. – user31481 Feb 16 at 1:25
  • I think Cobol used to work like that, however that is pretty old. :) – Nick Gammon Feb 16 at 2:27

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.