TinkerCAD Ardunio code error too few arguments

552 views Asked by At

I'm building a robot for my class, and we have to have 2 servos and 1 DC motor working in a specific way. Everything is hooked up to an arduino uno, and my code works, but I using tinkercad to test a few things but I'm getting an error which is stopping my code from functioning in tinker cad, and I'm at a total loss.

ERROR

In function 'void loop()': 
44:9: error: too few arguments to function 'void motor(char, char)' 
17:6: note: declared here 
exit status 1

CODE

#include <Servo.h>   // set servo header to let ardduino know you intend to use a servo

Servo mycontinuousservo;      // declare servos to be used
Servo mydegreeservo;

int In1 = 7;        // declare your global variables to indicate pin numbers
int In2 = 8;
int pin = 6;
int servocontinuouspin = 10;
int servodegreepin = 9;
int angle = 90;

void servopos();
void servocontinous();
void motor(char Speed,char Direction);


void setup() {
  
  // put your setup code here, to run once:

  pinMode(In1, OUTPUT);
  pinMode(In2, OUTPUT);
  pinMode(pin, OUTPUT);

  digitalWrite(In1, HIGH);  //pin 7 moves forward
  digitalWrite(In2, LOW);   //pin 8 moves forward

  analogWrite(pin, 0);      // start  at 0
  pinMode(servocontinuouspin, OUTPUT);
  pinMode(servodegreepin, OUTPUT);
  mycontinuousservo.attach(servocontinuouspin);
  mydegreeservo.attach(servodegreepin);
  mycontinuousservo.write(90);

  Serial.begin(9600); // for serial communication
}
void loop() {
  
  servocontinous(); //call by ref aforedeclared functions
  servopos();
  motor();

}

// EXIT THE LOOP

void servopos() {         //position function
  int degree = 0;
  int i = 0;
  for (i = 0; i < 18; i++) {
    mydegreeservo.write(degree);
    delay(500); //delay 0.5 seconds
    degree = degree + 10;
  }

}

void servocontinous()    // continous servo settings
{
  for (int angle = 90; angle >= 0; angle--) {
    mycontinuousservo.write(angle);
    delay(50);
  }
  if (angle == 0) {
    Serial.print("speed\n");
  }
  for (angle = 0; angle < 90; angle++)
  {
    mycontinuousservo.write(angle);
    delay(50);
  }

}

void motor()            //motor function
{
  char Speed = 0;
  char Direction = 0;

  if (Serial.available() > 0)          //initialising
  {
    if (Direction == 'f')            //70 representing F on the ASCII table
    {
      delay(500);
      Serial.println("F");
    }
    if (Direction == 'r')
    {
      delay(500);
      Serial.println("R");
    }
  }
  if (Serial.available() > 0)
  {
    Speed = Serial.read();

    if (Speed == '0')
    {
      Speed = 0;
      Serial.println("Speed 0");
    }
    if (Speed == '1')
    {
      Speed = 14;
      Serial.println("Speed 1");
    }
    if (Speed == '2')
    {
      Speed = 29;
      Serial.println("Speed 2");
    }
    if (Speed == '3')
    {
      Speed = 42;
      Serial.println("Speed 3");
    }
    if (Speed == '4')
    {
      Speed = 56;
      Serial.println("Speed 4");
    }
    if (Speed == '5')
    {
      Speed = 70;
      Serial.println("Speed 5");
    }
    if (Speed == '6')
    {
      Speed = 84;
      Serial.println("Speed 6");
    }
    if (Speed == '7')
    {
      Speed = 98;
      Serial.println("Speed 7");
    }
    if (Speed == '8')
    {
      Speed = 112;
      Serial.println("Speed 8");
    }
    if (Speed == '9')
    {
      Speed = 128;
      Serial.println("Speed 9");
    }
  }  delay(5000);
  analogWrite(pin, Speed);
  if (Direction == 'f')
  { digitalWrite(In1, HIGH);
    digitalWrite(In2, LOW);
  } if (Direction == 'r')
  {
    digitalWrite(In1, LOW);
    digitalWrite(In2, HIGH);
  }
}

1

There are 1 answers

0
tadman On

Here you declare the function as taking two arguments:

void motor(char Speed,char Direction);

Later you call it with no arguments, which is invalid when compared to that declaration:

motor();

This will be an immediate compiler error. That function is described as having two arguments, you call it with zero. Compile hard fails and stops because of this contradiction.

Yet when you define it the arguments are gone, they're actually local variables:

void motor()            //motor function
{
  char Speed = 0;
  char Direction = 0;
  // ...
}

This too contradicts the earlier declaration, so if you comment out the place where it's called you'll likely get a different error.

Local variables are the private business of a function, they do not need to be shown in the function signature, so don't think these need to be included as arguments.

What you need to do is either snip the arguments from the declaration, make sure that declaration matches the function signature exactly, or move the motor() function definition to before where it is first called.

I prefer to organize things so that pre-declaration is not necessary, or at least minimized. There's no reason to not put the motor() definition before loop().