domingo, 30 de dezembro de 2018
sábado, 29 de dezembro de 2018
ESPERO AJUDAR OBG
#include <Psx.h>
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#define dataPin 4
#define cmndPin 3
#define attPin 2
#define clockPin 5
Psx Psx;
byte envio[8];
byte tx_endereco[5] = {0xD0,0xD0,0xD0,0xD0,0xD0};
void setup()
{
Serial.begin(57600);
Psx.setupPins(dataPin, cmndPin, attPin, clockPin, 10);
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.payload = 8;
Mirf.config();
}
void loop()
{
Psx.read();
if(Psx.psxID != 0x00)
{
envio[0] = Psx.psxID; // DIGITAL (82) , ANALOGICO (CE)
envio[1] = Psx.psxStatus;
envio[2] = Psx.psxThumbL; // SETAS, SELECT, DIGITAL L
envio[3] = Psx.psxThumbR; // (x,[],/\,O), START, DIGITAL R, (L1,L2,R1,R2)
envio[4] = Psx.psxJoyRX; // EIXO ANALOGICO "X" DO "R"
envio[5] = Psx.psxJoyRY; // EIXO ANALOGICO "Y" DO "R"
envio[6] = Psx.psxJoyLX; // EIXO ANALOGICO "X" DO "L"
envio[7] = Psx.psxJoyLY; // EIXO ANALOGICO "Y" DO "L"
Serial.print("Data 0 ==> ");
Serial.println(envio[2]);
Serial.print("Data 1 ==> ");
Serial.println(envio[3]);
Mirf.setTADDR(tx_endereco);
Mirf.send(envio);
while(Mirf.isSending());
}
delay(50);
}
//receptor//
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
byte entrada[8];
byte rx_endereco[5] = {0xD0,0xD0,0xD0,0xD0,0xD0};
uint32_t lastCommandTime = 0;
uint8_t data1, data2,data3, data4,data5, data6,data7;
void setup()
{
Serial.begin(57600);
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR(rx_endereco);
Mirf.payload = 8;
Mirf.config();
}
void loop()
{
if(!Mirf.isSending() && Mirf.dataReady())
{
Mirf.getData(entrada);
data1 = ~entrada[1];
data2 = ~entrada[2];
data3 = ~entrada[3];
data4 = ~entrada[4];
data5 = ~entrada[5];
data6 = ~entrada[6];
data7 = ~entrada[7];
/* data 1
* 1 = Seta para esquerda / analogico L para esquerda
* 2 = Seta para baixo / analogico L para baixo
* 4 = seta para direita / analogico L para direita
* 8 = seta para cima / analogico L para cima
*/
/* data 2
* 1 = [] / analogico R para esquerda
* 2 = X / analogico R para baixo
* 4 = O / analogico R para direita
* 8 = /\ / analogico R para cima
* 16 = R1
* 32 = L1
* 64 = R2
* 128 = L2
*/
Serial.print("Data 1 ==> ");
Serial.println(data1);
Serial.print("Data 2 ==> ");
Serial.println(data2);
Serial.print("Data 3 ==> ");
Serial.println(data3);
Serial.print("Data 4 ==> ");
Serial.println(data4);
Serial.print("Data 5 ==> ");
Serial.println(data5);
Serial.print("Data 6 ==> ");
Serial.println(data6);
Serial.print("Data 7 ==> ");
Serial.println(data7);
Mirf.flushRx();
lastCommandTime = millis();
}
if((millis() - lastCommandTime) > 100)
{
Mirf.flushRx();
lastCommandTime = millis();
}
}
rov arduino
#include <Psx.h>
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#define dataPin 4
#define cmndPin 3
#define attPin 2
#define clockPin 5
Psx Psx;
byte envio[8];
byte tx_endereco[5] = {0xD0,0xD0,0xD0,0xD0,0xD0};
void setup()
{
Serial.begin(57600);
Psx.setupPins(dataPin, cmndPin, attPin, clockPin, 10);
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.payload = 8;
Mirf.config();
}
void loop()
{
Psx.read();
if(Psx.psxID != 0x00)
{
envio[0] = Psx.psxID; // DIGITAL (82) , ANALOGICO (CE)
envio[1] = Psx.psxStatus;
envio[2] = Psx.psxThumbL; // SETAS, SELECT, DIGITAL L
envio[3] = Psx.psxThumbR; // (x,[],/\,O), START, DIGITAL R, (L1,L2,R1,R2)
envio[4] = Psx.psxJoyRX; // EIXO ANALOGICO "X" DO "R"
envio[5] = Psx.psxJoyRY; // EIXO ANALOGICO "Y" DO "R"
envio[6] = Psx.psxJoyLX; // EIXO ANALOGICO "X" DO "L"
envio[7] = Psx.psxJoyLY; // EIXO ANALOGICO "Y" DO "L"
Serial.print("Data 0 ==> ");
Serial.println(envio[2]);
Serial.print("Data 1 ==> ");
Serial.println(envio[3]);
Mirf.setTADDR(tx_endereco);
Mirf.send(envio);
while(Mirf.isSending());
}
delay(50);
}
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
byte entrada[8];
byte rx_endereco[5] = {0xD0,0xD0,0xD0,0xD0,0xD0};
uint32_t lastCommandTime = 0;
uint8_t data1, data2,data3, data4,data5, data6,data7;
void setup()
{
Serial.begin(57600);
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR(rx_endereco);
Mirf.payload = 8;
Mirf.config();
}
void loop()
{
if(!Mirf.isSending() && Mirf.dataReady())
{
Mirf.getData(entrada);
data1 = ~entrada[1];
data2 = ~entrada[2];
data3 = ~entrada[3];
data4 = ~entrada[4];
data5 = ~entrada[5];
data6 = ~entrada[6];
data7 = ~entrada[7];
/* data 1
* 1 = Seta para esquerda / analogico L para esquerda
* 2 = Seta para baixo / analogico L para baixo
* 4 = seta para direita / analogico L para direita
* 8 = seta para cima / analogico L para cima
*/
/* data 2
* 1 = [] / analogico R para esquerda
* 2 = X / analogico R para baixo
* 4 = O / analogico R para direita
* 8 = /\ / analogico R para cima
* 16 = R1
* 32 = L1
* 64 = R2
* 128 = L2
*/
Serial.print("Data 1 ==> ");
Serial.println(data1);
Serial.print("Data 2 ==> ");
Serial.println(data2);
Serial.print("Data 3 ==> ");
Serial.println(data3);
Serial.print("Data 4 ==> ");
Serial.println(data4);
Serial.print("Data 5 ==> ");
Serial.println(data5);
Serial.print("Data 6 ==> ");
Serial.println(data6);
Serial.print("Data 7 ==> ");
Serial.println(data7);
Mirf.flushRx();
lastCommandTime = millis();
}
if((millis() - lastCommandTime) > 100)
{
Mirf.flushRx();
lastCommandTime = millis();
}
}
domingo, 23 de dezembro de 2018
segunda-feira, 12 de novembro de 2018
domingo, 4 de novembro de 2018
código arduino ,,esquema
/*
Front Motor (Steering) => Channel A
Back Motor => Channel B
Since the motor shield hijacks 6 pins for the motors'
control, they are declared in the MotorShieldR3 library.
*/
#include <MotorShield.h>
#include <AFMotor.h>
AF_DCMotor motorsteer(1);
AF_DCMotor motorengine(2);
#define pinfrontLights 4 //Pin that activates the Front lights.
#define pinbackLights 8 //Pin that activates the Back lights.
char command = 'S';
char prevCommand = 'A';
int velocity = 0;
unsigned long timer0 = 2000; //Stores the time (in millis since execution started)
unsigned long timer1 = 0; //Stores the time when the last command was received from the phone
void setup()
{
Serial.begin(9600); //Set the baud rate to that of your Bluetooth module.
pinMode(pinfrontLights , OUTPUT);
pinMode(pinbackLights , OUTPUT);
motorengine.setSpeed(200);
motorsteer.setSpeed(200);
}
void loop() {
if (Serial.available() > 0) {
timer1 = millis();
prevCommand = command;
command = Serial.read();
//Change pin mode only if new command is different from previous.
if (command != prevCommand) {
//Serial.println(command);
switch (command) {
case 'F':
motorengine.run(FORWARD);
break;
case 'B':
motorengine.run(BACKWARD);
break;
case 'L':
motorsteer.run(FORWARD);
break;
case 'R':
motorsteer.run(BACKWARD);
break;
case 'S':
motorsteer.run(RELEASE);
motorengine.run(RELEASE);
break;
case 'I': //FR
motorengine.run(FORWARD);
motorsteer.run(BACKWARD);
break;
case 'J': //BR
motorengine.run(BACKWARD);
motorsteer.run(BACKWARD);
break;
case 'G': //FL
motorengine.run(FORWARD);
motorsteer.run(FORWARD);
break;
case 'H': //BL
motorengine.run(BACKWARD);
motorsteer.run(FORWARD);
break;
case 'W': //Font ON
digitalWrite(pinfrontLights, HIGH);
break;
case 'w': //Font OFF
digitalWrite(pinfrontLights, LOW);
break;
case 'X':
digitalWrite(pinfrontLights, HIGH);
digitalWrite(pinbackLights, HIGH);
break;
case 'x':
digitalWrite(pinfrontLights, LOW);
digitalWrite(pinbackLights, LOW);
break;
case 'U': //Back ON
digitalWrite(pinbackLights, HIGH);
break;
case 'u': //Back OFF
digitalWrite(pinbackLights, LOW);
break;
case 'D': //Everything OFF
digitalWrite(pinfrontLights, LOW);
digitalWrite(pinbackLights, LOW);
motorengine.run(RELEASE);
motorsteer.run(RELEASE);
break;
default: //Get velocity
if (command == 'q') {
velocity = 255; //Full velocity
motorengine.run(RELEASE);
motorsteer.run(RELEASE);
motorengine.run(RELEASE);
digitalWrite(pinfrontLights, LOW);
digitalWrite(pinbackLights, LOW);
}
else {
//Chars '0' - '9' have an integer equivalence of 48 - 57, accordingly.
if ((command >= 48) && (command <= 57)) {
//Subtracting 48 changes the range from 48-57 to 0-9.
//Multiplying by 25 changes the range from 0-9 to 0-225.
velocity = (command - 48) * 25;
motorsteer.run(RELEASE);
motorengine.run(RELEASE);
}
}
}
}
}
else {
timer0 = millis(); //Get the current time (millis since execution started).
//Check if it has been 500ms since we received last command.
if ((timer0 - timer1) > 500) {
//More tan 500ms have passed since last command received, car is out of range.
//Therefore stop the car and turn lights off.
digitalWrite(pinfrontLights, LOW);
digitalWrite(pinbackLights, LOW);
}
}
}
Front Motor (Steering) => Channel A
Back Motor => Channel B
Since the motor shield hijacks 6 pins for the motors'
control, they are declared in the MotorShieldR3 library.
*/
#include <MotorShield.h>
#include <AFMotor.h>
AF_DCMotor motorsteer(1);
AF_DCMotor motorengine(2);
#define pinfrontLights 4 //Pin that activates the Front lights.
#define pinbackLights 8 //Pin that activates the Back lights.
char command = 'S';
char prevCommand = 'A';
int velocity = 0;
unsigned long timer0 = 2000; //Stores the time (in millis since execution started)
unsigned long timer1 = 0; //Stores the time when the last command was received from the phone
void setup()
{
Serial.begin(9600); //Set the baud rate to that of your Bluetooth module.
pinMode(pinfrontLights , OUTPUT);
pinMode(pinbackLights , OUTPUT);
motorengine.setSpeed(200);
motorsteer.setSpeed(200);
}
void loop() {
if (Serial.available() > 0) {
timer1 = millis();
prevCommand = command;
command = Serial.read();
//Change pin mode only if new command is different from previous.
if (command != prevCommand) {
//Serial.println(command);
switch (command) {
case 'F':
motorengine.run(FORWARD);
break;
case 'B':
motorengine.run(BACKWARD);
break;
case 'L':
motorsteer.run(FORWARD);
break;
case 'R':
motorsteer.run(BACKWARD);
break;
case 'S':
motorsteer.run(RELEASE);
motorengine.run(RELEASE);
break;
case 'I': //FR
motorengine.run(FORWARD);
motorsteer.run(BACKWARD);
break;
case 'J': //BR
motorengine.run(BACKWARD);
motorsteer.run(BACKWARD);
break;
case 'G': //FL
motorengine.run(FORWARD);
motorsteer.run(FORWARD);
break;
case 'H': //BL
motorengine.run(BACKWARD);
motorsteer.run(FORWARD);
break;
case 'W': //Font ON
digitalWrite(pinfrontLights, HIGH);
break;
case 'w': //Font OFF
digitalWrite(pinfrontLights, LOW);
break;
case 'X':
digitalWrite(pinfrontLights, HIGH);
digitalWrite(pinbackLights, HIGH);
break;
case 'x':
digitalWrite(pinfrontLights, LOW);
digitalWrite(pinbackLights, LOW);
break;
case 'U': //Back ON
digitalWrite(pinbackLights, HIGH);
break;
case 'u': //Back OFF
digitalWrite(pinbackLights, LOW);
break;
case 'D': //Everything OFF
digitalWrite(pinfrontLights, LOW);
digitalWrite(pinbackLights, LOW);
motorengine.run(RELEASE);
motorsteer.run(RELEASE);
break;
default: //Get velocity
if (command == 'q') {
velocity = 255; //Full velocity
motorengine.run(RELEASE);
motorsteer.run(RELEASE);
motorengine.run(RELEASE);
digitalWrite(pinfrontLights, LOW);
digitalWrite(pinbackLights, LOW);
}
else {
//Chars '0' - '9' have an integer equivalence of 48 - 57, accordingly.
if ((command >= 48) && (command <= 57)) {
//Subtracting 48 changes the range from 48-57 to 0-9.
//Multiplying by 25 changes the range from 0-9 to 0-225.
velocity = (command - 48) * 25;
motorsteer.run(RELEASE);
motorengine.run(RELEASE);
}
}
}
}
}
else {
timer0 = millis(); //Get the current time (millis since execution started).
//Check if it has been 500ms since we received last command.
if ((timer0 - timer1) > 500) {
//More tan 500ms have passed since last command received, car is out of range.
//Therefore stop the car and turn lights off.
digitalWrite(pinfrontLights, LOW);
digitalWrite(pinbackLights, LOW);
}
}
}
sábado, 3 de novembro de 2018
CODIGO CAR
/*
* Created by Vasilakis Michalis // 12-12-2014 ver.2
* Project: Control RC Car via Bluetooth with Android Smartphone
* More information at www.ardumotive.com
*/
//L293 Connection
const int motorA1 = 5; // Pin 2 of L293
const int motorA2 = 6; // Pin 7 of L293
const int motorB1 = 10; // Pin 10 of L293
const int motorB2 = 9; // Pin 14 of L293
//Leds connected to Arduino UNO Pin 12
const int lights = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
const int buzzer = 3 ;
//Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
const int BTState = 2;
//Calculate Battery Level
const float maxBattery = 8.0;// Change value to your max battery voltage level!
int perVolt; // Percentage variable
float voltage = 0.0; // Read battery voltage
int level;
// Use it to make a delay... without delay() function!
long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec.
long interval = 1000*10; // interval at which to read battery voltage, change it if you want! (10*1000=10sec)
unsigned long currentMillis; //unsigned long currentMillis;
//Useful Variables
int i=0;
int j=0;
int state;
int vSpeed=200; // Default speed, from 0 to 255
void setup() {
// Set pins as outputs:
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
pinMode(lights, OUTPUT);
pinMode(BTState, INPUT);
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
//Stop car when connection lost or bluetooth disconnected
// if(digitalRead(BTState)==LOW) { state='S'; }
//Save income data to variable 'state'
if(Serial.available() > 0){
state = Serial.read();
}
//Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
if (state == '0'){
vSpeed=0;}
else if (state == '1'){
vSpeed=100;}
else if (state == '2'){
vSpeed=180;}
else if (state == '3'){
vSpeed=200;}
else if (state == '4'){
vSpeed=255;}
/***********************Forward****************************/
//If state is equal with letter 'F', car will go forward!
if (state == 'F') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/**********************Forward Left************************/
//If state is equal with letter 'G', car will go forward left
else if (state == 'G') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/**********************Forward Right************************/
//If state is equal with letter 'I', car will go forward right
else if (state == 'I') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/***********************Backward****************************/
//If state is equal with letter 'B', car will go backward
else if (state == 'B') {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/**********************Backward Left************************/
//If state is equal with letter 'H', car will go backward left
else if (state == 'H') {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/**********************Backward Right************************/
//If state is equal with letter 'J', car will go backward right
else if (state == 'J') {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/***************************Left*****************************/
//If state is equal with letter 'L', wheels will turn left
else if (state == 'L') {
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/***************************Right*****************************/
//If state is equal with letter 'R', wheels will turn right
else if (state == 'R') {
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/************************Lights*****************************/
//If state is equal with letter 'W', turn leds on or of off
else if (state == 'W') {
if (i==0){
digitalWrite(lights, HIGH);
i=1;
}
else if (i==1){
digitalWrite(lights, LOW);
i=0;
}
state='n';
}
/**********************Horn sound***************************/
//If state is equal with letter 'V', play (or stop) horn sound
else if (state == 'V'){
if (j==0){
tone(buzzer, 1000);//Speaker on
j=1;
}
else if (j==1){
noTone(buzzer); //Speaker off
j=0;
}
state='n';
}
/************************Stop*****************************/
//If state is equal with letter 'S', stop the car
else if (state == 'S'){
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/***********************Battery*****************************/
//Read battery voltage every 10sec.
currentMillis = millis();
if(currentMillis - (previousMillis) > (interval)) {
previousMillis = currentMillis;
//Read voltage from analog pin A0 and make calibration:
voltage = (analogRead(A0)*5.015 / 1024.0)*11.132;
//Calculate percentage...
perVolt = (voltage*100)/ maxBattery;
if (perVolt<=75) { level=0; }
else if (perVolt>75 && perVolt<=80) { level=1; } // Battery level
else if (perVolt>80 && perVolt<=85) { level=2; } //Min ------------------------ Max
else if (perVolt>85 && perVolt<=90) { level=3; } // | 0 | 1 | 2 | 3 | 4 | 5 | >
else if (perVolt>90 && perVolt<=95) { level=4; } // ------------------------
else if (perVolt>95) { level=5; }
Serial.println(level);
}
}
* Created by Vasilakis Michalis // 12-12-2014 ver.2
* Project: Control RC Car via Bluetooth with Android Smartphone
* More information at www.ardumotive.com
*/
//L293 Connection
const int motorA1 = 5; // Pin 2 of L293
const int motorA2 = 6; // Pin 7 of L293
const int motorB1 = 10; // Pin 10 of L293
const int motorB2 = 9; // Pin 14 of L293
//Leds connected to Arduino UNO Pin 12
const int lights = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
const int buzzer = 3 ;
//Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
const int BTState = 2;
//Calculate Battery Level
const float maxBattery = 8.0;// Change value to your max battery voltage level!
int perVolt; // Percentage variable
float voltage = 0.0; // Read battery voltage
int level;
// Use it to make a delay... without delay() function!
long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec.
long interval = 1000*10; // interval at which to read battery voltage, change it if you want! (10*1000=10sec)
unsigned long currentMillis; //unsigned long currentMillis;
//Useful Variables
int i=0;
int j=0;
int state;
int vSpeed=200; // Default speed, from 0 to 255
void setup() {
// Set pins as outputs:
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
pinMode(lights, OUTPUT);
pinMode(BTState, INPUT);
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
//Stop car when connection lost or bluetooth disconnected
// if(digitalRead(BTState)==LOW) { state='S'; }
//Save income data to variable 'state'
if(Serial.available() > 0){
state = Serial.read();
}
//Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
if (state == '0'){
vSpeed=0;}
else if (state == '1'){
vSpeed=100;}
else if (state == '2'){
vSpeed=180;}
else if (state == '3'){
vSpeed=200;}
else if (state == '4'){
vSpeed=255;}
/***********************Forward****************************/
//If state is equal with letter 'F', car will go forward!
if (state == 'F') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/**********************Forward Left************************/
//If state is equal with letter 'G', car will go forward left
else if (state == 'G') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/**********************Forward Right************************/
//If state is equal with letter 'I', car will go forward right
else if (state == 'I') {
analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/***********************Backward****************************/
//If state is equal with letter 'B', car will go backward
else if (state == 'B') {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/**********************Backward Left************************/
//If state is equal with letter 'H', car will go backward left
else if (state == 'H') {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/**********************Backward Right************************/
//If state is equal with letter 'J', car will go backward right
else if (state == 'J') {
analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/***************************Left*****************************/
//If state is equal with letter 'L', wheels will turn left
else if (state == 'L') {
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 200); analogWrite(motorB2, 0);
}
/***************************Right*****************************/
//If state is equal with letter 'R', wheels will turn right
else if (state == 'R') {
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 200);
}
/************************Lights*****************************/
//If state is equal with letter 'W', turn leds on or of off
else if (state == 'W') {
if (i==0){
digitalWrite(lights, HIGH);
i=1;
}
else if (i==1){
digitalWrite(lights, LOW);
i=0;
}
state='n';
}
/**********************Horn sound***************************/
//If state is equal with letter 'V', play (or stop) horn sound
else if (state == 'V'){
if (j==0){
tone(buzzer, 1000);//Speaker on
j=1;
}
else if (j==1){
noTone(buzzer); //Speaker off
j=0;
}
state='n';
}
/************************Stop*****************************/
//If state is equal with letter 'S', stop the car
else if (state == 'S'){
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
/***********************Battery*****************************/
//Read battery voltage every 10sec.
currentMillis = millis();
if(currentMillis - (previousMillis) > (interval)) {
previousMillis = currentMillis;
//Read voltage from analog pin A0 and make calibration:
voltage = (analogRead(A0)*5.015 / 1024.0)*11.132;
//Calculate percentage...
perVolt = (voltage*100)/ maxBattery;
if (perVolt<=75) { level=0; }
else if (perVolt>75 && perVolt<=80) { level=1; } // Battery level
else if (perVolt>80 && perVolt<=85) { level=2; } //Min ------------------------ Max
else if (perVolt>85 && perVolt<=90) { level=3; } // | 0 | 1 | 2 | 3 | 4 | 5 | >
else if (perVolt>90 && perVolt<=95) { level=4; } // ------------------------
else if (perVolt>95) { level=5; }
Serial.println(level);
}
}
Assinar:
Postagens (Atom)