About analogWrite(PinX, 0), and analogWrite(Pinx, 255)

Discussions about the STM32generic core
Post Reply
csnol
Posts: 15
Joined: Thu Aug 24, 2017 12:57 pm

About analogWrite(PinX, 0), and analogWrite(Pinx, 255)

Post by csnol » Mon Sep 04, 2017 1:42 pm

Hi, All,

I'm testing a airpump system use analogWrite based STM32GENERIC. and my board was out of control on analogWrite(PinX, 0), and analogWrite(PinX, 255).

If I wanna use digitalWrite function, do I have to disabled PWM function? Aren't they in ONE Boat(sketch :D ) at sametime?

Thanks for any helps.

C.S Nol

Code: Select all

[
curms = millis();
if (runflag == 1) {
    switch (runmode) {
      case 0:
         analogWrite(pump, mpulse);
        if ((curms - prems) > 2000) {
          myGLCD.clrScr();
          prems = curms;
          if (mpulse == 0)                            // Wrong at here. 
          mpulse = 255;
          else 
          mpulse = 0;
        }        
        myGLCD.print("MODE0", CENTER, 20);
        itoa(analogRead(PA1), analogstr, 10);
        myGLCD.print(analogstr, CENTER, 40);
        readalarm(alarmlevel);
        break;
      case 1:
        analogWrite(pump, pulse);
        if ((curms - prems) > 2000) {
          prems = curms;
          if (pulse == 150) 
          pulse = 255;                           //Here, My pump should be run fast. but it 's stopped.
          else pulse = 150;
        }
        readalarm(alarmlevel);
        break;
      case 2:
        analogWrite(pump, fadenum);
        if ((curms - prems) > 300) {
          prems = curms;
 //         digitalWrite(pump, pumpflag);    // I didn't got the result I wanted once use digitalWrite.
          if (fadenum < 250) 
          fadenum += 15;
          else 
          fadenum = 100;
        }
        readalarm(alarmlevel);
        break;
    }
]

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: About analogWrite(PinX, 0), and analogWrite(Pinx, 255)

Post by danieleff » Mon Sep 04, 2017 5:38 pm

Use pinMode(pin, OUTPUT) to turn off analogWrite(), after that you can use digitalWrite() for that pin.

csnol
Posts: 15
Joined: Thu Aug 24, 2017 12:57 pm

Re: About analogWrite(PinX, 0), and analogWrite(Pinx, 255)

Post by csnol » Tue Sep 05, 2017 3:32 am

OK, Thank you! Daniel

Code: Select all

void closepump() {
analogWrite(pump,0);                // must be run first, otherwise you can't close the pump
pinMode(pump, OUTPUT);
digitalWrite(pump, LOW);
}

csnol
Posts: 15
Joined: Thu Aug 24, 2017 12:57 pm

Re: About analogWrite(PinX, 0), and analogWrite(Pinx, 255)

Post by csnol » Thu Sep 07, 2017 8:56 am

danieleff wrote:
Mon Sep 04, 2017 5:38 pm
Use pinMode(pin, OUTPUT) to turn off analogWrite(), after that you can use digitalWrite() for that pin.
@ danieleff,
analogWrite(PinX, 255) still is a problem. I'm trying to repair it.

Post Reply