Hello guys, i am trying to build a rc self parking car for my embedded course project. I've managed to send direction (w,a,s,d on pc) over bluetooth to arduino and car seem to work fine but here is the strange part;
If i make short presses car behaves OK but if i long press car stucks to direction and stops after a while. I connected the cables which goes to l298n(in,in2,in3,in4) to 4 leds and resistors on breadboard without any changes to the system but that problem never occurred on that test setup. Any smart ideas about what is causing this?
Arduino Code:
#include <SoftwareSerial.h>
SoftwareSerial BlueTooth(3, 2);
const int ena = 9; //PWM ile motor hızı kontrolü pini
const int in1 = 10; //Ön motor geri yön pini
const int in2 = 11; //Ön motor ileri yön pini
const int in3 = 12; //Arka motor geri yön pini
const int in4 = 13; //Arka motor ileri yön pini
char RData; //Bluetooth üzerinden alınan veri
void setup()
{
BlueTooth.begin(9600);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(8, OUTPUT);
}
void loop()
{
if (BlueTooth.available())
{
RData=(BlueTooth.read());
if (RData=='w')
{
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
analogWrite(ena,150);
tone(8, 1000, 100);
}
if (RData=='s')
{
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
analogWrite(ena,150);
tone(8, 1000, 100);
}
if (RData=='a')
{
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
tone(8, 1000, 100);
}
if (RData=='d')
{
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
tone(8, 1000, 100);
}
if (RData=='z')
{
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);
}
}
}
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace Araba_Park
{
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (String item in SerialPort.GetPortNames())
{
comboBox1.Items.Add(item);
}
if (comboBox1.Items.Count > 0)
{
comboBox1.Text = comboBox1.Items[0].ToString();
}
}
SerialPort sp = new SerialPort();
int mSpeed;
private void Button1_Click(object sender, EventArgs e)
{
if (comboBox1.Items.Count == 0)
{
MessageBox.Show("Bağlanacak cihaz yok!");
}
else
{
sp.BaudRate = 9600;
sp.PortName = comboBox1.Text;
//sp.StopBits = StopBits.One;
sp.Open();
label2.Text = "Bağlandı";
comboBox1.Enabled = false;
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
}
if (sp.BytesToRead>0)
{
label4.Text = sp.ReadExisting();
}
}
private void TrackBar1_ValueChanged(object sender, EventArgs e)
{
mSpeed = Convert.ToInt16(trackBar1.Value);
}
private void Button2_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
sp.Close();
label2.Text = "Bağlantı kesildi";
comboBox1.Enabled = true;
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
}
}
public void key_down(object sender, KeyEventArgs e)
{
if (sp.IsOpen)
{
if (e.KeyCode == Keys.W)
{
button3.BackColor = Color.Red;
sp.Write("w");
}
if (e.KeyCode == Keys.A)
{
button4.BackColor = Color.Red;
sp.Write("a");
}
if (e.KeyCode == Keys.D)
{
button6.BackColor = Color.Red;
sp.Write("d");
}
if (e.KeyCode == Keys.S)
{
button5.BackColor = Color.Red;
sp.Write("s");
}
}
}
public void key_up(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
button3.BackColor = SystemColors.Control;
sp.Write("z");
}
if (e.KeyCode == Keys.A)
{
button4.BackColor = SystemColors.Control;
sp.Write("z");
}
if (e.KeyCode == Keys.D)
{
button6.BackColor = SystemColors.Control;
sp.Write("z");
}
if (e.KeyCode == Keys.S)
{
button5.BackColor = SystemColors.Control;
sp.Write("z");
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
sp.Close();
}
}
}
EDIT: I finally found the problem. Toy motor seems to be a just standart motor but draws more current and runs under higher voltage. Now i connected standart 3v motor and it runs perfectly.