-1

Sketch:

//XPERT MIND.
//This program is used to control a robot using a app that communicates with Arduino through a bluetooth module.
//Error Code Chart: Code 01; Turnradius is higher than Speed; Code 02; Speed is higher than 255;
#include "Arduino.h"
#include "SoftwareSerial.h"

#define in1 3 //L298n Motor Driver pins.
#define in2 4
#define in3 5
#define in4 6
#define enA 2
#define enB 7
#define klaksonON 8
#define klaksonOFF  8
#define ledON 9
#define ledOFF 9

int command; //Int to store app command state.
int Speed = 255; // 0 - 255.
int Speedsec;
int buttonState = 0;
int lastButtonState = 0;
int Turnradius = 0; //Set the radius of a turn, 0 - 255 Note:the robot will malfunction if this is higher than int Speed.
int brakeTime = 45;
int brkonoff = 1; //1 for the electronic braking system, 0 for normal.
void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(ledOFF, OUTPUT); //Set the LED pin.
  pinMode(ledON, OUTPUT); //Set the LED pin.
  pinMode(klaksonOFF, OUTPUT);
  pinMode(klaksonON, OUTPUT);

  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.

}

void loop() {
  if (Serial.available() > 0) {
    command = Serial.read();
    Stop(); //Initialize with motors stoped.

    switch (command) {
      case 'F':
        forward();
        break;
      case 'B':
        back();
        break;
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
      case 'G':
        forwardleft();
        break;
      case 'I':
        forwardright();
        break;
      case 'H':
        backleft();
        break;
      case 'J':
        backright();
        break;
      case '0':
        Speed = 100;
        break;
      case '1':
        Speed = 140;
        break;
      case '2':
        Speed = 153;
        break;
      case '3':
        Speed = 165;
        break;
      case '4':
        Speed = 178;
        break;
      case '5':
        Speed = 191;
        break;
      case '6':
        Speed = 204;
        break;
      case '7':
        Speed = 216;
        break;
      case '8':
        Speed = 229;
        break;
      case '9':
        Speed = 242;
        break;
      case 'q':
        Speed = 255;
        break;
      case 'V':
        klaksonON();
        break;
      case 'v':
        klaksonOFF();
        break;
      case 'W':
        ledON();
        break;
      case 'w':
        ledOFF();
        break;
    }

    Speedsec = Turnradius;
    if (brkonoff == 1) {
      brakeOn();
    } else {
      brakeOff();
    }
  }
}

void forward() {
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  analogWrite(in2, Speed);
  analogWrite(in4, Speed);
}

void back() {
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  analogWrite(in1, Speed);
  analogWrite(in3, Speed);
}

void left() {
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  analogWrite(in4, Speed);
  analogWrite(in1, Speed);
}

void right() {
  digitalWrite(enB, HIGH);
  digitalWrite(enA, HIGH);
  analogWrite(in3, Speed);
  analogWrite(in2, Speed);
}
void forwardleft() {
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  analogWrite(in2, Speedsec);
  analogWrite(in4, Speed);
}
void forwardright() {
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  analogWrite(in2, Speed);
  analogWrite(in4, Speedsec);
}
void backright() {
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  analogWrite(in1, Speed);
  analogWrite(in3, Speedsec);
}

void backleft() {
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
  analogWrite(in1, Speedsec);
  analogWrite(in3, Speed);
}

void Stop() {
  analogWrite(in1, 0);
  analogWrite(in2, 0);
  analogWrite(in3, 0);
  analogWrite(in4, 0);
}
void klaksonON() {
   tone(klaksonON, 1000); //Wygeneruj sygnał o częstotliwości 1000Hz na pinie 8

}
void klaksonOFF() {
   noTone(klaksonOFF);

}
void ledON() {

}
void ledOFF() {

}


