Analog read stopped while PWM out

I’m a new user with Arduino.

I would like to control Stepper motor by PWM out and the same time I need to read ADC (Analog read from Pressure sensor)

But I can not solve this problem.

I connected the arduino board to PC via USB port and get data from serial port.

Are you saying you want to control the speed of the stepper motor using input data from a pressure sensor?

But I can not solve this problem.

You didn't describe a problem, except there's a clue in your thread title. Do you mean that when you output PWM, the analog read is disabled?)

I can't see your code or circuit from here 8) so please post more detail.

ChokeBUU:
I’m a new user with Arduino.

I would like to control Stepper motor by PWM out and the same time I need to read ADC (Analog read from Pressure sensor)

But I can not solve this problem.

I connected the arduino board to PC via USB port and get data from serial port.

/*
//????????????? BUU PSV Pressure Setting
 Smoothing
 
 Reads repeatedly from an analog input, calculating a running average
 and printing it to the computer.  Keeps ten readings in an array and 
 continually averages them.
 
 The circuit:
 * Analog sensor (potentiometer will do) attached to analog input 0
 
 Created 22 April 2007
 By David A. Mellis  <dam@mellis.org>
 
 http://www.arduino.cc/en/Tutorial/Smoothing
 
 This example code is in the public domain.
 
 */
//    'A1 ??? Pressure Sensor 500psi
//   'A0 ??? Differential Pressure (for detect pressure out from Releif Valve when opening

// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
//SSSSSSSSSSSSSSSSSSSSSSSS
#define led1 41        // ????? Relay1                        //led1 Pin Control Relay 1
#define led2 43        // ????? Relay2                         //led2 Pin Control Relay 2
#define led3 45        // ????? Relay3                         //led2 Pin Control Relay 3
#define led4 47        // ????? Relay4                         //led2 Pin Control Relay 4
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
#include <LCD_4BIT.h>   //?????? LCD

#define LCD_BL   22   //?????? LCD
#define LCD_RS   23    //?????? LCD
#define LCD_RW   24   //?????? LCD
#define LCD_EN   25   //?????? LCD 
#define LCD_D4   26   //?????? LCD
#define LCD_D5   27   //?????? LCD
#define LCD_D6   28   //?????? LCD
#define LCD_D7   29   //?????? LCD
LCD_4BIT lcd = LCD_4BIT(LCD_RS, LCD_RW, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);     // Used R/W Pin (7 Pin)   //?????? LCD

//SSSSSSSSSSSSSSSSSSSSSSSSSS

const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input

int index = 0;                  // the index of the current reading

int total = 0; // the running total

int average = 0;                // the average

int inputPin =1;

int DiffPin1 =0;  //????????? ???????
//int inputPin2 =2; //????????? ???????

int MT = 2;

void setup()
{
  //FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  lcd.Initial();                                        // Initial LCD   ?????? LCD 
  // pinMode(LCD_BL, OUTPUT);                              // Backlight Control   ?????? LCD      

  //FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;  

  //SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSs
  pinMode(led1, OUTPUT);                        //led2 = Output
  pinMode(led2, OUTPUT);                        //led1 = Output
  pinMode(led3, OUTPUT); 
  pinMode(led4, OUTPUT); 

  //SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
  // Motor Control
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  digitalWrite(9, LOW);  //?????????????????
  //SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS      
}


