Hello, this is my first post on the forum. I've been hanging around a while trying to make guesses at the feasibility of some of my ideas by seeing if anyone has achieved them and how. My current project/idea has practical application as part of a larger project which is building a motor home.
Does anyone of you have time to give me some help in understanding what I'm doing wrong. I appreciate that im jumping in a little deeper than maybe I aught to but looking at code designed for similar functions.
I want to control the brightness of 12v LED strips through a mosfet module or a more complex PWM motor controller module I have which maybe necessary for power handling capability, although both will tested for efficiency. I want digital inputs(momentary triple through single pole paddle switches). this allows brightness control from two terminals in the motor home unlike a Pot.
Here is what I have so far. I have only created the step up code.
const int upPin = 3;
const int downPin = 4;
const int Mosfet = 11;
int up = digitalRead(upPin);
int increment = 30;
int val;
int up;
int lastval = 5;
}
void loop(){
if (up == HIGH) val = lastval + increment;
(lastval = val);
digitalWrite(Mosfet, val);
Serial.println(val);
if (up == LOW) val = lastval;
(lastval = val);
digitalWrite(Mosfet, val);
Serial.println(val);
delay(5);
}
The compiler has already pointed out to you some scope issues, but even when you've fixed those, you've got the classic noob mistake of testing variables you haven't given a value to (like "up"), and parentheses () when you should have braces {} (assuming that's what you meant).
I'm not sure what you're trying to do with the incrementing and decrementing "val" and then using it in a digitalWrite.
The IDE is packed with worked examples; why not try working through a few?
thanks for the reply. Scope issues are gone. you can probably see I am very new to this. Is this what you meant by assigning a value to up? I appreciate you don't want to write my code for me but can you give a clue on the braces? I will of course re-read the basics.
int increment = 30;
int val;
int up = LOW;
int lastval = 5;
const int upPin = 3;
const int downPin = 4;
const int Mosfet = 11;
int increment = 30;
int val;
int up = LOW;
int lastval = 5;
const int upPin = 3;
const int downPin = 4;
const int Mosfet = 11;
void setup ()
{
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
int up = digitalRead(upPin);
}
void loop()
{
if (up == HIGH) val = lastval + increment;
(lastval = val);
digitalWrite(Mosfet, val);
Serial.println(val);
if (up == LOW) val = lastval;
(lastval = val);
digitalWrite(Mosfet, val);
Serial.println(val);
delay(5);
}
You have a global variable 'up' (declared before setup()). In setup you use a different variable 'up'.
As you never read upPin in loop() and 'up' is initialised with LOW, only the second if condition in loop() will be satisfied; val = lastval + increment; will never be executed. I guess you need
void loop()
{
up = digitalRead(upPin);
if (up == HIGH) val = lastval + increment;
...
...
...
}
Lastly, the second parameter for digitalWrite will switch the pin LOW if val equals zero and HIGH in all other cases. So digitalWrite(Mosfet, 5) and digitalWrite(Mosfet, 500) will do exactly the same; maybe you want to use analogWrite
I want to control the brightness of 12v LED strips through a mosfet module or a more complex PWM motor controller module I have which maybe necessary for power handling capability, although both will tested for efficiency
LED strips tend to be inefficient because they use resistors for current limiting. (But, I don't know if they are less efficient than incandescent lighting.)
"High Power" LEDs used for room-lighting (as opposed to accent or effect lighting) are normally powered by a constant-current switchmode power supply. (Technically, the dimmable versions are actually controlled-current designs.)
I have been reading about these problems and i have decided to continue with my strips but also use some point sources(single chip or clusters) which i haven't yet decided on the form of to produce the bulk of the photons. would my motor controller or a mosfet with a smoothing cap(low pass filter) present a constant current to what ever LEDs do the dimming. i haven't decided if it all has to dim yet. so very open to suggestion. I appreciate LED lighting is a well developed field by now that many still don't know about the finer points of but probably gets simplified by generalist hobby engineers.