Sie sind auf Seite 1von 5

// Include Libraries

#include "Arduino.h"
#include "BTHC05.h"
#include "DCMDriverL298.h"

// Pin Definitions
#define BTHC05_PIN_TXD 11
#define BTHC05_PIN_RXD 10
#define DCMOTORDRIVERL298_PIN_INT1 2
#define DCMOTORDRIVERL298_PIN_ENB 6
#define DCMOTORDRIVERL298_PIN_INT2 3
#define DCMOTORDRIVERL298_PIN_ENA 5
#define DCMOTORDRIVERL298_PIN_INT3 4
#define DCMOTORDRIVERL298_PIN_INT4 7

// Global variables and defines

// object initialization
BTHC05 bthc05(BTHC05_PIN_RXD,BTHC05_PIN_TXD);
DCMDriverL298
dcMotorDriverL298(DCMOTORDRIVERL298_PIN_ENA,DCMOTORDRIVERL298_PIN_INT1,DCMOTORDRIVE
RL298_PIN_INT2,DCMOTORDRIVERL298_PIN_ENB,DCMOTORDRIVERL298_PIN_INT3,DCMOTORDRIVERL2
98_PIN_INT4);

// define vars for testing menu


const int timeout = 10000; //define timeout of 10 sec
char menuOption = 0;
long time0;

