Braces help!!

Hi,

Can some one please explain where my code is going wrong?
it wont compile i cant see what the problem is im sure its obvious to some one a bit more talented.
The code is written to “listen” on software Serial to the nextion screen.
the user inputs a value on the screen and presses button “A”

In theory this sends “A” and the value to the arduino.

An if statement checks to see if its an “A” if it is then it uses another if statement to decide on wether to turn an led on or off.

this is part of a bigger project but the very beginning.

side note i have had a very basic version of this code working but the LED only lit up dimly, so i assume that it is too slow to be on full brightness.

please dont be too mean about the sloppy code, im still learning…

#include <SoftwareSerial.h>
SoftwareSerial Serial1(10, 11); // RX, TX

const ledpin (12); //used for debugging
int setting1; //value stored from nextion
String dfd_global;

void setup() {
   Serial1.begin(9600); //serial from nextion display
   Serial.begin(115200); //used for debugging
}

void loop() {

  if(Serial1.available()){
    char character;
    String  dfd;
    {
    while(Serial1.available())
       character=char(Serial1.read());
    }
    
    if(dfd_global.startsWith("A")){        // To check which button the value has come from
       dfd_global.remove(0,1);             // removes the prefix
       Serial.println("A=" + dfd_global);
       int setting1=dfd_global.toInt();
    }
    Serial.print(setting1);{                // used for debugging
    if (setting1 >= 2)
      digitalWrite (ledpin, HIGH);
    else
      digitalWrite (ledpin, LOW);
    }
}

Thanks in advance

#include <SoftwareSerial.h>
SoftwareSerial Serial1(10, 11); // RX, TX

const byte ledpin (12); //used for debugging
int setting1; //value stored from nextion
String dfd_global;

void setup()
{
  Serial1.begin(9600); //serial from nextion display
  Serial.begin(115200); //used for debugging
}

void loop()
{
  if (Serial1.available())
  {
    char character;
    String  dfd;
    {
      while (Serial1.available())
        character = char(Serial1.read());
    }

    if (dfd_global.startsWith("A"))        // To check which button the value has come from
    {
      dfd_global.remove(0, 1);            // removes the prefix
      Serial.println("A=" + dfd_global);
      int setting1 = dfd_global.toInt();
    }
    Serial.print(setting1);                 // used for debugging
    {
      if (setting1 >= 2)
        digitalWrite (ledpin, HIGH);
      else
        digitalWrite (ledpin, LOW);
    }
  }
  
} //END of loop()

I'd say you are missing one at the end of the while line,
and then 2 at the end.
Use CTRL-T to help find them.

You are reading your serial into the variable 'character' but then not doing anything with it. You are looking for "A" in your dfd String. It appears you forget to append 'character' to the end of dfd while reading

dfd += character;

BUT.... You should abandon the use of the String class. It will only lead to strange problems as it fragments memory. A much better approach would be to read Robin2's Serial Input Basics tutorial and do it that way.

      while (Serial1.available())  // <-----------------------!
        character = char(Serial1.read());
    }

Not only that but how many characters do you think your variable ‘character’ can hold at once?
How many could come in on the serial port during the time while (Serial1.available()) is true?

That’s as far as I’ve looked so I don’t guarantee there are not other problems.

Hi Thanks for the replies folks.

I've taken what you've all said on board and will use the above tutorial to better my code skills.

Before I get to deep into this is what I want possible?

Here is what I would like to accomplish.
User inputs a target weight on nextion and then presses "set"
This weight value is stored as an integer in Arduino
The user then goes to a motor speed page. And sets three different speeds from 0-100 for example
Each one is then also stored in more integers.

Then when the go button is pressed. The Arduino looks at target weight compares it to the load cell and then starts the motor at speed 1.
The Arduino is always checking the load cell value and when it reaches say 80% of target weight it changes to speed 2 (lower)
And then when it reaches say 95% it slows down to speed 3 (lowest)

I'm not asking for this to be written for me by any means but a few pointers here and there would be great as above.

Thanks
Andy

Sounds totally doable. I would suggest you attack this in pieces.

  1. attach your load cell. Get it working. Make sure you can print out the value to the Serial Monitor and that is actually changing as the load changes. Save this sketch.

  2. attach your motor using the appropriate motor shield. Get it working. Make sure you can turn it on/off and vary the speed just by using constants in the sketch. Save this sketch.

  3. Get your Nextion working. Make sure you can display different pages/screens and get user input reliably. Print this user input to to the Serial Monitor to verify you are getting what you desire. Save this Sketch.

  4. Start combining the above 3 sketches together - slowly adding in new features. Make sure something works before adding in more. Build up your final solution.

If you get stuck, post back with your code and your trouble and people will help.

Hmm that makes logical sense.

I'll get onto that and get back to you all

Thanks