Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
9.1 kB
3
Indexable
Never
#include <Arduino.h>


const int ledpin = 25;
const int POT = 26;
const int FAN = 27;
const int buzz = 17;
int menu = 0;
int counter = 0; 
float speed;
float x;
float display;
float vlt;
float cur;
float pwr;
float rpm;
const int but1 = 33;
const int but2 = 35;
const int but3 = 32;

int butstate1;
int lastbut1 = LOW;

unsigned long lastbut1Time = 0;

int butstate2;
int lastbut2 = LOW;

unsigned long lastbut2Time = 0;

int butstate3;
int lastbut3 = LOW;

unsigned long lastbut3Time = 0;
unsigned long debounceDelay = 50;



void setup() {


  Serial.begin(115200);
 
  pinMode(POT, INPUT);
  pinMode(but1, INPUT_PULLUP);
  pinMode(but2, INPUT_PULLUP);
  pinMode(but3, INPUT_PULLUP);
  pinMode(FAN, OUTPUT);
  pinMode(ledpin, OUTPUT);
  pinMode(buzz, OUTPUT);
  delay(800);
  digitalWrite(ledpin, HIGH);
  tone(buzz,1535,300);
  delay(400);


  


  
  Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  Serial.println();
  Serial.print("Initializing");
  delay(200);
  Serial.print(".");
  delay(300);
  Serial.print(".");
  delay(300);
  Serial.print(".");
  delay(300);
  Serial.print(".");
  delay(300);
  Serial.print(".");
  delay(300);
  Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  

 


  digitalWrite(ledpin, LOW);
  Serial.println("Press '0' to stop");
  Serial.println("Press '1' to potentiometer mode");
  Serial.println("Press '2' to computer mode");
         Serial.println("Press stop button to stop ");
        Serial.println("Press left button to subtract %5 speed");
        Serial.println("Press right button to add %5 speed");
}

