Sending data from android to arduino MIT APP2 HC06

I have a difficulty when I try to continuously run my stepper motor using application created in MIT APP2,this is the code on Arduino Uno:

#include <SoftwareSerial.h>

int Incoming_value = 8;//vrijednost primljena s aplikacije putem bluetooth modula
//String Outgoing_sensorValues = "start0/0/0/0end";
const int stepPin = 6;  //PUL -Pulse Motor 1
const int dirPin = 3; //DIR -Direction Motor 1
const int enPin = 5;  //ENA -Enable Motor 1
const int stepPin2 = 11;  //PUL -Pulse Motor 2
const int dirPin2 = 10; //DIR -Direction Motor 2
const int enPin2 = 9;  //ENA -Enable Motor 2

SoftwareSerial hc06(2,4);

void setup() {
  pinMode(13,OUTPUT);
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
  pinMode(stepPin2,OUTPUT); 
  pinMode(dirPin2,OUTPUT);
  pinMode(enPin2,OUTPUT);
  digitalWrite(enPin2,LOW);
 Serial.begin(9600);
 hc06.begin(9600);
 delay(200);
}

void loop() {
  if(hc06.available()>0)
  {
    Incoming_value = hc06.read();
    Serial.println(Incoming_value);
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
int sensorValue2 = analogRead(A2);
int sensorValue3 = analogRead(A3);

//Outgoing_sensorValues = "start0/0/0/0end";
//Outgoing_sensorValues = String("start" + String(sensorValue1) + '/' + String(sensorValue2) + '/' + String(sensorValue3) + '/' + String(sensorValue) + "end");
//delay(100);


 // Serial.println(Outgoing_sensorValues);


  
  if(Incoming_value == 1){//strelica gore
    Serial.println("Forward");
digitalWrite(dirPin,HIGH);
 for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000);
  }
  //Makes 200 Pulses for making one full cycle rotation
}

   else if(Incoming_value == 2){//strelica dolje
    digitalWrite(dirPin,LOW);
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 100; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000);
  }}
   else if(Incoming_value == 3){//strelica livo
    digitalWrite(dirPin2,HIGH);
    digitalWrite(dirPin,HIGH);
  //Makes 200 Pulses for making one full cycle rotation
  for(int x = 0; x < 50; x++){
    digitalWrite(stepPin2,HIGH); 
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin2,LOW); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(2000); 
 }
 }
   else if(Incoming_value == 4){//strelica desno
   digitalWrite(dirPin2,LOW);
   digitalWrite(dirPin,LOW);
  //Makes 200 Pulses for making one full cycle rotation
  for(int x = 0; x < 50; x++){
    digitalWrite(stepPin2,HIGH); 
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin2,LOW); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(2000); 
  }
  }
   else if(Incoming_value == 8){//promjena moda rada
  }
