I dont know what Im doing and need help

I'm new to the whole coding thing and wanted to test to see if I could make an RC car. I just don't know what I'm doing and get a lot of error messages with no idea on how to fix them. There's supposed to be about 4 moving parts, 3 servos, 1 motor, using a bluetooth module to control from my phone. The code is currently
char inputByte = 'z';
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
while (Serial.available() > 0) {
inputByte = Serial.read();

if (inputByte == 'Z')
{ digitalWrite(13, HIGH);
}
else (inputByte == 'z')
{
  digitalWrite(13, LOW);
}

}
Servo headlights;
Servo left;
Servo right;
void loop()
char inputByte = 'z';

int pos = 0; // headlights pos

void setup() {
headlights.attach(9);
}

Serial.begin(9600);
pinMode(9, OUTPUT);
void loop() {
while (Serial.available() > 0) {
inputByte = Serial.read();
Serial.println(c);
if (inputByte == 'Y') {
void loop() {
for (pos = 0; pos <= 90; pos += 1) {
// in steps of 1 degree
headlights.write(pos);
delay(150);
}
else if (inputByte == 'y') {
for (pos = 90; pos <= 0; pos -= 1) {
// in steps of 1 degree
headlights.write(pos);
delay(1000); // headlight control
}
void setup() {
left.attach(10); // Left wheel
}

        void loop() {
          for (pos = 0; pos <= 300; pos -= 1) {
            // in steps of 1 degree
            left.write(pos);
            delay(1000);
          }
          for (pos = 300; pos >= 0; pos += 1) {
            /
            left.write(pos);
            delay(10);
          }
          void setup() {
            right.attach(11);
          }

          void loop() {
            for (pos = 0; pos <= 60; pos += 1) { //
              // in steps of 1 degree
              right.write(pos);              //
              delay(1000);                       //
            }
            for (pos = 60; pos >= 0; pos -= 1) { //
              right.write(pos);              //
              delay(15);                       //
            }
          }

and I haven't been able to understand what to fix so far
error messages are
Arduino: 1.8.20 Hourly Build 2021/12/20 07:33 (Windows 10), Board: "Arduino Uno"

test_-_FD:11:6: error: 'else' without a previous 'if'

 ;else (inputByte == 'z')

  ^~~~

test_-_FD:12:5: error: expected ';' before '{' token

 {

 ^

test_-_FD:16:3: error: 'Servo' was not declared in this scope

Servo headlights;

^~~~~

C:\Users\myste\Documents\Arduino\test_-FD\test-_FD.ino:16:3: note: suggested alternative: 'Serial'

Servo headlights;

^~~~~

Serial

test_-_FD:17:9: error: expected ';' before 'left'

Servo left;

     ^~~~

test_-_FD:18:9: error: expected ';' before 'right'

Servo right;

     ^~~~~

test_-_FD:20:3: error: expected initializer before 'char'

char inputByte = 'z';

^~~~

test_-_FD:25:16: error: a function-definition is not allowed here before '{' token

void setup() {

            ^

test_-_FD:31:15: error: a function-definition is not allowed here before '{' token

void loop() {

           ^

test_-_FD:77:15: error: expected '}' at end of input

           }

           ^

exit status 1

'else' without a previous 'if'

Any tips??

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Welcome to the forum

There is so much wrong that it is difficult to know where to start

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  while (Serial.available() > 0)
  {
    inputByte = Serial.read();
    if (inputByte == 'Z')
    {
      digitalWrite(13, HIGH);
    }
    else (inputByte == 'z')
    {
      digitalWrite(13, LOW);
    }
  }

How long do you think that the while loop will wait for input ?

  Servo headlights;
  Servo left;
  Servo right;
  void loop()
  char inputByte = 'z';
  int pos = 0; // headlights pos
  void setup()
  {
    headlights.attach(9);
  }

Why are you trying to call the loop() function here and in multiple other places in the code and why do you have multiple setup() and loop() functions ? You can only have one of each in a sketch

  Serial.begin(9600);
  pinMode(9, OUTPUT);

This code is not in a function

  void loop()
  {
    while (Serial.available() > 0)
    {
      inputByte = Serial.read();
      Serial.println(c);
      if (inputByte == 'Y')
      {
        void loop()
        {

This may be where you want to start the definition of the loop() function, but I can't tell. Again, how long will the while loop wait and why are you calling loop() again ?

I stopped looking at that point

It seems that you have not understood the fundamental structure of an Arduino sketch. I assume that you have looked at the examples. One setup() function that executes once and one loop() function that executes repeatedly

@corneliuspb

An Arduino sketch has only one setup() function, one loop() function and one or none or none user-defined functions. Your program has many setup() and loop() functions that have introduced difficulties to guide you in the right direction. Anyway, try to execute the following steps and see what happens:

1. Connect your BT (Bluetooth) device with UNO as per Fig-1.
hc5-1y
Figure-1:

2. Upload the following sketch (your one with some modifications):

#include<SoftwareSerial.h>
SoftwareSerial SUART(3, 2);

#include<Servo.h>
Servo headlights;

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  headlights.attach(9);
}

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    char x = SUART.read();
    if (x == 'Y')
    {
      headlights.write(60);//turn Sero by 90 degree.
    }
    if (x == 'y')
    {
      headlights.write(30); //turn servo by 30 degree
    }
  }
}

3. Pair your BT with your Android Mobile Phone.
4. Enter Y from Phone and check that the Servo has turned by 60 degree.
5. Enter y from Phone and check that Servo has moved to 30 degree position.
6. If Step-4 and 5 work, then add your codes with the sketch of Step-2 to realize your objectives.

Thank you, I’ll test it as soon as I possibly can. And thank you for letting me know about the loop functions, coding’s new to me so I’m still trying to figure it all out.

This is (Fig-1) the standard sketch of the Arduino IDE. Always, start with this template and put your codes line-by-line with good judgement.

Tasks that are executed only once or for a definite number of times, those are placed in the setup() function. Tasks that will be executed again and again (repetitively) will go in the loop() function.

Try to make your program modular by calling user-defined functions (UDF) from loop() as needed. This practice will help you keeping your loop() function clean and less populated by code lines.

arduinoSketch
Figure-1:

So… have you done anything at all before this dive into the deep end of the pool?

It is recommended that you play a bit with vastly simpler examples to get acquainted with the IDE, the C/C++ language and the particular structure of an Arduino program.

The IDE has many examples, I suggest you work through a few of them until you can write a sketch, even a simple one, understand and fix errors the compiler will throw at you and start to incorporate more features to something as simple as

pushbutton make LED go on and off

level of complexity.

Getting an r/c car to work would be a challenge even if you were fluent in C/C++ and could do the IDE dance in you sleep.

I know it's a drag, but there is no instant or even rapid gratification to be found in programming for real. No one was born knowing anything about it! No one was writing significant programs a week or two after thinking it might be fun to try.

It will be fun, but there's work you'll have to do yourself. Look around (google) for a tutorial series that matches your learning style. Maybe you like the old man that drones on and on, maybe you'd rather watch the cute chick who is a bit more breezy. Maybe you'd rather read than watch.

Give anything that looks plausible 5 or 10 minutes. You'll learn something about programming, probably, but you'll def know if a love match is in the offing or whether to ditch a turkey and try another.

Oh, welcome to the community and we here to help if you stumble on something that makes the wrong kind of sense to you.

a7

I've looked into C++ and C# before, my father talked to me about it once but that was a couple of years ago. About a year ago I wanted to get back into it but never found the time for it. I've looked at the fairly simple coding examples and then tried to make something out of that but had no real idea on how to change it from turning a led on and off to turning a motor on and off. I then ran into having no real idea on what to do with the servo situation, luckily I've been told about the function issue I had and somebody provided me an example on what to do and I've since then tried to add on the other 2 servo lines of code. I don't run into any more error messages but would still like any tips or advice on how to add an on/off for a motor, no changing direction nor controlling speed, just an on or off, if possible.

#include<SoftwareSerial.h>
SoftwareSerial SUART(3, 2);

#include<Servo.h>
Servo headlights;
void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  headlights.attach(9);
}

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    char x = SUART.read();
    if (x == 'Y')
    {
      headlights.write(63);//
    }
    if (x == 'y')
    {
      headlights.write(127);
    }
    Servo right;
    {
      char x = SUART.read();
      if (x == 'Z')
      {
        right.write(34);
      }
      if (x == 'z')
      {
        right.write(-27);
      }
    }
  }
}

You can post code by using this method that adds the code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Thank you! I'm currently not at home and am doing the coding on codebender.cc while at school. I'll remember that next time I upload code while using Arduino-IDE!

I redid the code and got

#include<SoftwareSerial.h>
SoftwareSerial SUART(3, 2);

#include<Servo.h>
Servo headlights;
void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  headlights.attach(9);
}

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    char x = SUART.read();
    if (x == 'Y')
    {
      headlights.write(63);//
    }
    if (x == 'y')
    {
      headlights.write(127);
    }
    Servo right;
    {
      char x = SUART.read();
      if (x == 'Z')
      {
        right.write(34);
      }
      if (x == 'z')
      {
        right.write(-27);
      }
    }
  }
}

I do need to do some more refining but at the moment it works, how could I implement a motor on/off button? That's all I have left and then the code should be done!

You never call right.attach() so your 'right' servo won't do anything.

Note: 'right.write(-27);' makes no sense since servos have a range of 0 to 180. Perhaps you meant '90+34' and '90-27' in place of 34 and -27.

Your second "char x = SUART.read();" will pull a character out of the buffer. That character will never be checked for 'Y' or 'y'.

This is your code modified to be more likely to work:

#include<SoftwareSerial.h>
SoftwareSerial SUART(3, 2);

#include<Servo.h>
Servo headlights;
Servo right;

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  headlights.attach(9);
  right.attach(10);
}

void loop()
{
  if (SUART.available())
  {
    char x = SUART.read();
    // switch(x) statement is shortcut for a bunch 
    // of 'if (x == constant)' statements
    switch (x)
    {
      case 'Y': headlights.write(63); break;
      case 'y': headlights.write(127); break;
      case 'Z': right.write(90 + 34); break;
      case 'z': right.write(90 - 27); break;
    }
  }
}

Are headlights.write(); and break; sub-ordinate clauses to case 'Y': case label?

After Auto Formatting the line of code in the IDE the result is

case 'Y':
  headlights.write(63);
  break;

Is that clearer ?

Vert much!

What's about:

case 'Y':
{
  headlights.write(63);
  break;
}

No. They are just statements. The case labels are just a kind of label. The equivalent code is, roughly:

  {
    // switch (x)
    if (x == 'Y')
      goto LabelY;
    else if (x == 'y')
      goto Labely;
    else if (x == 'Z')
      goto LabelY;
    else if (x == 'z')
      goto Labely;
    else
      goto Label;

LabelY:
    headlights.write(63);
    break;
Labely:
    headlights.write(127);
    break;
LabelZ:
    right.write(90 + 34);
    break;
Labelz:
    right.write(90 - 27);
    break;
Label:
  }

Note: If you define a 'default:' case, that takes the place of 'Label:'

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.