Hello. I want to use PWM to send different signals to 4 leds that are connected to 4 buttons. The pwm signal must be "online" for 2 seconds. When i press a button one of the leds turns on using this function that sends pwm.
I want to make each led have different PWM, but the function to stay "online" for 2 seconds.
So it will be: void PWM (int lednumber, int pwm, int seconds) or something like this. I don`t want it to fade or something... just blink with different frequency for 2 seconds
LEDs connected to switches does not make sense. LEDs are connected to output pins. Switches are connected to input pins. Any "connection" between an input pin and an output pin is strictly in the code.
So my circuit is this one: i have 4 buttons connected to 4 digital pins, to 4 PWM pins i have connected 4 leds. When a button is pressed, one of the leds is turned on. But i don`t want it just to turn on. I want to make a function that keeps working with the led for 2 seconds. In this function i want to be able to change the PWM.
Basically one led will turn on 10 times in 2 seconds, other 6 times, and so on...
First: is this called that i send different signals on each led?
Second: how do i create this function, the instructions in it..
You don't need PWM to blink LEDs on and off. In fact, there is a minimum frequency for hardware PWM so you can't use it for this. Reading pushbutton switches and blinking is more than one task, so you need to look at using millis(), as it is used in the BlinkWithoutDelay example sketch.
Xeyow:
Let`s say i use what you said... The homework says: turn on leds with different frequency... Can arduino send different frequency signals to the leds?
PWM means the same frequency, but with different pulse width. So, 100Hz, but 75%, or 25%, or 10%, high, then the rest low. The fequency stays the same, only the high-low ratio changes.
So, for different frequencies, you would need to change the wavelength (the time between low to high transitions). Hence, use the blink without delay example, and modify it for multiple leds.
Oke. so now i know that i can send different frequencies. Can you provide me a function that blinks that led without delay, for 2 seconds, but with different frequencies?
For example: void blinkwithoutdelay (int LED, inf freq, int sec) {insert code}
Where: LED = the led i want to make it blink | FREQ = the frequency with which i want to make it blink, and SEC = the amount of time the led will blink.
I think you have a misunderstanding of PWM, i.e.Pulse Width Modulation. As implied in the term, it's the width of the the pulse that changes, not the frequency. Actually what is controlled is the ratio of on time to off time of the pulse. Check this out.
It seems you could accomplish what you want by determining how long an led should be on and then off, multiplied by the number of times cycled on and off to equal 2 seconds, e.g. (2 seconds/number of times to cycle on and off)/2.
Study Blink Without Delay to accomplish that and get it working for one led and then come back for advice to add leds.
Xeyow:
Oke. so now i know that i can send different frequencies. Can you provide me a function that blinks that led without delay, for 2 seconds, but with different frequencies?
For example: void blinkwithoutdelay (int LED, inf freq, int sec) {insert code}
Where: LED = the led i want to make it blink | FREQ = the frequency with which i want to make it blink, and SEC = the amount of time the led will blink.
Thanks
The point of the homework is for you to learn.
You code it up, and if it doesn't work, you can ask us to help you get it working no problem.
My code is big... has lot`s of leds / buttons / conditions...
The problem is: how do i send DIFFERENT frequency on each led? I need a program, that i can put it in my code, that sends different frequency to a led, so one time it will blink 5 times, other times 10...
Thats the only thing i need. And i dont know what to use, how to make it
Thats the only thing i need. And i dont know what to use, how to make it
Do NOT try to do "it" (whatever "it" is) for all LEDs and all switches at once.
Do "it" for one LED and one switch. When you can do "it" for one LED/switch, then it is easy to expand for other LEDs and switches.
Write down how YOU would make an LED turn on and off at regular intervals, with nothing more than a pencil and paper (variables) and a watch (milllis()). When you can do that, you can make an LED blink. When you can do that without using the delay() function, you can do that for any number of LEDs, by using arrays.
This is be pretty close to what you describe, but more importantly, this should be very easy to read, follow, understand.
// Make 4 LEDs blink with 4 different patterns using 4 buttons
// Digital pins that we connect buttons and LEDs to
const int outLedPins[4] = {2,3,4,5};
const int inButtonPins[4] = {10,11,12,13};
// The 4 different blinking patterns
const int blinkPattern[4][2] =
{ // ON, OFF (in msec)
{500,500}, // Slow blink
{200,200}, // Fast blink
{50,450}, // Slow flash
{50,200} // Fast flash
};
const int blinkTimeout = 2000; // Blink for this long after button press
// These must survive across loop()s to keep track of states:
boolean buttonState[4]; // Is the button pressed?
boolean blinkState[4]; // Is the LED on?
unsigned long buttonTime[4]; // When was the button pressed?
void setup()
{
int i;
for (i = 0;i < 4;i++)
{
pinMode(outLedPins[i],OUTPUT);
pinMode(inButtonPins[i],INPUT);
buttonState[i] = 0;
blinkState[i] = 0;
}
}
void loop()
{
doButtonStuff();
doBlinkStuff();
}
void doButtonStuff()
{ /* This function checks the state of the buttons.
* When a button goes from not-pressed to pressed,
* it marks the time.
*/
int i;
boolean butnVal;
for (i = 0;i < 4;i++)
{ // Read button state
butnVal = digitalRead(inButtonPins[i]) == HIGH;
if (butnVal && !buttonState[i]) // button is pressed, was not pressed before, ..
buttonTime[i] = millis(); // .. so mark button time
// No need to debounce since we just turn something on that times out by itself
buttonState[i] = butnVal;
}
}
void doBlinkStuff()
{ /* This function checks whether it is time to turn a LED on/off,
* and if it is, it does so.
*/
int i;
unsigned long m;
boolean ledOn;
m = millis();
for (i = 0;i < 4;i++)
{ // Determine desired state: whether the LED should be on or off..
// Are we within 2 seconds of the last button press?
if (m >= buttonTime[i] && m < buttonTime[i] + blinkTimeout)
{ // Yes we are. -> Chek whether we are in the ON or OFF part of the blinking pattern
ledOn = (m - buttonTime[i]) % (blinkPattern[i][0] + blinkPattern[i][1]) < blinkPattern[i][0];
}
else
{ // No we are not. -> LED should be off.
ledOn = 0;
}
// If the desired state is not equal to the current state, change it
if (ledOn != blinkState[i])
{
digitalWrite(outLedPins[i],ledOn);
blinkState[i] = ledOn;
}
}
}
Xeyow:
I needed only the function that blinks the led. i don`t need 4 leds, 4 buttons... just that function
So write that function the way you think it should be, post it here, then we will help you fix it. We are definitely here to help you learn, but you only learn if you go through the processes yourself.