Components and supplies
Arduino UNO
Project description
Code
Arduino Code
arduino
This is the code that recives the fan speed updates and acts accordingly.
1/* 2 * Thanks to https://www.youtube.com/watch?v=vHeG3Gt6STE for the tutorial 3 * Edited by Simon Granstrm 4 */ 5 6word VentPin = 3; 7String inputString = ""; // a string to hold incoming data 8boolean stringComplete = false; // whether the string is complete 9String commandString = ""; 10 11boolean isConnected = false; 12 13byte lastFanSpeed = 0; 14 15void setup() { 16 pinMode(VentPin, OUTPUT); 17 Serial.begin(9600); 18 pwm25kHzBegin(); 19 pwmDuty(30); 20 /* 21 inputString = "#UPDATE45\ 22"; 23 stringComplete = true; 24 */ 25} 26 27void loop() { 28 CheckForSerial(); 29 30 if(stringComplete) 31 { 32 stringComplete = false; 33 getCommand(); 34 35 if(commandString.equals("UPDATE")) 36 { 37 UpdateFanSpeed(); 38 } 39 40 inputString = ""; 41 } 42} 43 44void pwm25kHzBegin() { 45 TCCR2A = 0; // TC2 Control Register A 46 TCCR2B = 0; // TC2 Control Register B 47 TIMSK2 = 0; // TC2 Interrupt Mask Register 48 TIFR2 = 0; // TC2 Interrupt Flag Register 49 TCCR2A |= (1 << COM2B1) | (1 << WGM21) | (1 << WGM20); // OC2B cleared/set on match when up/down counting, fast PWM 50 TCCR2B |= (1 << WGM22) | (1 << CS21); // prescaler 8 51 OCR2A = 79; // TOP overflow value (Hz) 52 OCR2B = 0; 53} 54 55void pwmDuty(byte ocrb) { 56 OCR2B = ocrb; // PWM Width (duty) 57} 58 59void getCommand() 60{ 61 if(inputString.length()>0) 62 { 63 commandString = inputString.substring(1,7); 64 } 65} 66 67void UpdateFanSpeed() { 68 int newFanSpeed = (inputString.substring(7,inputString.length())).toInt(); 69 70 if (newFanSpeed != lastFanSpeed) { 71 pwmDuty((byte)(newFanSpeed / (100 / 79))); 72 lastFanSpeed = newFanSpeed; 73 } 74} 75 76void CheckForSerial() { 77 while (Serial.available()) { 78 // get the new byte: 79 char inChar = (char)Serial.read(); 80 // add it to the inputString: 81 inputString += inChar; 82 // if the incoming character is a newline, set a flag 83 // so the main loop can do something about it: 84 if (inChar == '\n') { 85 stringComplete = true; 86 } 87 } 88} 89
C# Program
csharp
This is the program that reads and sends updates to the arduino. Some code is unnecessary but I don't have the motivation to clean it up right now.
1using System; 2using System.Collections.Generic; 3using System.IO; 4using System.IO.Ports; 5using System.Linq; 6using System.Management; 7using System.Runtime.InteropServices; 8using System.Text; 9using System.Threading.Tasks; 10using OpenHardwareMonitor.Hardware; 11 12namespace ArduinoPWMController 13{ 14 public class Program 15 { 16 [DllImport("kernel32.dll")] 17 static extern IntPtr GetConsoleWindow(); 18 19 [DllImport("user32.dll")] 20 static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 21 22 const int SW_HIDE = 0; 23 const int SW_SHOW = 5; 24 25 static void Main(string[] args) 26 { 27 /* 28 * Cred to : https://stackoverflow.com/a/3571628 for the console hide function. 29 */ 30 var handle = GetConsoleWindow(); 31 32 // Hide or Show console 33 ShowWindow(handle, SW_HIDE); 34 35 Controller controller = new Controller(); 36 37 while (true) 38 { 39 float cpuTemp = CpuTemperature.CpuTemp.GetCpuTemp(); 40 Console.WriteLine("Current temp: " + cpuTemp.ToString()); 41 42 if (cpuTemp < 50) //Under 40 C 43 { 44 controller.UpdateFanSpeed(35); 45 } 46 else if (cpuTemp >= 50 && cpuTemp < 57) //Between 50 and 57 C 47 { 48 controller.UpdateFanSpeed(45); 49 } 50 else if (cpuTemp >= 57 && cpuTemp < 62) //Between 57 and 62C 51 { 52 controller.UpdateFanSpeed(55); 53 } 54 else //Above 62 C 55 { 56 controller.UpdateFanSpeed(100); 57 } 58 59 System.Threading.Thread.Sleep(5000); 60 } 61 } 62 } 63 64 /* 65 * Credit for the base controller class https://www.youtube.com/watch?v=vHeG3Gt6STE 66 * Edited by: Simon Granstrm 67 */ 68 69 public class Controller 70 { 71 bool isConnected = false; 72 string selectedPort; 73 String[] ports; 74 SerialPort port; 75 76 public Controller() 77 { 78 getAvailableComPorts(); 79 80 /*foreach (string port in ports) 81 { 82 Console.WriteLine(port); 83 } 84 Console.WriteLine(""); 85 86 Console.Write("Choose a port: "); 87 88 selectedPort = Console.ReadLine(); 89 90 Console.WriteLine("");*/ 91 92 selectedPort = "COM3"; 93 94 connectToArduino(); 95 } 96 97 private void getAvailableComPorts() 98 { 99 ports = SerialPort.GetPortNames(); 100 } 101 102 private void connectToArduino() 103 { 104 isConnected = true; 105 port = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One); 106 port.Open(); 107 } 108 109 private void disconnectFromArduino() 110 { 111 isConnected = false; 112 port.Close(); 113 } 114 115 public void UpdateFanSpeed(int _percentage) 116 { 117 //float arduinoValue = _percentage / (100f / 79f); 118 119 //Console.WriteLine("Sending: " + arduinoValue.ToString()); 120 121 string _send = "#UPDATE" + _percentage.ToString() + "\ 122"; 123 port.Write(_send); 124 Console.WriteLine("Sent: " + _send); 125 } 126 } 127} 128 129/* 130 * Major thanks to https://openhardwaremonitor.org/downloads/. 131 * Also thanks to https://www.hackster.io/haoming-weng/get-cpu-temperature-using-open-hardware-monitor-in-c-1a3338 for the instructions. 132 */ 133 134namespace CpuTemperature 135{ 136 class CpuTemp 137 { 138 public class UpdateVisitor : IVisitor 139 { 140 public void VisitComputer(IComputer computer) 141 { 142 computer.Traverse(this); 143 } 144 public void VisitHardware(IHardware hardware) 145 { 146 hardware.Update(); 147 foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this); 148 } 149 public void VisitSensor(ISensor sensor) { } 150 public void VisitParameter(IParameter parameter) { } 151 } 152 153 public static float GetCpuTemp() 154 { 155 UpdateVisitor updateVisitor = new UpdateVisitor(); 156 Computer computer = new Computer(); 157 computer.Open(); 158 computer.CPUEnabled = true; 159 computer.Accept(updateVisitor); 160 161 float combinedTemp = 0; 162 int numberOfSensors = 0; 163 164 for (int i = 0; i < computer.Hardware.Length; i++) 165 { 166 if (computer.Hardware[i].HardwareType == HardwareType.CPU) 167 { 168 for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++) 169 { 170 if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature) 171 { 172 combinedTemp += (float)computer.Hardware[i].Sensors[j].Value; 173 numberOfSensors++; 174 } 175 } 176 } 177 } 178 179 float avgTemp = combinedTemp / numberOfSensors; 180 181 computer.Close(); 182 183 return avgTemp; 184 } 185 } 186} 187
Arduino Code
arduino
This is the code that recives the fan speed updates and acts accordingly.
1/* 2 * Thanks to https://www.youtube.com/watch?v=vHeG3Gt6STE for the 3 tutorial 4 * Edited by Simon Granstrm 5 */ 6 7word VentPin = 3; 8String 9 inputString = ""; // a string to hold incoming data 10boolean stringComplete 11 = false; // whether the string is complete 12String commandString = ""; 13 14boolean 15 isConnected = false; 16 17byte lastFanSpeed = 0; 18 19void setup() { 20 pinMode(VentPin, 21 OUTPUT); 22 Serial.begin(9600); 23 pwm25kHzBegin(); 24 pwmDuty(30); 25 /* 26 27 inputString = "#UPDATE45\ 28"; 29 stringComplete = true; 30 */ 31} 32 33void 34 loop() { 35 CheckForSerial(); 36 37 if(stringComplete) 38 { 39 stringComplete 40 = false; 41 getCommand(); 42 43 if(commandString.equals("UPDATE")) 44 45 { 46 UpdateFanSpeed(); 47 } 48 49 inputString = ""; 50 51 } 52} 53 54void pwm25kHzBegin() { 55 TCCR2A = 0; // 56 TC2 Control Register A 57 TCCR2B = 0; // TC2 Control 58 Register B 59 TIMSK2 = 0; // TC2 Interrupt Mask 60 Register 61 TIFR2 = 0; // TC2 Interrupt Flag Register 62 63 TCCR2A |= (1 << COM2B1) | (1 << WGM21) | (1 << WGM20); // OC2B cleared/set on 64 match when up/down counting, fast PWM 65 TCCR2B |= (1 << WGM22) | (1 << CS21); 66 // prescaler 8 67 OCR2A = 79; // TOP overflow 68 value (Hz) 69 OCR2B = 0; 70} 71 72void pwmDuty(byte ocrb) { 73 OCR2B = ocrb; 74 // PWM Width (duty) 75} 76 77void getCommand() 78{ 79 80 if(inputString.length()>0) 81 { 82 commandString = inputString.substring(1,7); 83 84 } 85} 86 87void UpdateFanSpeed() { 88 int newFanSpeed = (inputString.substring(7,inputString.length())).toInt(); 89 90 91 if (newFanSpeed != lastFanSpeed) { 92 pwmDuty((byte)(newFanSpeed / (100 93 / 79))); 94 lastFanSpeed = newFanSpeed; 95 } 96} 97 98void CheckForSerial() 99 { 100 while (Serial.available()) { 101 // get the new byte: 102 char inChar 103 = (char)Serial.read(); 104 // add it to the inputString: 105 inputString += 106 inChar; 107 // if the incoming character is a newline, set a flag 108 // so 109 the main loop can do something about it: 110 if (inChar == '\ 111') { 112 stringComplete 113 = true; 114 } 115 } 116} 117
Comments
Only logged in users can leave comments
MyName1sSimon
0 Followers
•0 Projects
Table of contents
Intro
5
0