2

HI I would like to control 8 x Pumps with Bluetooth on my arduino... I whant to change number '1' for 'ON' to '1on' and '2' for 'OFF' to '1off', than '2on' , '2off' , '3on' , '3off' etc. but I don't know how because I'm not a coder... Here is my code:

int PUMP1 = 2;
int PUMP2 = 3;
char value = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(PUMP1, OUTPUT);
  pinMode(PUMP2, OUTPUT);
}

void loop()
{
  if (Serial.available())
  {
    value = Serial.read();
    if (value == '1') digitalWrite(PUMP1, LOW);                 //relay1 on
    else if (value == '0') digitalWrite(PUMP1, HIGH);           //relay1 off
    if (value == '3') digitalWrite(PUMP2, LOW);                //relay2 on
    else if (value == '2') digitalWrite(PUMP2, HIGH);           //relay2 off
  }
}
001
  • 13,291
  • 5
  • 35
  • 66
tanjamaya
  • 23
  • 1
  • 8
  • 1
    Not sure I understand. I think you want [`Serial.readString()`](https://www.arduino.cc/en/Serial/ReadString). – 001 Apr 06 '17 at 18:09
  • Looks about right, but I can't say I much like that doc page. "Returns A string read from the serial buffer" What kind of string? std::string? NUL terminated string? Ball of string? – user4581301 Apr 06 '17 at 18:12
  • if (value == '1') digitalWrite(PUMP1, LOW); to be like this if (value == '1on') digitalWrite(PUMP1, LOW); this else if (value == '0') digitalWrite(PUMP1, HIGH); ti be like this else if (value == '1off') digitalWrite(PUMP1, HIGH); – tanjamaya Apr 06 '17 at 18:17
  • @user4581301 I agree. It does not even clearly specify how it delineates a complete string. Nul byte, \r, \n, timeout? – 001 Apr 06 '17 at 18:20
  • as I say I don't know coding... so it would be nice if anybody can help me – tanjamaya Apr 06 '17 at 18:24
  • 1
    [I highly recommend learning at least the basics of C++ first](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), as any provided answer will be wasted if you cannot understand it. The alternative is writing the program for you, and I think I can speak for @JohnnyMopp when I say we aren't going to do that. – user4581301 Apr 06 '17 at 18:30

1 Answers1

2

You need to replace Serial.read() with Serial.readStringUntil('\n'). The strings will need to be sent with a trailing new line character.

void loop()
{
    if (Serial.available())
    {
        String value = Serial.readStringUntil('\n');
             if (value == "1on")  digitalWrite(PUMP1, LOW);           //relay1 on
        else if (value == "1off") digitalWrite(PUMP1, HIGH);          //relay1 off
        else if (value == "2on")  digitalWrite(PUMP2, LOW);           //relay2 on
        else if (value == "2off") digitalWrite(PUMP2, HIGH);          //relay2 off
        // and so on
    }
}

Serial.readString() was suggested in the comments, but that will lead to 1 sec of delay (by default). In this case the string is considered complete when there is no data for certain amount of time. And it will be impossible to control multiple relays at the same time.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • Thank you gre_gor. That's the missing delimiter and return type information we were wondering about in the comments. `String` and hard-delimited by time. – user4581301 Apr 06 '17 at 19:00
  • 2
    @tanjamaya If this answered your question, you should accept the answer. – gre_gor Apr 06 '17 at 23:09