void brakeOn() {
  //Here's the future use: an electronic braking system!
  // read the pushbutton input pin:
  buttonState = command;
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (lastButtonState == 'F') {
      if (buttonState == 'S') {
        back();
        delay(brakeTime);
        Stop();
      }
    }
    if (lastButtonState == 'B') {
      if (buttonState == 'S') {
        forward();
        delay(brakeTime);
        Stop();
      }
    }
    if (lastButtonState == 'L') {
      if (buttonState == 'S') {
        right();
        delay(brakeTime);
        Stop();
      }
    }
    if (lastButtonState == 'R') {
      if (buttonState == 'S') {
        left();
        delay(brakeTime);
        Stop();
      }
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}
void brakeOff() {

}

Output:

--------------------------------------------------------------
Arduino:1.8.5 (Windows 10), Płytka:"Arduino/Genuino Uno"

C:\Users\jjayy\Desktop\Samochodzik_z_lixbota\Samochodzik_z_lixbota.ino\Samochodzik_z_lixbota.ino.ino: In function 'void setup()':

C:\Users\jjayy\Desktop\Samochodzik_z_lixbota\Samochodzik_z_lixbota.ino\Samochodzik_z_lixbota.ino.ino:35:21: warning: invalid conversion from 'void (*)()' to 'uint8_t {aka unsigned char}' [-fpermissive]

   tone(Klakson, 1000); //Wygeneruj sygnał o częstotliwości 1000Hz na pinie 8

                     ^

In file included from C:\Users\jjayy\Desktop\Samochodzik_z_lixbota\Samochodzik_z_lixbota.ino\Samochodzik_z_lixbota.ino.ino:4:0:

C:\Users\jjayy\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Arduino.h:246:6: note: initializing argument 1 of 'void tone(uint8_t, unsigned int, long unsigned int)'

 void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);

      ^

C:\Users\jjayy\Desktop\Samochodzik_z_lixbota\Samochodzik_z_lixbota.ino\Samochodzik_z_lixbota.ino.ino: In function 'void loop()':

Samochodzik_z_lixbota.ino:104: error: expression cannot be used as a function

         klaksonON();

                   ^

Samochodzik_z_lixbota.ino:107: error: expression cannot be used as a function

         klaksonOFF();

                    ^

Samochodzik_z_lixbota.ino:110: error: expression cannot be used as a function

         ledON();

               ^

Samochodzik_z_lixbota.ino:113: error: expression cannot be used as a function

         ledOFF();

                ^

C:\Users\jjayy\Desktop\Samochodzik_z_lixbota\Samochodzik_z_lixbota.ino\Samochodzik_z_lixbota.ino.ino: In function 'void Klakson()':

C:\Users\jjayy\Desktop\Samochodzik_z_lixbota\Samochodzik_z_lixbota.ino\Samochodzik_z_lixbota.ino.ino:186:29: warning: invalid conversion from 'void (*)()' to 'uint8_t {aka unsigned char}' [-fpermissive]

   digitalWrite(Klakson, HIGH);

                             ^

In file included from C:\Users\jjayy\Desktop\Samochodzik_z_lixbota\Samochodzik_z_lixbota.ino\Samochodzik_z_lixbota.ino.ino:4:0:

C:\Users\jjayy\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Arduino.h:134:6: note: initializing argument 1 of 'void digitalWrite(uint8_t, uint8_t)'

 void digitalWrite(uint8_t, uint8_t);

      ^

exit status 1
expression cannot be used as a function

Ten raport powinien zawierać więcej informacji jeśli w 
File -> Preferencje zostanie włączona opcja "Pokaż
szczegółowe informacje podczas kompilacji"

closed as unclear what you're asking by Greenonline, sempaiscuba, per1234, gre_gor, Michel Keijzers Sep 10 '18 at 8:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Where's the question? – Greenonline Sep 9 '18 at 19:53
1

I'd recommend to find some C++ basics tutorial.

Anyway Klakson is the function, not pin number definition.

On the other side, calling the klaksonON() (which is just #define) will be replaced to 8() which is nonsense here.

Basically you are confusing defines and functions.

  • Thank you :D I find my errors and i fixed them :) Yes i confused defines with function :p – NiebieskiBanan Sep 9 '18 at 17:31
0

Start with the first problem that the compiler finds, not the last one.

The name Klakson is confusing, it can be read as "Klaks-on". Can you call it Beep?
For a pin number, use the term pin. For example: const int beepPin = 8;.
Or, in your language const int klaksonPinie = 8;

Use a single #define or a single const int for a pin, not two.

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