void loop() {

int push1 = digitalRead(but1);

if (push1 != lastbut1) {
    
    lastbut1Time = millis();
  }
if ((millis() - lastbut1Time) > debounceDelay) {
    
    if (push1 != butstate1) {
      butstate1 = push1;

      
      if (butstate1 == HIGH) {
       
  
      digitalWrite(ledpin, HIGH);
        tone(buzz,500,1400);
      

      Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      Serial.println("----------");
      Serial.println("Stopped!");
      Serial.println("----------");

      counter = 0;
      analogWrite(FAN, 0);
      }
    }
  }
  lastbut1 = push1;

        int push2 = digitalRead(but2);

if (push2 != lastbut2) {
    
    lastbut2Time = millis();
  }
if ((millis() - lastbut2Time) > debounceDelay) {
    
    if (push2 != butstate2) {
      butstate2 = push2;

      
      if (butstate2 == HIGH) {
       
      tone(buzz,1100,100);
      delay(50);
      tone(buzz,1300,100);
      counter += 5; 
       Serial.print("Current speed : ");
  Serial.println(counter);


      Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      Serial.println("%5 increase");

  digitalWrite(ledpin, LOW);


      Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      Serial.println("Press left button to subtract %5 speed");
      Serial.println("Press right button to add %5 speed");



      display = counter;
      display = constrain(display, 0, 100);
      speed = map(display, 0, 100, 0, 255);
      analogWrite(FAN, speed);
      vlt = map(display, 0, 100, 0, 12);
      cur = map(display, 0, 100, 0, 22);
      pwr = map(display, 0, 100, 0, 150);
      rpm = map(display, 0, 100, 0, 13000);


      Serial.print("Fan speed set to: ");
      Serial.print(speed);
      Serial.println();

      Serial.print("Percentage of fan speed (%) :");
      Serial.print(display);
      Serial.println();

      Serial.print("Voltage (V):");
      Serial.print(vlt);
      Serial.println();

      Serial.print("Current (mA) :");
      Serial.print(cur);
      Serial.println();

      Serial.print("Power (mW) :");
      Serial.print(pwr);
      Serial.println();

      Serial.print("Speed (rpm) :");
      Serial.print(rpm);
      Serial.println();

      Serial.println("----------");
      Serial.println("Press left button to subtract %5 speed");
      Serial.println("Press right button to add %5 speed");
  if (counter > 100) {
    counter = 100;
   
  }
    }
      }
      
    }
  
  lastbut2 = push2;



        int push3 = digitalRead(but3);

if (push3 != lastbut3) {
    
    lastbut3Time = millis();
  }
if ((millis() - lastbut3Time) > debounceDelay) {
    
    if (push3 != butstate3) {
      butstate3 = push3;

      
      if (butstate3 == HIGH) {
       
      tone(buzz,1100,100);
      delay(50);
      tone(buzz,900,100);
      counter -= 5; 

      digitalWrite(ledpin, LOW);
      Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      display = counter;
      display = constrain(display, 0, 100);
      speed = map(display, 0, 100, 0, 255);
      analogWrite(FAN, speed);
      vlt = map(display, 0, 100, 0, 12);
      cur = map(display, 0, 100, 0, 22);
      pwr = map(display, 0, 100, 0, 150);
      rpm = map(display, 0, 100, 0, 13000);


      Serial.print("Fan speed set to: ");
      Serial.print(speed);
      Serial.println();

      Serial.print("Percentage of fan speed (%) :");
      Serial.print(display);
      Serial.println();

      Serial.print("Voltage (V):");
      Serial.print(vlt);
      Serial.println();

      Serial.print("Current (mA) :");
      Serial.print(cur);
      Serial.println();

      Serial.print("Power (mW) :");
      Serial.print(pwr);
      Serial.println();

      Serial.print("Speed (rpm) :");
      Serial.print(rpm);
      Serial.println();

      Serial.println("----------");
      Serial.println("Press left button to subtract %5 speed");
      Serial.println("Press right button to add %5 speed");
  if (counter < 0) {
    counter = 0;
   
  }
  

    }

      
      }
      
    }
  
  lastbut3 = push3;


   if (Serial.available() > 0) {
   menu = Serial.read();
   push1 = digitalRead(but1);
  
    if (menu == '1') {
      tone(buzz,1000,100);
      delay(50);
      tone(buzz,850,100);
      delay(50);
      tone(buzz,1150,100);
      for(int i = 0; i <= 1; i++) {
        digitalWrite(ledpin, LOW);
        x = analogRead(POT);
        speed = map(x, 0, 4096, 0, 255);
        analogWrite(FAN, speed);
        display = (speed * 100) / 255;
        counter = display;
        vlt = map(display, 0, 100, 0, 12);
        cur = map(display, 0, 100, 0, 22);
        pwr = map(display, 0, 100, 0, 150);
        rpm = map(display, 0, 100, 0, 13000);


        Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        Serial.print("Percentage of fan speed (%) :");
        Serial.print(display);
        Serial.println();

        Serial.print("Voltage (V):");
        Serial.print(vlt);
        Serial.println();

        Serial.print("Current (mA) :");
        Serial.print(cur);
        Serial.println();

        Serial.print("Power (mW) :");
        Serial.print(pwr);
        Serial.println();

        Serial.print("Speed (rpm) :");
        Serial.print(rpm);
        Serial.println();

        Serial.println("----------");
        Serial.println("Press '1' again to read new values");
        Serial.println("----------");
#

       
      }
    } 

    else if (menu == '0') {
  
      digitalWrite(ledpin, HIGH);
      tone(buzz,500,1400);
      counter = 0;
      Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      Serial.println("----------");
      Serial.println("Stopped!");
      Serial.println("----------");


      analogWrite(FAN, 0);
    }

    else if (menu == '2') {
      
      tone(buzz,1000,100);
      delay(130);
      tone(buzz,850,100);
      delay(130);
      tone(buzz,1150,180);
      digitalWrite(ledpin, LOW);


      Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
      Serial.println("Enter percentage of fan speed % (0 to 100):");


      while (Serial.available() == 0) {} 
      tone(buzz,1100,100);
      delay(350);
      tone(buzz,1100,100);
      display = Serial.parseInt();
      display = constrain(display, 0, 100);
      counter = display;
      speed = map(display, 0, 100, 0, 255);
      analogWrite(FAN, speed);
      vlt = map(display, 0, 100, 0, 12);
      cur = map(display, 0, 100, 0, 22);
      pwr = map(display, 0, 100, 0, 150);
      rpm = map(display, 0, 100, 0, 13000);


      Serial.print("Fan speed set to: ");
      Serial.print(speed);
      Serial.println();

      Serial.print("Percentage of fan speed (%) :");
      Serial.print(display);
      Serial.println();

      Serial.print("Voltage (V):");
      Serial.print(vlt);
      Serial.println();

      Serial.print("Current (mA) :");
      Serial.print(cur);
      Serial.println();

      Serial.print("Power (mW) :");
      Serial.print(pwr);
      Serial.println();

      Serial.print("Speed (rpm) :");
      Serial.print(rpm);
      Serial.println();

      Serial.println("----------");
      Serial.println("Press '2' again to set a new speed");
      Serial.println("----------");

    }


   }
}

Leave a Comment