Serial.read problem. Programmers pls have a look it.

Hi folks. I have question about serial.read in IDE.
Im trying to build a demo maschine but i have problem. I have dc motors and i want to controll them with the serial monitor. Sequently i want these;
1.i want to select motor
2.want to select motor turning direction
3. How many steps will it turn.(i made encoder disks for each motors and it works fine,im clearly sure that)
The problem is ,i want them controll like “M1 CW 24”. That means ;first motor, clockwise direction and 24 steps. İ tried all codes but they didnt work. İs that any option or advice to me ?
So i decided to ask you guys. Waiting your answers as soon as possible.
Thanks in advance

make a good power plan, Arduino cannot drive motors directly

How much experience do you have in programming?

İ tried all codes but they didnt work

That is clearly nonsense.
Post some of the code you tried.

(deleted)

hello guys and thanks for quick response…

i will share my code but i changed all part of the codes, imean its little bit dirty.

#define MotY_SENSOR_1 4 //Photo sensor 1
#define MotY_SENSOR_2 5 //Photo sensor 2
#define MotY_DIRECTION 10 //Direction output motor
#define MotY_PWM 11 //PWM output motor
#define STEP_MARGIN 1L //10 - 1000 (2)
#define MIN_DUTYCYCLE 30 //0 - 255 (125)
#define MAX_DUTYCYCLE 70 //0 - 255 (255)

int i=1;
int MotYSensor02 = 0;
int MotYSensor01 = 0;
int MotYDutyCycle = 10; // 10 - 255
long MotYSetPoint = 0;
long MotYIsPoint = 0;
byte MotYStep = 0;
byte MotYStepDone = 0;
int readValueOld = 0;
int LEDvalue = 0;
int timeOld = 0;
//int incomingByte= 0;
//int incomingByte1= 0;
byte byteRead;
long num;

void setup() {
// put your setup code here, to run once:
pinMode(MotY_DIRECTION, OUTPUT);
pinMode(MotY_PWM, OUTPUT);

pinMode(MotY_SENSOR_1, INPUT);
pinMode(MotY_SENSOR_2, INPUT);

MotYStep = 0; MotYStepDone = 1;
MotYSetPoint = 0;
MotYIsPoint = 0;
Serial.begin(57600);
}
unsigned int integerValue=0; // Max value is 65535
unsigned int integerValue1=0;
unsigned int integerValue2=0;
char incomingByte;
char incomingByte1;
char incomingByte2;
void loop() {

//copy paste from balengineer.com//
//baudrate in 57600 olması yetiyor…9600 geç kalıyor,112500 de hızlı.
integerValue = 0;
integerValue1 = 0;
integerValue2 = 0;

//____________________
if (Serial.available()){//if we get a valid byte, read from serial:
//get incoming byte:
start:
delay(3000);
integerValue = Serial.read();

//.__________________________________
if(integerValue == ‘CW’){
Serial.print(“CW Steps:”);
if (Serial.available()>0) { // something came across serial
integerValue1 = 0; // throw away previous integerValue
while(1) { // force into a loop until ‘n’ is received
incomingByte1 = Serial.read();
if (incomingByte1 == ‘\n’) break; // exit the while(1), we’re done receiving
if (incomingByte1 == -1) continue; // if no characters are in the buffer read() returns -1
integerValue1 *= 10; // shift left 1 decimal place
// convert ASCII to integer, add, and shift left 1 decimal place
integerValue1 = ((incomingByte1 - 48) + integerValue1);

}

Serial.println(integerValue1); // Do something with the value

}
MotYSetPoint=(MotYSetPoint+integerValue1);
goto again;
}
// ____________________________________
if(integerValue == ‘CCW’){
Serial.print(“CCW Steps:”);
if (Serial.available()>0) { // something came across serial
integerValue2 = 0; // throw away previous integerValue
while(1) { // force into a loop until ‘n’ is received
incomingByte2 = Serial.read();
if (incomingByte2 == ‘\n’) break; // exit the while(1), we’re done receiving
if (incomingByte2 == -1) continue; // if no characters are in the buffer read() returns -1
integerValue2 *= 10; // shift left 1 decimal place
// convert ASCII to integer, add, and shift left 1 decimal place
integerValue2 = ((incomingByte2 - 48) + integerValue2);

}

Serial.println(integerValue2); // Do something with the value

}
MotYSetPoint=(MotYSetPoint-integerValue2);
goto again;
}
else if ((integerValue !=‘CCW’) ||(integerValue != ‘CW’)) {
goto start;
}
}

// put your main code here, to run repeatedly:
MotYSensor01 = digitalRead(MotY_SENSOR_1);
MotYSensor02 = digitalRead(MotY_SENSOR_2);

if(MotYSensor02 == 0 && MotYSensor01 == 0){
if(MotYStep == 1){
MotYIsPoint–;
}
if(MotYStep == 3){
MotYIsPoint++;
}
MotYStep = 0;
}

if(MotYSensor02 == 1 && MotYSensor01 == 0){
if(MotYStep == 0){
MotYIsPoint++;
}
if(MotYStep == 2){
MotYIsPoint–;
}
MotYStep = 1;
}

if(MotYSensor02 == 1 && MotYSensor01 == 1){
if(MotYStep == 3){
MotYIsPoint–;
}
if(MotYStep == 1){
MotYIsPoint++;
}
MotYStep = 2;
}

if(MotYSensor02 == 0 && MotYSensor01 == 1){
if(MotYStep == 2){
MotYIsPoint++;
}
if(MotYStep == 0){
MotYIsPoint–;
}
MotYStep = 3;
}

again:
//MotYSetPoint=(MotYSetPoint+integerValue);
if(abs(MotYSetPoint - MotYIsPoint) < STEP_MARGIN){
analogWrite(MotY_PWM, 0);
digitalWrite(MotY_DIRECTION, 0);
MotYStepDone = 1;
MotYDutyCycle = MIN_DUTYCYCLE;
}
else{
if(MotYIsPoint < MotYSetPoint){
digitalWrite(MotY_DIRECTION, 1);
analogWrite(MotY_PWM, 255 - MotYDutyCycle);
}
if(MotYIsPoint > MotYSetPoint){
digitalWrite(MotY_DIRECTION, 0);
analogWrite(MotY_PWM, MotYDutyCycle);

}
}

//if(millis() - timeOld > 1000){

// LEDvalue = MotYIsPoint - readValueOld;

//Serial.print(LEDvalue);
// Serial.print(" ");

// Serial.println(MotYIsPoint);
//readValueOld = MotYIsPoint;
}