void loop() 
{
  //FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  lcd.ClearScreen(); //?????? LCD 
  //int DiffPin1 =0;
  //int inputPin =1;
  lcd.SetCursor(0x00);   //?????? LCD 
  int P1 = analogRead(DiffPin1);   //?????? LCD 
  if (P1 > 108)   //?????? LCD 
  {                     //?????? LCD 
    P1= P1-107;       //?????? LCD
  }                     //?????? LCD
  else                    //?????? LCD
  {                        //?????? LCD
    P1=0;                   //?????? LCD
  }                        //?????? LCD
  if (P1 > 842)       //?????? LCD
  {                   //?????? LCD
    P1=842;           //?????? LCD
  } 
  //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  P1=P1/1.684;          //?????? LCD  1.828
  //HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
  lcd.Print("P1= ") , lcd.Print(P1);    //?????? LCD
  lcd.SetCursor(0x07);        //?????? LCD
  //UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
  int P2 = analogRead(inputPin);   //?????? LCD
  if (P2 > 108)          //?????? LCD
  {                 //?????? LCD
    P2= P2-107;    //?????? LCD
  }                 //?????? LCD
  else              //?????? LCD
  {                //?????? LCD
    P2=0;             //?????? LCD
  }                 //?????? LCD
  if (P2 > 842)       //?????? LCD
  {                   //?????? LCD
    P2=842;           //?????? LCD
  }                   //?????? LCD
  //UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
  P2=P2/1.684;         //?????? LCD
  //UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
  lcd.Print("P2= "),lcd.Print(P2);        //?????? LCD
  // int C = (analogRead(DiffPin1));
  //int D = (analogRead(inputPin));

  //FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 
  // subtract the last reading:
  total= total - readings[index];    

  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];   
  // advance to the next position in the array:  
  index = index + 1;                    
  // if we're at the end of the array...
  if (index >= numReadings)    
    // ...wrap around to the beginning: 
    index = 0;                           
  // calculate the average:
  average = total / numReadings;    
  // send it to the computer (as ASCII digits) 

  int BA = analogRead(DiffPin1);   //  ????????? ???????  ???????????????

  //Serial.println(average, DEC);

  //Serial.print(B),Serial.print("x"),Serial.println(average, DEC),Serial.print("#");  ???????????????
  Serial.print(average,DEC),Serial.print("x"),Serial.println(BA),Serial.print("#");  // Output to Visual Studio
  //Serial.print(C),Serial.print("#"),Serial.print(B),Serial.print("x"),Serial.println(average, DEC),  Serial.print(","); ????????? ???
  //SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

  delay (100);

  int pos;
  if (Serial.available()){

    while(Serial.available()>0){
      pos=Serial.read();     //reads the value sent from Visual Basic  

      //JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
      if(pos=='L')     //?????? LCD
        digitalWrite(22, HIGH);    //?????? LCD
      if(pos=='M')             //?????? LCD
        digitalWrite(22, LOW);    //?????? LCD
      //JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

      if(pos=='H')

      Motor_L();


      if(pos=='I')

      Motor_R();


      if(pos=='A')
        digitalWrite(41, HIGH);       // On Relay 1

      else if(pos=='Z')
        digitalWrite(41, LOW);  //   Off Relay 1

      //SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

      if(pos=='B')
        digitalWrite(43, HIGH);       //On Relay 2

      else if(pos=='C')
        digitalWrite(43, LOW); // Off Relay 2

      if(pos=='D')
        digitalWrite(45, HIGH);       //On Relay 3

      else if(pos=='E')
        digitalWrite(45, LOW);  // Off Relay 3

      if(pos=='F')
        digitalWrite(47, HIGH);       //On Relay 4

      else if(pos=='G')
        digitalWrite(47, LOW);  // Off Relay 4
    }
  } 
}

//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

void Motor_L(void) // Run Motor Left
{
  for (int x = 0; x <200; x++)
  {
    if (x>199)
    {
      break;
    }
     digitalWrite(9, LOW); 
    digitalWrite(8, HIGH);
    delayMicroseconds(2500); 
    digitalWrite(8, LOW);
    delayMicroseconds(2500);
  }
 
}

//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

void Motor_R(void) // Run Motor Right
{
  for (int x = 0; x <200; x++)
  {
    if (x>199)
    {
      break;
    }
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    delayMicroseconds(2500); 
    digitalWrite(8, LOW);
    delayMicroseconds(2500);
  }

}

Moderator edit: Tags cleaned up.

Yikes, I can't read that quote in a quote. It'll be easier if you put the code in code tags: that's the # just above the :sweat_smile: in the editing window.

I still didn't see a description of the problem... or did I miss that in the messy quote in a quote.

