Go Down

Topic: [SOLVED] Arduino suddenly stops (Read 421 times) previous topic - next topic

esercankutay

May 26, 2019, 04:46 pm Last Edit: May 28, 2019, 07:49 am by esercankutay Reason: SOLVED
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?

Video

Arduino Code:
Code: [Select]

#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:

Code: [Select]

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.

jremington

#1
May 26, 2019, 05:31 pm Last Edit: May 26, 2019, 05:32 pm by jremington
For informed help, please read and follow the directions in the "How to use this forum" post.

Few people, if any on this forum, will download anything from a mysterious URL.

esercankutay

#2
May 26, 2019, 05:41 pm Last Edit: May 26, 2019, 05:42 pm by esercankutay
For informed help, please read and follow the directions in the "How to use this forum" post.

Few people, if any on this forum, will download anything from a mysterious URL.
You dont have to download anything. it just opens the code but i edited post and added the codes anyway. I love hastebin cuz it has code coloring feature. you are the first one to think suspicous about this excellent website i guess.

jremington

Quote
i guess
Very bad guess.

Robin2

If i make short presses  car behaves OK but if i long press car stucks to direction and stops after a while.
I'm not familiar with C#. Is your program sending a stream of characters when your button is pressed or is it (more correctly) sending one message when the button goes down and another when it goes up?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

esercankutay

#5
May 26, 2019, 11:07 pm Last Edit: May 26, 2019, 11:07 pm by esercankutay
I'm not familiar with C#. Is your program sending a stream of characters when your button is pressed or is it (more correctly) sending one message when the button goes down and another when it goes up?

...R
correct. (not necessarily) sends 'z' when any button is released. i did that to make sure motor goes freefall and not stuck when button released.apperantly only initial works.

Robin2

correct. (not necessarily) sends 'z' when any button is released.
Sorry, but that is ambiguous. Which part of my question are you referring to?

And what does "not necessarily" mean in this case?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

esercankutay

Sorry, but that is ambiguous. Which part of my question are you referring to?

And what does "not necessarily" mean in this case?

...R
car runs fine without "sp.write("z")".
Sorry, but that is ambiguous. Which part of my question are you referring to?

And what does "not necessarily" mean in this case?

...R
program sends one character at a time. one for press down one for press up. when you release any button 'z' character is sent. "not necessarily" means car runs fine without sp.write("z") part.

Robin2

program sends one character at a time. one for press down one for press up. when you release any button 'z' character is sent.
I imagine it would be better to send a different character to mark which key has been released. For example send 'W' (upper case) when the W key is pressed and send 'w' (lower case) when it is released. That way you could interleave the pressing and releasing of keys.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up