Hello,
I am new to arduino and programming. I am trying to control a pc fan with the option of manual setting which is fan on with speed control or an auto setting which will turn fan on high when the reedswitch is open. My code seems to work by itself, but when i place it all together, no matter what I do the fan is only controlled by the potentiometer and the code seems to ignore the reedswitch input. Here is a snip of my code.
// this constant won't change:
const int reedswitch = 4;//door switch
const int buttonPin1 = 2;//push button for light control
const int buttonPin2 = 3;//push button for fan control
const int ledPinonL = 12;//led showing state of light control
const int ledPinautoL = 11;//led showing state of light control
const int ledPinonF = 10;//led showing state of fan control
const int ledPinautoF = 9;//led showing state of fan control
const int analogOutPin = 6;//to fan
const int analogInPin = A0;//from potentiometer
int sensorValue = 0;
int outputValue = 0;
// Variables will change:
int buttonPushCounter1 = 0;
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonPushCounter2 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
void setup() {
pinMode(reedswitch, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(ledPinonL, OUTPUT);
pinMode(ledPinautoL, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPinonF, OUTPUT);
pinMode(ledPinautoF, OUTPUT);
pinMode(analogOutPin, OUTPUT);
pinMode(analogInPin, INPUT);
Serial.begin(9600);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 != lastButtonState1)
if (buttonState1 == HIGH)
{
buttonPushCounter1++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter1);
}
else {
Serial.println("off");
}
{
lastButtonState1 = buttonState1;
}
if (buttonPushCounter1 % 3 == 1) {
digitalWrite(ledPinonL, HIGH);
} else {
digitalWrite(ledPinonL, LOW);
}
if (buttonPushCounter1 % 3 == 2) {
digitalWrite(ledPinautoL, HIGH);
} else {
digitalWrite(ledPinautoL, LOW) ;
}
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 != lastButtonState2)
if (buttonState2 == HIGH)
{
buttonPushCounter2++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter2);
}
else {
Serial.println("off");
}
{
lastButtonState2 = buttonState2;
}
if (buttonPushCounter2 % 3 == 1) {
digitalWrite(ledPinonF, HIGH);//Turn on led for fan
} else {
digitalWrite(ledPinonF, LOW);
}
switch (buttonPushCounter2)
case 1:if (buttonPushCounter2 %3 == 1)
{analogWrite(analogOutPin, outputValue);
break;
case 2:if (buttonPushCounter2 %3 == 2 && reedswitch == LOW);
{analogWrite(analogOutPin, 255);}
break;
default: {analogWrite(analogOutPin, 0); }
if (buttonPushCounter2 % 3 == 2) {
digitalWrite(ledPinautoF, HIGH);//Turn on led for standby operation
} else {
digitalWrite(ledPinautoF, LOW) ;
}
{
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue,0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}}}
If anyone can help with code or suggest with example another way of performing the desired outcome i would much appreciate it.