else {
  }
}
}
  void auto_mod(){
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
int sensorValue2 = analogRead(A2);
int sensorValue3 = analogRead(A3);

int Y = sensorValue1 - sensorValue2;
int X = sensorValue3 - sensorValue;
//Outgoing_sensorValues = "start0/0/0/0end";
//Outgoing_sensorValues = String("start" + String(sensorValue1) + '/' + String(sensorValue2) + '/' + String(sensorValue3) + '/' + String(sensorValue) + "end");
delay(10);

if((X<-20)&&(Y<-20))                    //oba manja
  {

digitalWrite(dirPin,LOW);
digitalWrite(dirPin2,LOW);
  for(int x = 0; x < 100; x++){
    digitalWrite(stepPin,HIGH); 
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin,LOW); 
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(2000); 
  }
  }//?
  
  else if((X>20)&&(Y>20))                 //oba veća
  {
digitalWrite(dirPin,HIGH);
digitalWrite(dirPin2,HIGH);
  for(int x = 0; x < 100; x++){
    digitalWrite(stepPin,HIGH); 
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin,LOW); 
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(2000); 
  }
  }

  else if((X<-20)&&(Y>20))                //X manji Y veći
  {
digitalWrite(dirPin,LOW);
digitalWrite(dirPin2,HIGH);
  for(int x = 0; x < 100; x++){
    digitalWrite(stepPin,HIGH); 
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin,LOW); 
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(2000); 
  }
  }
  else if((X>20)&&(Y<-20))                //X veći Y manji
  {
digitalWrite(dirPin,HIGH);
digitalWrite(dirPin2,LOW);
  for(int x = 0; x < 100; x++){
    digitalWrite(stepPin,HIGH); 
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin,LOW); 
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(2000); 
  }
  }
  
  else if((X<-20)&&((Y<20)&&(Y>-20)))     //X manji Y dobar
  {
    digitalWrite(dirPin,LOW);
    //digitalWrite(dirPin2,LOW);
  for(int x = 0; x < 100; x++){
    digitalWrite(stepPin,HIGH); 
    //digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin,LOW); 
    //digitalWrite(stepPin2,LOW);
    delayMicroseconds(2000); 
  }
  }
  
  else if((X>20)&&((Y<20)&&(Y>-20)))      //X veći Y dobar
  {
    digitalWrite(dirPin,HIGH);
    //digitalWrite(dirPin2,HIGH);
  for(int x = 0; x < 100; x++){
    digitalWrite(stepPin,HIGH); 
    //digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(2000); 
    digitalWrite(stepPin,LOW); 
    //digitalWrite(stepPin2,LOW);
    delayMicroseconds(2000); 
  }
  }
  else if(((X<20)&&(X>-20))&&(Y<-20))     //X dobar Y manji
  {
    //digitalWrite(dirPin,LOW);
    digitalWrite(dirPin2,LOW);
  for(int x = 0; x < 100; x++){
    //digitalWrite(stepPin,HIGH); 
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(2000); 
    //digitalWrite(stepPin,LOW); 
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(2000); 
  }
  }
  else if(((X<20)&&(X>-20))&&(Y>20))      //X dobar Y veći
  {
    //digitalWrite(dirPin,LOW);
    digitalWrite(dirPin2,HIGH);
  for(int x = 0; x < 100; x++){
    //digitalWrite(stepPin,HIGH); 
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(2000); 
    //digitalWrite(stepPin,LOW); 
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(2000); 
  }
  }
    else{ 
}
 // Serial.println(Outgoing_sensorValues);
  delay(100);
}

This is what i get in serial monitor:


Motor only moves one cycle,I want it to move continuously,as long as i hold the button in an application,this is the app block diagram:

Most of your code is within this conditional statement block and if you don't send a new number, the incoming value actions wont repeat.

if(hc06.available()>0)
  {

To repeat until something new is sent, change the code to read the incoming message, and then set up your conditional tree (switch...case might be cleaner) dependent on what you have read.

Separate the reading from taking action based on the last reading.

1 Like

Try something like this

void loop()
{
  static uint8_t move = 0;
  
  if( hc06.available() > 0 )
  {
    Incoming_value = hc06.read();
    
    if ( Incoming_value == 8 || ( Incoming_value >= 1 && Incoming_value <= 4 ) )
    {
      move = Incoming_value;
      
      switch (move)
      {
        case 1 :
        {
          Serial.println("Forward");
          break;
        }
        
        case 8 :
        {
          Serial.println("Stop");
          break;
        }
        
        default :
        {
          Serial.println("What?");
          break;
        }
      }
    }
  }
  
  switch (move)
  {
    case 1 :
    {
      // code to move forward
      break;
    }
    
    case 8 :
    {
      // code to stop
      break;
    }
    
    default :
    {
      break;
    }
  }
}
1 Like

Thank you a lot man,you are the best,I have one more little problem,I need to have two modes,the program starts in manual mode,where I can control the motors,this is diagram block for that in MIT APP


So in manual mode I should be able to move motors with buttons,and in auto mode the motors should move depending on LDR values,do i need to change the limit for incoming value from 1 to 6,or something else maybe too?

You just need to add a variable and some conditions..

static bool automatic = false;

...

if ( Incoming_value == 5 )
{
  automatic = true;
}
else if ( Incoming_value == 6 )
{
  automatic = false;
}

...

if ( automatic )
{
  auto_mod();
}
else
{
  switch (move)
  {
    case 1 :
    {
      // code to move forward
      ...
}
  

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.