Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!
So how's it going?
Yeah! That's the one.
You can either store the pin state in a variable and then base the if off that stateswitch1State = digitalRead(10);
if (switch1State == 1){dostuff}, or you can call the digital read in the if: if (digitalRead(10) == 1){dostuff}
Either way works, different situations may call for one or another flavors, for this situation it doesn't really matter much.
If you're feeling advanced you can use a "boolean" instead of an int or a byte for the variable. A boolean can store either a 0(false) or a 1(true).
So you can say:
boolean switch1State;
switch1State = digitalRead(10);
if (switch1State) {dostuff} //this will do stuff if the switch returns 1, it is functionally identical to if (switch1State == 1), or if (switchstate == TRUE), if you want it to trigger on a 0 us:
if (!switch1State) {dostuff} instead, it's exactly the opposite.
Whether you use a boolean or a byte or an int doesn't really matter at this point, it only matters if you're trying to make you code execute faster (a boolean check takes fewer clock cycles than an integer check, for instance), or save on ram (a boolean takes one bit, an integer takes 16 bits).
You can set targetpin to output in the function, it's clunky but it works.
Or you can set 8 to output before that.
That said, pin8 doesn't have PWM so you won't be able to do anything but turn a fan on and off with it.
If you want to do PWM you'll need to use one of the following: 3, 5, 6, 9, 10, 11.
Then you can "analogWrite(pinNumber, Level);"
With Level being from 0 (full off) to 255 (full on).
You'll need to use a transistor to switch the fan, it'll chunk the Arduino if you connect it directly. (Unless its a four wire fan and you hook the PWM line to the Arduino).
Oh, I think a fundamental just clicked....when you call the function, you are assigning values to those variables in the brackets in the function line?
So it's like saying targetPin = 3;
level = 255;
then having the function code inside the void loop but you keep it modular this way?
Here's my updated code....
/*
* Workshop 4 Task 01
* This program will switch a fan on for 1 second, then off for 1 second
*/
void setup(){
}
void loop(){
setSpeed(3,255);
}
void setSpeed(int targetPin, int level){
//set pwm pin output to "level"
analogWrite(targetPin,level);
delay(1000); //wait for a second
//set the pwm pin output to low
analogWrite(targetPin,LOW);
delay(1000); //wait for a second
}
That's actually an excellent way to do it, really.
Couple minor things:
You'll need to turn the LEDs off if you want to be able to turn the pot the other way and have something happen.
Analog input range is 0-1023.
Other than that, it looks great.
analogRead automatically sets the pin to input, so you don't have to worry about that.
You can pinMode it like that if you want though.
Your LED program looks like it should work!
I've been wondering how to make lookup tables, so I'm not going to be a huge help on that one
I'll look into it though, it's something I need to figure out.