PWM counter problem when counting down

Hello! Have a project in mind that can control DC motor with pwm, and it should be able to count up and down with some different levels it should stay on, for example “51” "102 and so on, until 255. And when it reads a temp value it should increase or decrease with +1, it works when i count up but not down? i know my code isn’t perfect but i’m not far away…

int speedValue; //=0
float oldtemp=0;

#define gear_level_zero 0
#define gear_level_one 51
#define gear_level_two 102
#define gear_level_three 153
#define gear_level_four 204
#define gear_level_five 255

void loop(){

oldtemp=T; // T is a value i read from an NTC sensor, oldtemp is the older value from temp

if(T<10){ //51
speedValue++;
analogWrite(AN1, speedValue);
analogWrite(AN2, speedValue);
delay(100);
//Serial.println(speedValue);

if(speedValue > gear_level_one) {
speedValue = gear_level_one;

} else if(speedValue < gear_level_zero) {
speedValue = gear_level_zero;
}
else if(T<oldtemp){
speedValue–;

}

}
if(T>10&&T<24){ //102
speedValue++;
analogWrite(AN1, speedValue);
analogWrite(AN2, speedValue);
delay(100);
//Serial.println(speedValue);

if(speedValue > gear_level_two) {
speedValue = gear_level_two;

} else if(speedValue < gear_level_zero) {
speedValue = gear_level_zero;
}
else if(T<oldtemp){

speedValue–;

}
Serial.println(speedValue);
}

coolingsystem.ino (3.83 KB)

@jimmyl97

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

You might not be far away but that code you posted is. T is not defined anywhere and nothing ever sets or changes it.

Steve

slipstick:
You might not be far away but that code you posted is. T is not defined anywhere and nothing ever sets or changes it.

Steve

Hi, actually it is defined, as you can see in the ino file i included, its a temp value i read from a ntc sensor :slight_smile:
So the only thing i'm stuck on is the count down, to the next "level" or "interval" with -1 and not a straight jump

Sorry, I can't be bothered to check an attachment to see if it has something different to the code in your post. Your problem is probably something to do with when and how often you read the sensor.

Steve

OP’s code in code tags:

#include <CytronMotorDriver.h>

int DIG1 = 7; // Arduino pin 7 is connected to MDDS60 pin DIG1.
int DIG2 = 4; // Arduino pin 4 is connected to MDDS60 pin DIG2.
int AN1 = 6; // Arduino pin 6 is connected to MDDS60 pin AN1.
int AN2 = 5; // Arduino pin 5 is connected to MDDS60 pin AN2.
int speedValue;  //=0
float oldtemp=0;
int temp = 43;
#define SPEED_VALUE_MAX 20
#define SPEED_VALUE_MIN 0

//int speedValue = 0;

#define gear_level_zero 0
#define gear_level_one 51
#define gear_level_two 102
#define gear_level_three 153
#define gear_level_four 204
#define gear_level_five 255

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;


void setup() {
  // put your setup code here, to run once:
  pinMode(DIG1, OUTPUT); // Set Arduino pin 7 (DIG1) as output.
  pinMode(DIG2, OUTPUT); // Set Arduino pin 4 (DIG2) as output.
  pinMode(AN1, OUTPUT); // Set Arduino pin 6 (AN1) as output.
  pinMode(AN2, OUTPUT); // Set Arduino pin 5 (AN2) as output.
  Serial.begin(9600);
  //delay(2000); // Delay for 5 seconds.
}

void loop() {
  // put your main code here, to run repeatedly:
  countUP();
  //countDOWN();
  //speedintervals();
  
  Vo = analogRead(ThermistorPin);
  R2 = R1 * (675.18 / (float)Vo - 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T = T - 273.15;
  //T = (T * 9.0)/ 5.0 + 32.0; 
  //T=7;
  
  Serial.print("Temperature: "); 
  Serial.print(T);
  Serial.println(" C"); 

  delay(100);
}

void countUP(){
  
  
  if(T<10){             //51
    speedValue++;
    analogWrite(AN1, speedValue);
    analogWrite(AN2, speedValue);
    delay(100);
    //Serial.println(speedValue);
    
    if(speedValue > gear_level_one) {
      speedValue = gear_level_one;
      
    } else if(speedValue < gear_level_zero) {
      speedValue = gear_level_zero;
    }
    else if(T<oldtemp){
      speedValue--;
      
    }
      
  }
    if(T>10&&T<24){     //102
    speedValue++;
    analogWrite(AN1, speedValue);
    analogWrite(AN2, speedValue);
    delay(100);
    //Serial.println(speedValue);
        
    if(speedValue > gear_level_two) {
      speedValue = gear_level_two;
    
      
      
     
      
    } else if(speedValue < gear_level_zero) {
      speedValue = gear_level_zero;
    }
    else if(T<oldtemp){
      
      speedValue--;
      
    }
    
  }
    
    
    if(T>24&&T<30){       //153
    speedValue++;
    analogWrite(AN1, speedValue);
    analogWrite(AN2, speedValue);
    delay(100);
    //Serial.println(speedValue);
        
    if(speedValue > gear_level_three) {
      
      speedValue = gear_level_three;
      
    } else if(speedValue < gear_level_zero) {
      speedValue = gear_level_zero;
    }
    else if(T<oldtemp){
      speedValue--;
      
    }
   
  }
    if(T>30&&T<40){      //204
    speedValue++;
    analogWrite(AN1, speedValue);
    analogWrite(AN2, speedValue);
    delay(100);
    //Serial.println(speedValue);
        
    if(speedValue > gear_level_four) {
      speedValue = gear_level_four;
      
    } else if(speedValue < gear_level_zero) {
      speedValue = gear_level_zero;
    }
    else if(T<oldtemp){
      speedValue--;
      
    }
    
  }
    if(T>40&&T<60){      //255
    speedValue++;
    analogWrite(AN1, speedValue);
    analogWrite(AN2, speedValue);
    delay(100);
    //Serial.println(speedValue);
        
    if(speedValue > gear_level_five) {
      speedValue = gear_level_five;
      
    } else if(speedValue < gear_level_zero) {
      speedValue = gear_level_zero;
    }
    else if(T<oldtemp){
      speedValue--;
      
    }
  }
  oldtemp=T;
//#########################################################
  
    Serial.println(speedValue);
}

One thing I quickly noticed, the test ranges for T have gaps between them…

@Markt, Thanks for answer! Don’t really understand what u mean by your answer tho… what kind of gaps between them do u mean? :slight_smile:

What happens if, for example, T == 10?

PerryBebbington:
What happens if, for example, T == 10?

@PerryBebbington good question, haven't thought about that but the value stays on 1, so my statements are kinda wrong, but the problem is when i tried it with my NTC sensor and it first goes up to 25 celsius, then down to 23 (or something under 24), it just jumps to the value 102 instead of counting down 1 by 1 to 102 from 153.. hope u understand what i mean :)

Each time you are in any temperature range you increase the speedValue by 1 and then if it hasn't hit one of the limits, and the temperature has gone down, it decreases speedValue by 1. Since you either add one or add one and subtract one it will never go down. Perhaps you should subtract 2 when the temperature has gone down.

Note that it will only go down when the temperature CHANGES. If the temperature goes down and then stays there the speedValue will only go up.

What is it you are trying to do?!?

johnwasser:
Each time you are in any temperature range you increase the speedValue by 1 and then if it hasn’t hit one of the limits, and the temperature has gone down, it decreases speedValue by 1. Since you either add one or add one and subtract one it will never go down. Perhaps you should subtract 2 when the temperature has gone down.

Note that it will only go down when the temperature CHANGES. If the temperature goes down and then stays there the speedValue will only go up.

What is it you are trying to do?!?

Hi John, thanks for answer, i’m trying to do a automatic fan system, similar to a fansystem for a pc. Still don’t understand what u are saying , can u give me a short code explanation?

jimmyl97:
@PerryBebbington good question, haven't thought about that but the value stays on 1, so my statements are kinda wrong, but the problem is when i tried it with my NTC sensor and it first goes up to 25 celsius, then down to 23 (or something under 24), it just jumps to the value 102 instead of counting down 1 by 1 to 102 from 153.. hope u understand what i mean :)

Not withstanding what others have mentioned, which is also valid, I don't think you understood what I was asking. Look closely at the ranges you have defined in your if statements and ask yourself which one is true for 10 and for all the other intermediate values.

PerryBebbington:
Not withstanding what others have mentioned, which is also valid, I don’t think you understood what I was asking. Look closely at the ranges you have defined in your if statements and ask yourself which one is true for 10 and for all the other intermediate values.

ye thanks, i’ve changed it now
T<=10;
T>=&&T<=24;
T>24&&T<=30;
T>30&&T<=40;
T>40&&T<=60;
BUT the problem is still that when it goes from >24 and down to 22 celsius, it jumps straight to 102 in value and not decreasing by 1… so it something wrong with my intervals or maybe something else

OK, good.

I think the other people helping you have some ideas about the other problems, I'll leave them to help.

The range for speedValue is -32,768 to 32,767.
The range for analogWrite is 0 to 255.

jimmyl97:
Hi John, thanks for answer, i'm trying to do a automatic fan system, similar to a fansystem for a pc.

You could map temperature directly to fan speed:

speedValue = map(constrain(T, 0, 60), 0, 60, 0, 255);

For temperatures below 0 the fan stops completely. For temperatures above 60 the fan runs full speed. For temperatures between the fan speed is proportional to the temperature. Unless you are expecting the temperature to suddenly change there is no reason to slowly ramp between fan speeds.
If you don't like a linear ramp you can use the MultiMap library instead of map(). MultiMap does piecewise linear interpolation. You set up a table, like you have, of temperatures and speeds. You put in a temperature and you get a speed out.

johnwasser:
You could map temperature directly to fan speed:

speedValue = map(constrain(T, 0, 60), 0, 60, 0, 255);

For temperatures below 0 the fan stops completely. For temperatures above 60 the fan runs full speed. For temperatures between the fan speed is proportional to the temperature. Unless you are expecting the temperature to suddenly change there is no reason to slowly ramp between fan speeds.
If you don't like a linear ramp you can use the MultiMap library instead of map(). MultiMap does piecewise linear interpolation. You set up a table, like you have, of temperatures and speeds. You put in a temperature and you get a speed out.

Thank you for replying! first time seeing this map/constrain thing, much appreciated! :slight_smile:

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