Untitled

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

int ledpin = 25;
int POT = 26;
int FAN = 27;
int menu = 0;
int speed;
int x;
int display;
int vlt;
int cur;
int pwr;
int rpm;

void setup() {
  Serial.begin(115200);
  pinMode(POT, INPUT);
  pinMode(FAN, OUTPUT);
  pinMode(ledpin, OUTPUT);
  delay(800);
  Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  Serial.println();
  Serial.print("Initializing");
  delay(500);
  Serial.print(".");
  delay(500);
  Serial.print(".");
  delay(500);
  Serial.print(".");
  delay(500);
  Serial.print(".");
  delay(500);
  Serial.print(".");
  delay(500);
  Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

  Serial.println("Press '0' to stop");
  Serial.println("Press '1' to potentiometer mode");
  Serial.println("Press '2' to computer mode");
}

void loop() {
  if (Serial.available() > 0) {
    menu = Serial.read();

    if (menu == '1') {
      digitalWrite(ledpin, LOW);
      x = analogRead(POT);
      speed = map(x, 0, 4096, 0, 255);
      analogWrite(FAN, speed);
      display = (speed * 100) / 255;
      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("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("----------");
      delay(400);
    } 

    else if (menu == '0') {
      digitalWrite(ledpin, HIGH);
      Serial.println("----------");
      Serial.println("Stopped!");
      Serial.println("----------");
      analogWrite(FAN, 0);
    }

    else if (menu == '2') {
      Serial.println("Enter fan speed (0 to 255):");
      while (Serial.available() == 0) {} 
      speed = Serial.parseInt();
      speed = constrain(speed, 0, 255);
      analogWrite(FAN, speed);
      display = (speed * 100) / 255;
      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