// Setup the essentials for your circuit to work. It runs first every time your
circuit is powered with electricity.
void setup()
{
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println("start");

bthc05.begin(9600);
//This example uses HC-05 Bluetooth to communicate with an Android device.
//Download bluetooth terminal from google play store,
https://play.google.com/store/apps/details?id=Qwerty.BluetoothTerminal&hl=en
//Pair and connect to 'HC-05', the default password for connection is '1234'.
//You should see this message from your arduino on your android device
bthc05.println("Bluetooth On....");
menuOption = menu();

// Main logic of your circuit. It defines the interaction between the components
you selected. After setup, it runs over and over again, in an eternal loop.
void loop()
{
if(menuOption == '1') {
// HC - 05 Bluetooth Serial Module - Test Code
String bthc05Str = "";
//Receive String from bluetooth device
if (bthc05.available())
{
//Read a complete line from bluetooth terminal
bthc05Str = bthc05.readStringUntil('\n');
// Print raw data to serial monitor
Serial.print("BT Raw Data: ");
Serial.println(bthc05Str);
}
//Send sensor data to Bluetooth device
bthc05.println("PUT YOUR SENSOR DATA HERE");
}
else if(menuOption == '2') {
// L298N Motor Driver with Dual Hobby DC motors - Test Code
//Start both motors. note that rotation direction is determined by the motors
connection to the driver.
//You can change the speed by setting a value between 0-255, and set the
direction by changing between 1 and 0.
dcMotorDriverL298.setMotorA(200,1);
dcMotorDriverL298.setMotorB(200,0);
delay(2000);
//Stop both motors
dcMotorDriverL298.stopMotors();
delay(2000);

if (millis() - time0 > timeout)


{
menuOption = menu();
}

// Menu function for selecting the components to be tested


// Follow serial monitor for instrcutions
char menu()
{

Serial.println(F("\nWhich component would you like to test?"));


Serial.println(F("(1) HC - 05 Bluetooth Serial Module"));
Serial.println(F("(2) L298N Motor Driver with Dual Hobby DC motors"));
Serial.println(F("(menu) send anything else or press on board reset
button\n"));
while (!Serial.available());

// Read data from serial monitor if received


while (Serial.available())
{
char c = Serial.read();
if (isAlphaNumeric(c))
{
if(c == '1')
Serial.println(F("Now Testing HC - 05 Bluetooth Serial Module"));
else if(c == '2')
Serial.println(F("Now Testing L298N Motor Driver with Dual Hobby
DC motors"));
else
{
Serial.println(F("illegal input!"));
return 0;
}
time0 = millis();
return c;
}
}
}

/*******************************************************

* Circuito.io is an automatic generator of schematics and code for off


* the shelf hardware combinations.

* Copyright (C) 2016 Roboplan Technologies Ltd.

* This program is free software: you can redistribute it and/or modify


* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,


* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.

* In addition, and without limitation, to the disclaimers of warranties


* stated above and in the GNU General Public License version 3 (or any
* later version), Roboplan Technologies Ltd. ("Roboplan") offers this
* program subject to the following warranty disclaimers and by using
* this program you acknowledge and agree to the following:
* THIS PROGRAM IS PROVIDED ON AN "AS IS" AND "AS AVAILABLE" BASIS, AND
* WITHOUT WARRANTIES OF ANY KIND EITHER EXPRESS OR IMPLIED. ROBOPLAN
* HEREBY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY, TITLE, FITNESS
* FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND THOSE ARISING BY
* STATUTE OR FROM A COURSE OF DEALING OR USAGE OF TRADE.
* YOUR RELIANCE ON, OR USE OF THIS PROGRAM IS AT YOUR SOLE RISK.
* ROBOPLAN DOES NOT GUARANTEE THAT THE PROGRAM WILL BE FREE OF, OR NOT
* SUSCEPTIBLE TO, BUGS, SECURITY BREACHES, OR VIRUSES. ROBOPLAN DOES
* NOT WARRANT THAT YOUR USE OF THE PROGRAM, INCLUDING PURSUANT TO
* SCHEMATICS, INSTRUCTIONS OR RECOMMENDATIONS OF ROBOPLAN, WILL BE SAFE
* FOR PERSONAL USE OR FOR PRODUCTION OR COMMERCIAL USE, WILL NOT
* VIOLATE ANY THIRD PARTY RIGHTS, WILL PROVIDE THE INTENDED OR DESIRED
* RESULTS, OR OPERATE AS YOU INTENDED OR AS MAY BE INDICATED BY ROBOPLAN.
* YOU HEREBY WAIVE, AGREE NOT TO ASSERT AGAINST, AND RELEASE ROBOPLAN,
* ITS LICENSORS AND AFFILIATES FROM, ANY CLAIMS IN CONNECTION WITH ANY OF
* THE ABOVE.
********************************************************/
/** \addtogroup BTHC05
* @{
*/

// BTHC05.h

#ifndef _BTHC05_H
#define _BTHC05_H

#include <Arduino.h>
#include <SoftwareSerial.h>
#include "BTHC05.h"

/*
* Constructor for SoftwareSerial case.
*/
BTHC05::BTHC05(int pinRx, int pinTx) : SoftwareSerial(pinRx, pinTx)
{
pinMode(pinRx, INPUT);
pinMode(pinTx, OUTPUT);
begin(9600);
}
/** \addtogroup DCMotorWDriver
* @{
*/

#ifndef _DCMDRIVERL298_H_
#define _DCMDRIVERL298_H_

//DcMotor driver class:


class DCMDriverL298
{

public:
DCMDriverL298(const int enA, const int pinA1, const int pinA2, const
int enB, const int pinB1, const int pinB2);
void setMotorA(int speed, bool dir);
void setMotorB(int speed, bool dir);
void stopMotorA();
void stopMotorB();
void stopMotors();
void setMotor(int pinPWM, int pinDir1, int pinDir2, int speed, bool
dir);
void off(int pinPWM, int pinDir1, int pinDir2);
private:
const int m_enA, m_enB, m_pinA1, m_pinA2, m_pinB1, m_pinB2;

};

#endif // _DCMDRIVERL298_H_
/** @}*/
#include <Arduino.h>
#include "DCMDriverL298.h"

/**
* Construct a DC Motor Driver instance.<BR>
* It constructs two DC Motor instances, motorL and motorR.<BR>
* enA, enB - enable pins for motors. connected to PWM pin on Arduino board.<BR>
* pinA1,pinA2,pinB1,pinB2 - direction pin of the motors. connected to digital pins
on Arduino board.
*/
DCMDriverL298::DCMDriverL298(const int enA, const int pinA1, const int pinA2, const
int enB, const int pinB1, const int pinB2) : m_enA(enA), m_enB(enB),
m_pinA1(pinA1), m_pinA2(pinA2), m_pinB1(pinB1), m_pinB2(pinB2)
{
pinMode(m_pinA1,OUTPUT);
pinMode(m_pinA2,OUTPUT);
pinMode(m_pinB1,OUTPUT);
pinMode(m_pinB2,OUTPUT);
pinMode(m_enA,OUTPUT);
pinMode(m_enB,OUTPUT);

stopMotors();
}

/**Set DC motor A speed and direction.


*/
void DCMDriverL298::setMotorA(int speed, bool dir)
{
setMotor(m_enA, m_pinA1, m_pinA2, speed, dir);
}

/**Set DC motor B speed and direction.


*/
void DCMDriverL298::setMotorB(int speed, bool dir)
{
setMotor(m_enB, m_pinB1, m_pinB2, speed, dir);
}
/**Stop DC motor A.
*/
void DCMDriverL298::stopMotorA()
{
off(m_enA, m_pinA1, m_pinA2);
}
/**Stop DC motor B.
*/
void DCMDriverL298::stopMotorB()
{
off(m_enB, m_pinB1, m_pinB2);
}
/**Stop both DC motors.
*/
void DCMDriverL298::stopMotors()
{
stopMotorA();

Das könnte Ihnen auch gefallen