for (int x = 0; x <200; x++)
  {
    if (x>199)
    {
      break;
    }

Doesn’t seem likely, does it?

Please see my code in attach.

I use Visual studio receive serial data from Arduino board via USB port. The data get from pressure sensor.

I use the data that get from ADC for make a decision to run the stepper motor.

In the Visual studio shown the pressure with interval as in Arduino code Delay 100().

I would like to monitor the pressure all the time when the program running.

But, when I send message from Visual studio “SerialPort1.Write(“H”)” that mean it call function in Arduino to run stepper motor.

When stepper motor running, The pressure that show in visual studio has stopped during motor running.

When the motor is stop, The pressure that show in visual studio is continue to show.

I think, I can’t use timer in Arduino more than 2 port in the same time. I mean, Delay ()

Port1 is serial.print: I use "Delay () with ADC code to send out to serial port with Delay 100() interval.
Port2 is PWM out: I use “Delay () with code that sent digital out PWM. (Code in Arduino is " delayMicroseconds(2500);”

I found in the tutorial “Blink without Delay” , But I don’t know how to apply that to my code.

Please see my code /circuit diagram and Visual studio screen in attach.

Thank.

New_PG_500psi.pde (8.42 KB)

You are trying to do analog reading from pin 1 ?

That may be a problem. Should you call it A1 ? Pin 1 might conflict with serial communications. Maybe analogRead() always defaults to analog pin 1, I am not sure, you should check.

What exactly happens when you get this problem. Does the analog input ever work sometimes, or never ?

If you have a sketch that is doing several things simultaneously then you have to structure
the code to time-slice effectively.

loop() calls the handler function for each part of the system, and just repeats.

All the handler functions have to progress the relevant actions they are reponsible for,
not taking too much time in one action (for instance not calling delay() ever).

This means using state-machine style coding for each sub-system and the blinkWithoutDelay
style timed event handling.

Re-casting a program in this way takes a bit of thinking through, its less intuitive than
just doing one task at once.

Stepper motors are not driven by PWM. You can't directly drive a stepper motor from the Arduino, either. You need a stepper motor driver between the Arduino and the stepper motor. Then, the analog reading would affect the interval between the steps, not the speed of the steps.

Thanks PaulS,

I already have the stepper motor driver. It usage only 2 signals to control it .The first one for the direction and the another one for Step.

I assigned PWM pin 8 for Step control and PWM pin 9 for direction control.

and PWM pin 9 for direction control.

But you're using it as a simple digital output, right?

I think so.

The code below is for run the motor 1 round left direction.

Make pulse by digital Hi and Lo.

void Motor_L(void) // Run Motor Left
{
  for (int x = 0; x <200; x++)
  {
    if (x>199)
    {
      break;
    }
     digitalWrite(9, LOW);  // For left direction
    digitalWrite(8, HIGH);  // Digital out HIGH
    delayMicroseconds(2500);  // 
    digitalWrite(8, LOW); // Digital out LOW
    delayMicroseconds(2500);
  }
 
}

Those delays will stop anything else from rumming. They seem very long for the time between steps.

The unit of them is micro sec. It's work for my stepper motor for turning 1 round. Not long.

But it is blocking code, you can't do anything else while this is executing. Which is why you think analog read has stopped working, you just can't get to call it.

for (int x = 0; x <200; x++)
  {
     digitalWrite(9, LOW);  // For left direction
    digitalWrite(8, HIGH);  // Digital out HIGH
    delayMicroseconds(2500);  // 
    digitalWrite(8, LOW); // Digital out LOW
    delayMicroseconds(2500);
  }

Not long.

200 x 5ms = one second (at least)

In the same void loop()

Read analog from pressure sensor and send out to serial port via USB to PC every delay(100) as below. analogRead(inputPin); // Read analog interval = delay (100) Serial.print(average,DEC),Serial.print("x"),Serial.println(BA),Serial.print("#"); // Output to Visual Studio

The result from my project while start the motor. The code above has not response. After motor stop. The code above has re-running again.

Thanks.

Is this motor a stepping motor, if so it is your stepping motor code that is blocking. If not a stepping motor then please provide a schematic of your wiring and post the code. That stuff you posted at the start of this thread is not a schematic.

My motor is the stepper motor. Total 8 Wire and common out to 4 Wire.

I have not Schematic of the circuit. Only the diagram as attched PDF.

Thanks.

Analog Read and Control Stepper Motor.pdf (165 KB)