I am trying to write a code for the built in led to blink when I push button 1. Its supposed to blink based on a factorial so 1 is 1 blink 2 is 2, 3 is 6, 4 is 24. My code compiles but when I click the push button once the Led just stays on and never turns off. What am i missing?
int button1 = 2;
int button2 = 4;
int buzzer = 9;
int count = 0;
void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buzzer, OUTPUT);
}
// return factorial of number
int factorial(int n) {
int fact = 1;
int i=0;
for (int i = 1; i<factorial(count); i++); {
fact *= i;
}
return fact;
}
void loop() {
// button 1 pressed?
if (digitalRead(button1) == HIGH) {
// increment count
count++;
// turn led on
digitalWrite(LED_BUILTIN, HIGH);
// delay according to factorial of count
delay(factorial(count));
// turn led off
digitalWrite(LED_BUILTIN, LOW);
// buzzer sound
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
// button 2 pressed?
}
if (digitalRead(button2) == HIGH) {
// reset count
count = 0;
}
}
The testing you should have done on the smaller elements of your program before you put them together into a bigger sketch.
Did you test the factorial function independently to confirm that it works?
Did you write a program to test the buttons to confirm that they work?
Did you want that part of the program to continue after you release the button switch? You don't allow that to happen, you just test the button state. When both buttons released, nothing in loop() does anything.
I will give you a good start with a sketch that calculates the factorial and uses the Serial Monitor to show them.
// Factorials
// For: https://forum.arduino.cc/t/need-help-with-factorial/1036677
// See: https://en.wikipedia.org/wiki/Factorial
// Wokwi project: https://wokwi.com/projects/344102020214424146
unsigned long count = 0;
void setup()
{
Serial.begin(9600);
Serial.println("Calculate factorials.");
for( int count = 0; count < 13; count++) // 12 is the highest possible.
{
Serial.print( count);
Serial.print( "! = ");
Serial.print( factorial(count) );
Serial.println();
}
Serial.println("Reached the end.");
}
void loop() {} // empty loop()
// return factorial of number
unsigned long factorial(int n)
{
unsigned long fact = 1;
for (int i = 1; i <= n; i++)
{
fact *= (unsigned long) i;
}
return fact;
}
This sketch in Wokwi:
How do want to select the number 2 ? By pressing the button two times or by keeping it pressed for two seconds or do you have a button for each number ?
Make a logical setup like:
Install Button-1 which when pressed, the L (built-in LED) will blink for one time (1! = 1).
Install Button-2 which when pressed, the L will blink for two times (2! = 2x1 = 2).
Install Button-3 which when pressed, the L will blink for 6 times (3! = 3x2x1 = 6).
You might encounter Button bouncing issues which could be handled later using Debounce.h Library.
So I have two push buttons. The first push button each time I push it should increase blinks by factorial amount. so 1 push = 1 blink 2 push = 2 blinks 3 = 6 blinks. When Push button 2 is pressed it should reset back to zero.
Your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem
Start with simple case with three buttons for 1!, 2!, and 3! (Fig-1). After that use single button to enter the input number in the form of number of presses of the button.
Im very new to arduiono. So I have a code with two pushbuttons. The led on my arduino is supposed to blink with a factorial based on the number of pushes for pushbutton 1. For example 1 push will be 1 blink and 3 pushes will be 6 blinks. My issue is I cannot get the counter to work. I click the button and its always blinking the factorial of 1. Any ideas what I have wrong?
const int pushbutton1 = 2;
int pushbutton2 = 4;
int buzzer = 9;
int button1state = 0;
int button2state = 0;
int count = 0;
void setup() {
pinMode(pushbutton1, INPUT);
pinMode(pushbutton2, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
button1state = digitalRead(pushbutton1);
button2state = digitalRead(pushbutton2);
if (button1state == HIGH) {
count++;
delay(25);
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
delay(100);
}
for (int i = 0; i < factorial (count); i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
if (button2state == HIGH) {
count = 0;
}
}
}
int factorial(int n) {
if (n == 1) return 1;
return n * factorial(n - 1);
}
Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting can result in a suspension from the forum.
In the future, please only create one topic for each distinct subject matter.
This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
You have come to the right place, we are all Arduino-enthusiasts.
We think that for a beginner it is important to use the Serial Monitor.
Can you make another sketch that tests your new factorial() function and shows it on the Serial Monitor.
We see all the time that a beginner rushes towards the end-result. But a advanced Arduino user does it step by step. No worries, you learn along the way.
Can you make a small sketch and prove to use that your new factorial() function works ?
This factorial function is wrong. It is recursive, meaning that it calls itself. It is possible to write a factorial function using recursion, but I would not recommend it on an Arduino. It is possible to write a factorial function using a for-loop. Your code seems to be using both a loop and recursion and so will not produce the correct result, I think.
Also, it uses the global variable count and does not use its own parameter n.
EDIT: see that in your cross-post, you completely changed the factorial function:
int factorial(int n) {
if (n == 1) return 1;
return n * factorial(n - 1);
}
This will produce the correct result, I think, but uses the recursive method I would not recommend on Arduino, at least not basic Arduino like Uno or Nano which have very limited RAM memory.
But even if you made a factorial function using a for-loop, it would still be very inefficient. It could be much more efficient because it calculates the factorial of count from scratch each time count is increased. It could retain the previous factorial result and calculate the updated factorial from that. For example suppose that count is 3 and the factorial had been calculated, which is 6. Then count is increased to 4. Instead of recalculating the factorial of 4 from scratch, it could more efficiently be calculated from the previous factorial, 12, and the new value of count, 4. Like this: 6 * 4 = 24 which is the factorial of 4.
In practice, this more efficient method won't make any noticeable difference in code which is full of delay() and needs to flash an led at a speed a human can count, but I thought it was interesting and could be important to you one day in the future.
1. Fig-1 is the hardware setup based on your description.
Figure-1:
2. Take a 5-sec time slot during which you are supposed to press the pushbutton1 for n (say: 1, 2, 3, 4, or 5) times and will be indicated by LED1 at DPin-12. Also, necessary information/message will appear on Serial Monitor.
3. Upload the following sketch; observe the result and come back with queries if any.
#include <Debounce.h>
#define pushbutton1 2
#define LED1 12
#define L LED_BUILTIN
Debounce Button(pushbutton1);
unsigned long prMillis = 0;
byte pressCount = 0;
void setup()
{
Serial.begin(9600);
pinMode(pushbutton1, INPUT_PULLUP);
pinMode(L, OUTPUT);
pinMode(LED1, OUTPUT);
}
void loop()
{
while (millis() - prMillis < 5000) //5-sec time slot
{
digitalWrite(LED1, Button.read()); // Read the button state and write it to the Led.
pressCount = Button.count(); //times button1 is pressed
}
Serial.println("5-sec is over.");
Serial.println(pressCount);
int facValue = factorial(pressCount);
//---------------------------------
Serial.print("Button Pressed ");
Serial.print(pressCount); Serial.print(" times: ");
Serial.print("Factorial: "); Serial.println(facValue);
//--------------------------------------
Button.resetCount();
pressCount = 0;
prMillis = millis();
}
int factorial(byte n)
{
if (n == 1)
{
return 1;
}
return (n * factorial(n - 1));
}