problem with blocking the program while reading the thermocouple

Hello
Please help where as a problem my code problem if I check the thermocouple temperature my stepper motor is not working properly


[pre][color=#202124]code in the annex[/color]

[/pre]

KotolMiro.ino (3.43 KB)

  1. If you don't want the program to block when reading the thermocouple then use millis() to determine when to read the thermocouple.

BlinkWithoutDelay

  1. previousMillis should be an 'unsigned long', not a long.

  2. There are several places where you probably think you are setting the global variables 'previousMillis' and 'currentMillis' but you are actually creating a local variable of the same name and never setting the global variable.

  3. Please format your code using the auto-format feature of the IDE.

Well this is interesting! Something happened to it in the annex, all the comments fell out and white space took there place. Post a simple schematic as well, not a frizzy thing. Show all power, ground and interconnections.

#include <Stepper.h>
#include <max6675.h>
#define iskra 13
#define plyn 12
#define thermostat 11
#define start_cyklu 10
#define ENBp A5
#define releiskra 6
#define releplyn 3
#define M1 9
#define kontrolka 4
int thermoDO = A2;
int thermoCS = A3;
int thermoCLK = A4;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);




const int stepsPerRevolution = 200;  


Stepper myStepper(stepsPerRevolution, 2, 7, 5, 8);

int stepCount = 0;  
long previousMillis = 0;
unsigned long currentMillis = millis();
int thermo = 1;
int teplota = thermocouple.readCelsius();



 
void setup() {

   pinMode(iskra , INPUT_PULLUP);  
    pinMode(plyn , INPUT_PULLUP);
   pinMode(thermostat , INPUT_PULLUP);
   pinMode(start_cyklu , INPUT_PULLUP);
   pinMode(ENBp , OUTPUT);
   pinMode(releiskra , OUTPUT);
   pinMode(releplyn , OUTPUT);
   pinMode(M1 , OUTPUT);
   pinMode(kontrolka , OUTPUT);
   Serial.begin(9600);
  delay(500);

  
  unsigned long currentMillis = millis();

 
 

  
}



void loop() {
  


int sensorReading = analogRead(A0);
int sensorReading2 = analogRead(A0);
int sensorReadingV = analogRead(A1);
int motorSpeed = map(sensorReading, 0, 1023, 100, 200);
int motorSpeed2 = map(sensorReading2, 0, 1023, 50, 100);
int motorSpeedV = (sensorReadingV ) / 4;


 

 Serial.print("C = "); 
Serial.println(thermocouple.readCelsius());
   int teplota = thermocouple.readCelsius();
   delay(500);
  
   
   




 if        (digitalRead(iskra) == LOW) {
      digitalWrite(releiskra, HIGH);
      
      }
  else if (digitalRead(iskra) == HIGH) {
     digitalWrite(releiskra, LOW);
     }

     if        (digitalRead(plyn) == LOW) {
      digitalWrite(releplyn, HIGH);
      
      }
  else if (digitalRead(plyn) == HIGH) {
     digitalWrite(releplyn, LOW);
     }
 




if        (digitalRead(start_cyklu) == LOW ) {
  analogWrite(M1, motorSpeedV);

   
      }



if        (digitalRead(start_cyklu) == HIGH and (thermocouple.readCelsius() < 200.00)) {
      previousMillis = currentMillis;
      digitalWrite(kontrolka, LOW);
      }
  else if (digitalRead(start_cyklu) == LOW and (thermocouple.readCelsius() < 200.00) and currentMillis - previousMillis > 60000){
     digitalWrite(kontrolka, HIGH);
   }
    
    
    
   
 




      
     

    
     



 
  
      

     
   
 

  

 
  

     
 
  
  
  if        (digitalRead(start_cyklu) == LOW and digitalRead(thermostat) == HIGH and  teplota > 200 ) {// velka dávka termostat 
     sensorReading = analogRead(A0);
    
    
      
    
    digitalWrite(ENBp, HIGH);
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
    
    }
    
    
    
  else if   (digitalRead(start_cyklu) == LOW and digitalRead(thermostat) == LOW and  teplota > 200 ) {  // mala dávka termostat
       sensorReading2 = analogRead(A0);
       
   
   
       
  
       digitalWrite(ENBp, HIGH);
 
    myStepper.setSpeed(motorSpeed2);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
    
    }







    



   

  


  
     

    if (digitalRead(start_cyklu) == HIGH) {
      analogWrite(M1, 0);
      digitalWrite(ENBp, LOW);
      long previousMillis = 0;
      unsigned long currentMillis = millis();


     

   
   
      
      
      }
     
 }
[color=#202124]even when using millins (1000), the engine stops every second and then continues



code 
[/color]
#include <Stepper.h>
#include <max6675.h>
#define iskra 13
#define plyn 12
#define thermostat 11
#define start_cyklu 10
#define ENBp A5
#define releiskra 6
#define releplyn 3
#define M1 9
#define kontrolka 4
int thermoDO = A2;
int thermoCS = A3;
int thermoCLK = A4;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);




const int stepsPerRevolution = 200; 


Stepper myStepper(stepsPerRevolution, 2, 7, 5, 8);

