arduino bluetooth rc car using case switch statment - arduino

I build a bluetooth controled car that i have some problem with the code to it. Im using a switch case Statements to control it from my phone. The code works fine but i got a problem with the default: i want it to stop the car if it doesn't receive anything via the bluetooth. My code doesent seem to execute the default: at all. and i dont know whats the problem.
well here is my code.
int IN1 = 7;
int IN2 = 5;
int IN3 = 4;
int IN4 = 2;
int inByte = 0;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop() {
//read my bluetooth
if (Serial.available() > 0) {
inByte = Serial.read();
Serial.print("I received: ");
Serial.println(inByte);
switch (inByte) {
case 'i':
Serial.println("forward");
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 'j':
Serial.println("left");
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 'l':
Serial.println("right");
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 'k':
Serial.println("reverse");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
Serial.println("stop");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
}
}
}

Related

Issues regarding IR sensor controlled car

I'm here regarding the issue I'm having building my project which is IR sensor controlled car with a LCD screen. I have built only 2 projects before this one that I have coded and designed their circuits, hence, I'm fairly new to all this. I expected this car to go forward, turn left & right, and to stop. I first used the code to identify the key code of my IR remote for the buttons I want to use to control my project, then I wrote the code given below. But, whenever I use the buttons I have programmed, the project respond only once to the IR remote and then freezes and continues on. Suppose, If I click the button which I have programmed it to go forward, it starts going forward, but then it stops responding to other buttons. I've tried using higher voltage and current batter but that doesn't seem to help.
Here is the code:
// Setting up LCD Display Here.
#include<LiquidCrystal.h>
int RS = 13;
int E = 12;
int D4 = 11;
int D5 = 10;
int D6 = 6;
int D7 = 2;
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);
// Setting up IR reciver sensor here.
#include<IRremote.h>
int IR_Reciver_Pin = A5;
IRrecv irrecv(IR_Reciver_Pin);
decode_results results;
// Setting up DC motor pins.
/* Motor A connections */
int enA = 9;
int in1 = 8;
int in2 = 7;
/* Motor B connections */
int enB = 3;
int in3 = 5;
int in4 = 4;
void setup() {
// Initiating LCD display here.
lcd.begin(16,2);
lcd.print("Welcome!");
// Initiating IR reciver sensor here.
irrecv.enableIRIn();
// Initiating Serial Monitor.
Serial.begin(9600);
// Initiating DC motors.
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Turning motors off - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop() {
// Reading IR remote value.
if(irrecv.decode(&results))
{
int value = results.value;
Serial.print(F("CODE: "));
Serial.println(results.value);
irrecv.resume();
}
// Code for providing 5V to L293D H-Brigde.
analogWrite(enA, 255);
analogWrite(enB, 255);
// Code for going forward.
if(results.value==3772778233)
{
lcd.clear();
lcd.print(F("Rolling forward"));
lcd.setCursor(0,2);
lcd.print(F("captain!"));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for turning right.
if(results.value==3772794553)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("right."));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// Code for turning left.
if(results.value==3772819033)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("left."));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for stoping.
if(results.value==3772782313)
{
lcd.clear();
lcd.print(F("Halting captain!"));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
}
This is the circuit diagram.
At first glance, I would say that the problem is that your if statments in loop should be nested inside : if(irrecv.decode(&results)){}
So, your loop should look something like this:
void loop() {
analogWrite(enA, 255);
analogWrite(enB, 255);
while(!(irrecv.decode(&results))); //It waits until no button is pressed
if(irrecv.decode(&results))
{
int value = results.value;
Serial.print(F("CODE: "));
Serial.println(results.value);
// Code for going forward.
if(results.value==3772778233)
{
lcd.clear();
lcd.print(F("Rolling forward"));
lcd.setCursor(0,2);
lcd.print(F("captain!"));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for turning right.
if(results.value==3772794553)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("right."));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// Code for turning left.
if(results.value==3772819033)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("left."));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for stoping.
if(results.value==3772782313)
{
lcd.clear();
lcd.print(F("Halting captain!"));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
irrecv.resume();
}
}

Ir code not changing commands when I click the next button on the remote

I am building an IR controlled helicopter with Arduino and the code works, but when I click the next button for the helicopter to move somewhere else, it keeps printing the same thing in the serial monitor and the command does not change... Can someone please help me?
Here is my code:
#include <IRremote.h>
#define in1 6
#define in2 7
#define in3 4
#define in4 5
int IR_RECEIVE_PIN = 2;
IRrecv IrReceiver(IR_RECEIVE_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
IrReceiver.enableIRIn();
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop(){
if(IrReceiver.decode(&results))
switch (results.value) {
case 0xFFA857:
Serial.println("case: UP");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
break;
case 0xFFE01F:
Serial.println("case: DOWN");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
break;
case 0xFF18E7:
Serial.println("case: FORWARD");
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
break;
case 0xFF10EF:
Serial.println("case: LEFT");
break;
case 0xFF5AA5:
Serial.println("case: RIGHT");
break;
case 0xFF48B5:
Serial.println("case: BACKWARDS");
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
break;
}
Serial.println(results.value, HEX);
IrReceiver.resume();
}
Try to run the code with curly brackets on the if:
#include <IRremote.h>
#define in1 6
#define in2 7
#define in3 4
#define in4 5
int IR_RECEIVE_PIN = 2;
IRrecv IrReceiver(IR_RECEIVE_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
IrReceiver.enableIRIn();
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
if (IrReceiver.decode(&results)) {
switch (results.value) {
case 0xFFA857:
Serial.println("case: UP");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
break;
case 0xFFE01F:
Serial.println("case: DOWN");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
break;
case 0xFF18E7:
Serial.println("case: FORWARD");
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
break;
case 0xFF10EF:
Serial.println("case: LEFT");
break;
case 0xFF5AA5:
Serial.println("case: RIGHT");
break;
case 0xFF48B5:
Serial.println("case: BACKWARDS");
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
break;
}
Serial.println(results.value, HEX);
IrReceiver.resume();
}
}

Aurdino coding to control a motor

//Motor A
const int motorpin1 = 6; // Pin 6 of L293
const int motorpin2 = 9; // Pin 3 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
else{
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
}
If we input a in the serial monitor the motor should rotate in one direction and if we input s the motor should rotate in another direction but it is not happening the motor is idle but i get the output like this:
a
one way
s
other way
There is no problem with hardware connections.
Could you please help me with this.Thanks in advance
According to the comments in your code, you are conecting arduino-pin-6 to the l293-pin-6, and arduino-pin-9 to the l293-pin-3.
According to this datasheet, the control pins in L293 are: 2, 7, 10, 15. So, I believe you are connecting it wrong. Also the pulses are being done in the wrong order (from HIGH to LOW and so on).
This should be the correct code (please look at the comments in the code):
//Motor A
const int motorpin1 = 6; // Pin 7 of L293
const int motorpin2 = 9; // Pin 2 of L293
const int motorenablepin = 10; // Pin 1 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
pinMode(motorenablepin, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorenablepin, HIGH); // we can let it enabled
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
Please note that I added the motorenablepin that was missing. It must be conected to the l293-pin-1.
Also, since the LOW and LOW states are common to the code, you can simplify it as I did.
There was small mistake in the logic:
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(10000);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);//changed to low
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(2000);
digitalWrite(motorpin1, LOW);//changed to low
digitalWrite(motorpin2, LOW);

Why did my arduino program suddenly stop working?

So I have a project to make for school in which we had to make an arduino project from scratch. I'm relatively new to arduino and his coding and made a simple, yet fun project. I had the idea of making a sort of safe where you have to enter a key and after that, use a potentiometer to make the right combination to open the safe. I don't know what I did wrong but when I uploaded it to my arduino, the program got to the first lines and stopped after printing the first line to the serial monitor. There the program is stuck and doesn't do anything. The weird thing is that it worked 5 minutes ago but now it just wont. Can anyone help me please? My deadline is in a few hours and I can't fix it... Here is the code and a schematic:
Picture of the setup. The 2 resistors on the left are 1k, the other 2 are 10k.
//variabelen
const int pot = A0;
const int pinA = 6;
const int pinB = 5;
const int pinC = 2;
const int pinD = 3;
const int pinE = 4;
const int pinF = 7;
const int pinG = 8;
const int pinP = 9;
const int enter = 11;
const int reset = 12;
byte enterState = 0;
byte lastEnterState = 0;
byte resetState = 0;
byte lastResetState = 0;
int enterTimer = 0;
String wachtwoord;
String ingegeven = "";
//setup
void setup(){
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinP, OUTPUT);
pinMode(enter, INPUT);
pinMode(reset, INPUT);
Serial.begin(9600);
Serial.println("Geef een wachtwoord op voor de safe.");
wachtwoord = Serial.read();
}
//eigen methoden
void dispChar(int getal){
switch(getal){
case 0:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
break;
case 1:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case 2:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
break;
case 3:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
break;
case 4:
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 5:
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 6:
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 7:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case 8:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case 9:
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
}
}
//Kijk of enter knop is ingedrukt
void onEnter(){
enterState = digitalRead(enter);
if(enterState == HIGH){
enterTimer++;
if(enterState != lastEnterState){
ingegeven += map(analogRead(pot), 0, 1023, 0, 9);
Serial.println(ingegeven);
enterTimer = 0;
}
}
delay(50); //Voorkomt bounce.
if(enterTimer > 50){
Serial.println("\nRESET");
enterTimer = 0;
ingegeven = "";
}
lastEnterState = enterState;
check();
}
bool check(){
if(ingegeven.equals(wachtwoord)){
return true;
} else{
if(ingegeven.length() > wachtwoord.length()){
Serial.println("FOUT!");
ingegeven = "";
return false;
}
}
}
//Kijk of reset knop is ingedrukt
void onReset(){
resetState = digitalRead(reset);
if(resetState != lastResetState){
if(resetState == HIGH){
ingegeven = "";
Serial.println("Geef een wachtwoord op voor de safe.");
wachtwoord = Serial.read();
}
}
delay(50); //Voorkomt bounce.
lastResetState = resetState;
}
//loop
void loop(){
while(!check()){
int getal = map(analogRead(pot), 0, 1023, 0, 9);
digitalWrite(pinP, HIGH);
dispChar(getal);
onEnter();
onReset();
}
Serial.println("CODE CORRECT!")
}
Thanks for the help!!

Arduino 5V Stepper Motor Moving Extremely Slowly

I have a Stepper Motor 5V 4-Phase 5-Wire and a driver board for it.
Unfortunately, I do not have the schematics and cannot find them anywhere on the internet for the driver board. Anyways, I connected everything as it was supposed to be and punched in the code (see below), and it works fine but it spins at about 20 RPM, which is horribly slow.
I cannot decrease the time for delayMicroseconds() anymore because if I do, the motor stops spinning, and starts to just vibrate loudly. Please help, I am stuck:
(Port 8 is connected to IN1 on the driver, Port 9 is in IN2, port 10 is
in IN3, and port 11 is in IN4)
int Pin0 = 8;
int Pin1 = 9;
int Pin2 = 10;
int Pin3 = 11;
int _step = 0;
boolean dir = true;// gre
void setup()
{
pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
}
void loop()
{
switch(_step){
case 0:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
case 1:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
break;
case 2:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 3:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 4:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 5:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 6:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 7:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
default:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
}
if(dir){
_step++;
}else{
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){
_step=7;
}
delayMicroseconds(800);
}

Resources