spycatcher2k:
No you did not! If you had, you would have a working sketch.

Read in the string until you get the end character (could be anything)

search until you find a space, you now have "M1"
do again. you have "CW"
The last part is "24"

Compare the extracted data, or convert it to a number.

thanks i ll try it at soon :slight_smile:

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

To make it easy for people to help you please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

…R

(deleted)

#define MotY_SENSOR_1     4    //Photo sensor 1
#define MotY_SENSOR_2     5    //Photo sensor 2
#define MotY_DIRECTION   10    //Direction output motor
#define MotY_PWM         11    //PWM output motor
#define STEP_MARGIN      1L    //10 - 1000 (2)
#define MIN_DUTYCYCLE    30    //0 - 255 (125)
#define MAX_DUTYCYCLE   70    //0 - 255 (255)




int MotYSensor02 = 0;
int MotYSensor01 = 0;
int MotYDutyCycle = 10; // 10 - 255
long MotYSetPoint = 0;
long MotYIsPoint = 0;
byte MotYStep = 0;
byte MotYStepDone = 0;
int readValueOld = 0;
int LEDvalue = 0;
int timeOld = 0;
int i=0;
int a=1;
int b=0;
int twodigitofbegin=0;
int twodigitofend=0;

void setup() {
  // put your setup code here, to run once:
  pinMode(MotY_DIRECTION, OUTPUT);     
  pinMode(MotY_PWM, OUTPUT);     
 
  
  pinMode(MotY_SENSOR_1, INPUT);
  pinMode(MotY_SENSOR_2, INPUT);

   MotYStep = 0;  MotYStepDone = 1;
     MotYSetPoint = 0;
       MotYIsPoint  = 0;
       Serial.begin(57600);
}
unsigned int integerValue=0;  // Max value is 65535
char incomingByte;
void loop() {
   
   
//copy paste from balengineer.com//
  //baudrate in 57600 olması yetiyor..9600 geç kalıyor,112500 de hızlı.

 if (Serial.available() > 0) {   // something came across serial
    integerValue = 0;         // throw away previous aintegerValue
    while(1) {            // force into a loop until 'n' is received
      incomingByte = Serial.read();
      if (incomingByte == '\n') break;   // exit the while(1), we're done receiving
      if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
      integerValue *= 10;  // shift left 1 decimal place
      // convert ASCII to integer, add, and shift left 1 decimal place
      integerValue = ((incomingByte - 48) + integerValue);
    }
    if(integerValue>0) {
    if(integerValue>10000 && integerValue<65535) {
      i=3;
    }
    if(integerValue>1000 &&integerValue<10000) {
      i=2;
    }
    if(integerValue>100 && integerValue<1000 ) {
      i=1;
    }
   
    for (int b=1; b <= i; b++ )  {
      a=10*a;
    }
    }
    twodigitofbegin=(abs(integerValue/a));
    twodigitofend=(integerValue-(twodigitofbegin*a));
    /*
    Serial.print("Motor number and direction :");
    Serial.println(twodigitofbegin);   // Do something with the value
     Serial.print("Step Count:");
    Serial.println(twodigitofend); 
*/
     a=1;
  }
  
if (twodigitofbegin == 29); {
  Serial.print("Y axis moving through the clockwise direction and moving");
    Serial.print(twodigitofend);
     Serial.println("steps.");
     MotYSetPoint=(MotYSetPoint+twodigitofend);
  
}
 if (twodigitofbegin == 49); {
  Serial.print("Y axis moving through the counter clockwise direction and moving");
    Serial.print(twodigitofend);
     Serial.println("steps.");
  MotYSetPoint=(MotYSetPoint-twodigitofend);

}
  
  // put your main code here, to run repeatedly:

 MotYSensor01 = digitalRead(MotY_SENSOR_1);
      MotYSensor02 = digitalRead(MotY_SENSOR_2);
    
      if(MotYSensor02 == 0 && MotYSensor01 == 0){
        if(MotYStep == 1){
          MotYIsPoint--;
        }
        if(MotYStep == 3){
          MotYIsPoint++;
        }
        MotYStep = 0;
      }
    
      if(MotYSensor02 == 1 && MotYSensor01 == 0){
        if(MotYStep == 0){
          MotYIsPoint++;
        }
        if(MotYStep == 2){
          MotYIsPoint--;
        }
        MotYStep = 1;
      }
    
      if(MotYSensor02 == 1 && MotYSensor01 == 1){
        if(MotYStep == 3){
          MotYIsPoint--;
        }
        if(MotYStep == 1){
          MotYIsPoint++;
        }
        MotYStep = 2;
      }
    
      if(MotYSensor02 == 0 && MotYSensor01 == 1){
        if(MotYStep == 2){
          MotYIsPoint++;
        }
        if(MotYStep == 0){
          MotYIsPoint--;
        }
        MotYStep = 3;
      }
      


 //MotYSetPoint=(MotYSetPoint+integerValue);
      if(abs(MotYSetPoint - MotYIsPoint) < STEP_MARGIN){
        analogWrite(MotY_PWM, 0);
        digitalWrite(MotY_DIRECTION, 0);
        MotYStepDone = 1;
        MotYDutyCycle = MIN_DUTYCYCLE;
      }
      else{
        if(MotYIsPoint < MotYSetPoint){
          digitalWrite(MotY_DIRECTION, 1);
          analogWrite(MotY_PWM, 255 - MotYDutyCycle);
        }
        if(MotYIsPoint > MotYSetPoint){
          digitalWrite(MotY_DIRECTION, 0);
          analogWrite(MotY_PWM, MotYDutyCycle);
      
        }
      }
  
    //if(millis() - timeOld > 1000){
      
  
   // LEDvalue = MotYIsPoint - readValueOld;


    //Serial.print(LEDvalue);
   // Serial.print("    ");

    //  Serial.println(MotYIsPoint);
//readValueOld = MotYIsPoint;
}

i have changed but this time motor’s steps doing something stupid…
waiting for help !!! thanks in advance…

spycatcher2k:
Get rid of the DELAYs & GOTOs Neither are needed in this case.

i did your way but have a another problem..

1C245==29245;
29 is 1.motor clockwise dir.
245 is steps

Qwertyqwq:
but have a another problem..

1C245==29245;
29 is 1.motor clockwise dir.
245 is steps

I don't understand.

Did you study Serial Input Basics ?

...R

Robin2:
I don't understand.

Did you study Serial Input Basics ?

...R

Looking at who OP quoted, I have my doubts :wink:

Robin2:
I don’t understand.

Did you study Serial Input Basics ?

…R

hahaha. I did not notice ,that quote is looks like a code but let me explain.

1C245 is (by my codes) equal 29245…
29 is 1C(1.motor,cw direction) , 245 is number of steps… :slight_smile:

a few hours ago i fixed my problem. it was a mistake in " if " command. now its work fine…