Click here to Skip to main content
15,905,963 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
//This program is used to control a robot using a app
//Error Code Chart: Code 01; Turnradius is higher than Speed; Code 02; Speed is higher than 255;
#define in1 5 //L298n Motor Driver pins.
#define in2 6
#define in3 10
#define in4 11
#define LED 13
int command; //Int to store app command state.
int Speed = 204; // 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.
int pinmode(int, OUTPUT)
int analogwrite(int, Speed)
int digitalwrite(int, High)
void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(LED, OUTPUT); //Set the LED pin.
  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;
    }
    Speedsec = Turnradius;
    if (brkonoff == 1) {
      brakeOn();
    } else {
      brakeOff();
    }
  }
}

void forward() {
  analogWrite(in1, Speed);
  analogWrite(in3, Speed);
}

void back() {
  analogWrite(in2, Speed);
  analogWrite(in4, Speed);
}

void left() {
  analogWrite(in3, Speed);
  analogWrite(in2, Speed);
}

void right() {
  analogWrite(in4, Speed);
  analogWrite(in1, Speed);
}
void forwardleft() {
  analogWrite(in1, Speedsec);
  analogWrite(in3, Speed);
}
void forwardright() {
  analogWrite(in1, Speed);
  analogWrite(in3, Speedsec);
}
void backright() {
  analogWrite(in2, Speed);
  analogWrite(in4, Speedsec);
}
void backleft() {
  analogWrite(in2, Speedsec);
  analogWrite(in4, Speed);
}

void Stop() {
  analogWrite(in1, 0);
  analogWrite(in2, 0);
  analogWrite(in3, 0);
  analogWrite(in4, 0);
}

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 (buttonState == 'S') {
      if (lastButtonState != buttonState) {
        digitalWrite(in1, HIGH);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, HIGH);
        delay(brakeTime);
        Stop();
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }
}
void brakeOff() {

}


What I have tried:

it gives alot of errors, this is the coding of arduino but i have to do this on turbo C++.
Posted
Updated 22-Jan-17 0:40am
Comments
Patrice T 22-Jan-17 6:31am    
Define 'it gives alot of errors'
Member 12962004 22-Jan-17 6:43am    
giving errors like:
16:OUTPUT has not declared
17: expected init-declarator before "int"
17: expected "or" before "int"
in function "void loop()"
29: "serial" undeclared
31: "stop" undeclared
and many other like this in dev c++
Michael_Davies 22-Jan-17 6:56am    
Semi-colons missing from the following:

int pinmode(int, OUTPUT)
int analogwrite(int, Speed)
int digitalwrite(int, High)
Member 12962004 22-Jan-17 6:58am    
when i put semi colon it give again the same errors
Michael_Davies 22-Jan-17 7:46am    
Show your amended code and the errors now.

1 solution

That code was written for Arduino and uses methods from libraries, that you have only on Arduino...
A simple Turbo C project will not have all those libraries, so you will have a few compilation error.
End even you will link somehow to the right libraries, the compiled code will fail on Arduino and on your computer as well...

So if you want to play with Arduino and robotics, than use Arduino IDE and board...
If you want to play with Turbo C and robotics, search for the right sample...
 
Share this answer
 
Comments
Member 12962004 22-Jan-17 6:45am    
can you help me to do this in turbo c++ or dev?
Kornfeld Eliyahu Peter 22-Jan-17 6:53am    
Why do you ask questions if you are neglect the answers - make up your own answers!!!
Member 12962004 22-Jan-17 6:58am    
i didn't neglect your answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900