trying to run my first statement exactly 1 time, until the next statement is executed. basically the PIR sensor is suppose to turn the fan on. it should not turn off till the push sensor is pressed. can this be accomplished with the if and else statements or do i need to use a bool?
int pwmPin = 12; // assigns pin 12 to variable pwm
int pot = A0; // assigns analog input A0 to variable pot
int c1 = 0; // declares variable c1
int c2 = 0; // declares variable c2
int buzzer = 2;
int PIR = 10;
#define INPUT_PULLUP 5
void setup() // setup loop
{
Serial.begin(9600);
pinMode(PIR,INPUT);
pinMode(pwmPin, OUTPUT);
pinMode(pot, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(INPUT_PULLUP, INPUT);
}
void loop()
{
int sensorVal = digitalRead(5);
Serial.println(sensorVal);
if(digitalRead(PIR) == HIGH)
{
Serial.println("Motion Detected, Turn On Fan");
c2= analogRead(pot);
c1= 1024-c2; // subtracts c2 from 1000 and saves the result in c1
digitalWrite(pwmPin, HIGH);
delayMicroseconds(c1);
digitalWrite(pwmPin, LOW);
delayMicroseconds(c2);
}
else (sensorVal == LOW);
{
digitalWrite(PIR,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(pwmPin,LOW);
}
}
PWM is accomplished by analogWrite(pwmPin, pwmValue)?
Take a llok at Arduino/reference for more.
When that works, please post the new code and continue.
i had the fan working with the potentiometer the way the code was. after adding in a pull up switch, and PIR sensor. i have 1. lost my adjustability with the potentiometer. and 2. the new additional part of code i added for the pull up switch to stop everything. doesn't accomplish anything. anyway, even switching over from digital to analog & changing the code to analog for those parts. its still the same outcome.
What follows is your code, each line commented where necessary, and commented out where I've made corrections. You can remove all the fluff, once you've understood what I did and why. Please ask if you don't understand something.
int pwmPin = 12; // assigns pin 12 to variable pwm
//more correctly, declares an int to hold the pin number for PWM output, and assigns it the value 12
int pot = A0; // assigns analog input A0 to variable pot
//similarly, declares an int to hold the pin number for the pot, and assigns it the value A0
int buzzer = 2;
int PIR = 10;
//for consistency, those last two need comments as well?
//#define INPUT_PULLUP 5
//INPUT_PULLUP is already 'system defined', giving it a new, different value breaks things
//I think you want the following:
int IRinput = 5;
int c1 = 0; // declares variable c1
int c2 = 0; // declares variable c2
void setup() // setup loop
//wrong, it's not a loop, it's a function
{
Serial.begin(9600);
pinMode(PIR, INPUT);
pinMode(pwmPin, OUTPUT);
pinMode(pot, INPUT);
pinMode(buzzer, OUTPUT);
// pinMode(INPUT_PULLUP, INPUT); //wrong
pinmode(IRinput, INPUT_PULLUP);
}
void loop()
{
int sensorVal = digitalRead(IRinput); //I think you wanted to read that input
Serial.println(sensorVal);
if (digitalRead(PIR) == HIGH)
{
Serial.println("Motion Detected, Turn On Fan");
c2 = analogRead(pot);
c1 = 1024 - c2; // subtracts c2 from 1000 and saves the result in c1
digitalWrite(pwmPin, HIGH);
delayMicroseconds(c1);
digitalWrite(pwmPin, LOW);
delayMicroseconds(c2);
}
//else (sensorVal == LOW);
else if (sensorVal == LOW) //maybe, try this. Note, two changes, add 'if' and lose ';'
{
digitalWrite(PIR, LOW);
digitalWrite(buzzer, LOW);
digitalWrite(pwmPin, LOW);
}
}