int stepCount = 0; 
long previousMillis = 0;
unsigned long currentMillis = millis();
int thermo = 1;
int teplota = thermocouple.readCelsius();



 
void setup() {

 pinMode(iskra , INPUT_PULLUP); 
 pinMode(plyn , INPUT_PULLUP);
 pinMode(thermostat , INPUT_PULLUP);
 pinMode(start_cyklu , INPUT_PULLUP);
 pinMode(ENBp , OUTPUT);
 pinMode(releiskra , OUTPUT);
 pinMode(releplyn , OUTPUT);
 pinMode(M1 , OUTPUT);
 pinMode(kontrolka , OUTPUT);
 Serial.begin(9600);
 delay(500);

 
 unsigned long currentMillis = millis();

 
 

 
}



void loop() {
 


int sensorReading = analogRead(A0);
int sensorReading2 = analogRead(A0);
int sensorReadingV = analogRead(A1);
int motorSpeed = map(sensorReading, 0, 1023, 100, 200);
int motorSpeed2 = map(sensorReading2, 0, 1023, 50, 100);
int motorSpeedV = (sensorReadingV ) / 4;


 
unsigned long currentMillis = millis();

 if (currentMillis - previousMillis >= 1000) {
 Serial.print("C = "); 
Serial.println(thermocouple.readCelsius());
 int teplota = thermocouple.readCelsius();
 delay(500);
 previousMillis = currentMillis;

 }
 
 
 




 if (digitalRead(iskra) == LOW) {
 digitalWrite(releiskra, HIGH);
 
 }
 else if (digitalRead(iskra) == HIGH) {
 digitalWrite(releiskra, LOW);
 }

 if (digitalRead(plyn) == LOW) {
 digitalWrite(releplyn, HIGH);
 
 }
 else if (digitalRead(plyn) == HIGH) {
 digitalWrite(releplyn, LOW);
 }
 




if (digitalRead(start_cyklu) == LOW ) {
 analogWrite(M1, motorSpeedV);

 
 }



if (digitalRead(start_cyklu) == HIGH and (thermocouple.readCelsius() < 200.00)) {
 previousMillis = currentMillis;
 digitalWrite(kontrolka, LOW);
 }
 else if (digitalRead(start_cyklu) == LOW and (thermocouple.readCelsius() < 200.00) and currentMillis - previousMillis > 60000){
 digitalWrite(kontrolka, HIGH);
 }
 
 
 
 
 




 
 

 
 



 
 
 

 
 
 

 

 
 

 
 
 
 
 if (digitalRead(start_cyklu) == LOW and digitalRead(thermostat) == HIGH and teplota > 200 ) {// velka dávka termostat 
 sensorReading = analogRead(A0);
 
 
 
 
 digitalWrite(ENBp, HIGH);
 myStepper.setSpeed(motorSpeed);
 // step 1/100 of a revolution:
 myStepper.step(stepsPerRevolution / 100);
 
 }
 
 
 
 else if (digitalRead(start_cyklu) == LOW and digitalRead(thermostat) == LOW and teplota > 200 ) { // mala dávka termostat
 sensorReading2 = analogRead(A0);
 
 
 
 
 
 digitalWrite(ENBp, HIGH);
 
 myStepper.setSpeed(motorSpeed2);
 // step 1/100 of a revolution:
 myStepper.step(stepsPerRevolution / 100);
 
 }







 



 

 


 
 

 if (digitalRead(start_cyklu) == HIGH) {
 analogWrite(M1, 0);
 digitalWrite(ENBp, LOW);
 long previousMillis = 0;
 unsigned long currentMillis = millis();


 

 
 
 
 
 }
 
 }

You have
delay(500);
in your code so that will account for half of the problem
Check out my detailed tutorial on Multi-tasking in Arduino which includes a detailed stepper example
and the tools you need to track down your loop delay.
Also loop at How to write Timers and Delays in Arduino

You still have issues I pointed out earlier:

  • previousMillis should be an 'unsigned long', not a long.

  • There are several places where you probably think you are setting the global variables 'previousMillis' and 'currentMillis' but you are actually creating a local variable of the same name and never setting the global variable.

You still have a delay in this code. You are re-declaring 'teplota' here and it is NOT setting the global 'teplota' value.

  if (currentMillis - previousMillis >= 1000) {
    Serial.print("C = ");
    Serial.println(thermocouple.readCelsius());
    int teplota = thermocouple.readCelsius();
    delay(500);
    previousMillis = currentMillis;
  }

Why are you directly reading the thermocouple here if you only want to read it every second? currentMillis - previousMillis will never be greater than 60000 since you are resetting previousMillis once per second every time you read the thermocouple.

  if (digitalRead(start_cyklu) == HIGH and (thermocouple.readCelsius() < 200.00)) {
    previousMillis = currentMillis;
    digitalWrite(kontrolka, LOW);
  }
  else if (digitalRead(start_cyklu) == LOW and (thermocouple.readCelsius() < 200.00) and currentMillis - previousMillis > 60000) {
    digitalWrite(kontrolka, HIGH);
  }

You are declaring local variables for 'previousMillis' and 'currentMillis' in the following code. This does nothing.

  if (digitalRead(start_cyklu) == HIGH) {
    analogWrite(M1, 0);
    digitalWrite(ENBp, LOW);
    long previousMillis = 0;
    unsigned long currentMillis = millis();
  }

You need to understanding scoping of variables in C/C++. Here is a tutorial:

C++ Variable Scope

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