Results 1 to 2 of 2

Thread: Using a potentiometer to control a fan

  1. #1
    Join Date
    Nov 2019
    Posts
    3
    Post Thanks / Like

    Default Using a potentiometer to control a fan

    I am writing a sketch that uses vixen to run a sequence. Everything worked fine up until I decided to use a potentiometer to control a fan to keep the board cool if needed. For some reason now, the potentiometer will not let vixen and arduino talk and it causes the lights to randomly start going. How do I fix?? Code is below:

    Code:
    int numberOfRelays = 8;
    int relay[] = {2, 3, 4, 5, 6, 7, 8, 9};
    int fan = A0;
    
    #define MAX_CHANNELS 8
     
    int ch;
    int state;
    int chVal[MAX_CHANNELS] = {0};
    int pins[] = {2, 3, 4, 5, 6, 7, 8, 9};
    
    enum states
    {
      IDLE,
      DELIM,
      READ,
      DISP
    };
     
    void setup(){
      pinMode(fan, OUTPUT);
    
      for (ch=0; ch<MAX_CHANNELS; ch++)
      {
        pinMode(pins[ch], OUTPUT);
        digitalWrite(pins[ch], LOW);
       }
      
      state = IDLE;
      ch = 0;
      
      Serial.begin(115200);
    }
    
     
    void loop(){
    
      int pot = analogRead(A1); //pot reads analog input A0 value
      int fan_speed = pot * (255 / 1023.0); //this scales the value between 0-255
      analogWrite(fan,fan_speed); //this sends pwm pulse to pin 9 of fan_speed no
      Serial.println(fan_speed); //this prints fanspeed value in serial
      delay(10); // delay of 10 to provide stability
      
      if (Serial.available())
      {
        switch (state)
        {
          case IDLE:  
            ch = 0;
            if (Serial.read() == '+')
            {
              state = DELIM;          
            }
            else
            {
              state = IDLE;
            }
          break;
            
          case DELIM:
            ch = 0;
            if (Serial.read() == '>')
            {
              state = READ;
            }
            else
            {
              state = IDLE;
            }
          break;
          
          case READ:
            chVal[ch++] = Serial.read();
            if (ch >= MAX_CHANNELS)
            {
              ch = 0;
              state = DISP;
            }
          break; 
          
          case DISP:
            state = IDLE;
            for (ch=0; ch<MAX_CHANNELS; ch++)
            {   
              //analogWrite(pins[ch], chVal[ch]); // Write current values to LED pins  
             
              
              if (chVal[ch] > 0)
              {
                digitalWrite(pins[ch], HIGH);
              }
              else
              {
                digitalWrite(pins[ch], LOW);
              }
              
            }
          break;
        }
      }
    }

  2. #2
    Join Date
    Nov 2009
    Location
    Brisbane - Australia
    Posts
    957
    Post Thanks / Like

    Default Re: Using a potentiometer to control a fan

    You're reading the analog pin, setting the PWM and sending serial on every loop!

    There's no need to read the pot more than every few hundred milliseconds. When you do, compare the new reading to the old one (you'll need another variable for that) and only update the PWM if it changed.
    www.da-share.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •