0

Not good at coding at all, so I'm at a total loss here.

But I'm doing a project that involves controlling a larger DC voltage through a mosfet. What I'm doing is I'm using a HC-05 to transmit data between a phone and the Arduino Uno. Seeing the value within a couple of seconds is necessary.

However, sending the mosfet gate a command to open or close up work very inconsistently. Sometimes never and sometimes it takes up to 30 seconds for it to register.

The code here is a short version of the project that I'm working on, but behaves similarly.

#define mosfet 11
int state = 0;          
int gate = 0;
int maxgate = 255;      
int mingate = 0;        

void setup() {
  delay(2000);
  pinMode(mosfet, gate);
  analogWrite(mosfet, 0);   //starts closed
  Serial.begin(9600); 
}

void loop() {
  while(Serial.available() > 0){
    state = Serial.read();

    for (int y=0; y <= 5; y+=5){
      delay(500);
      Serial.println("testphase 1");
      delay(500);
    }

    for (int y=5; y >= 0; y--){
      delay(250);
      Serial.println("testphase 2");
      delay(250);
    }

    if (state == '0') {                   
    gate = gate + 1;
    if(gate > maxgate) gate = maxgate;
    analogWrite(mosfet, gate);                        
    Serial.println(int(gate));Serial.print(", +1"); 
    state = 0;
    }

  if (state == '1') {                                   
    gate = gate + 10;
    if(gate > maxgate) gate = maxgate;
    analogWrite(mosfet, gate);                          
    Serial.println(int(gate));Serial.print(", +10");    
    state = 0;
    }
} 
}  

When sending 'State == 1', for example it will go through both for loops multiple times and the maybe sometimes do gate + 10. Sometimes the commands goes completely ignored.

I have no idea how I would interrupt a loop to execute the given command and then go back.

  • Why do you have these for loops? If you want to do something timed but also be responsive to commands you can make your code non-blocking, like in the BlinkWithoutDelay example of the Arduino IDE. – chrisl Nov 25 '18 at 9:47
  • The pinMode() seems incorrect (because of the variable gate bring where the pinmode constant should be). But that shouldn't be a problem, since analogWrite() configures the pin correctly. – chrisl Nov 25 '18 at 9:52
  • How do you know, that it takes so long? Are you measuring the PWM signal, which goes to the MOSFET? You would need to send many times '0' or '1' to get to the maximum value – chrisl Nov 25 '18 at 9:55
  • Those state changing IF-loops are just a few of the many I have in my code, but I cut out most to just test the code. Even adding a +1 or a +10 to the gate variable should show some signs of life on an LED I'm using to test the system, before I hook it up to a more powerful DC source. – ProbablyGonnaBurnMyHouse Nov 25 '18 at 12:15
  • Also, adding to my previous comment. How do I know it takes so long? It should print the current value + the added amount when a change is detected and the change should be easily observable with the green LED that I'm using for testing. Although I now see that it actually just prints the current value + the added amount. – ProbablyGonnaBurnMyHouse Nov 25 '18 at 12:18
2

Actually got it working myself, but thank you so much for your help @chrisl

Ditched the for loop completely just to try things out and reorganized my code a bit. Surprisingly it started to work well enough.

Basically just moved my measuring stuff outside the while loop*(and now that I think about it, why would it ever be withing the while loop? Literally makes no sense)* and to the end of the whole void loop part, and only polled for state changes within the while loop while the BT connection was active.

Doesn't miss a command as far as I can tell, and the adjustment is within 2 seconds when using my whole code, which is perfect. Also the whole program doesn't just freeze itself while trying to think what happened when the change set was send.

Here's a short version, should somebody bump into this thread in the future.

void loop() {
   while(Serial.available() > 0){       
    state = Serial.read();              

  if (state == '0') {stuff happens here,
    }
    }  //End of the while loop.

  sensors.requestTemperatures();
  delay(1000); 
  Celcius=sensors.getTempCByIndex(0);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.