So, I'm quite new at Arduino coding, just started a few days ago... My friend gave a problem sheet about coding in Arduino IDE, one of the problems is:
"Write a program and upload 2 values to the serial monitor where the greater value is shown 15 times."
I made something that kinda works but I think that there is some better way to do it, if anyone got ideas or is able to do it, please tell me. Thnx
Here is my code
int x;
int y = 15;
bool stopLoop = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(stopLoop == false){
for(int x = 1; x <= y; x++) {
Serial.println(y);
delay(100);
}
stopLoop = true;
}
}
This code only works if x = 1 and y = 15 or if the number's difference is 14, so it that is why I think there probably way better ways to do this.
Your friend is a teacher who wants you to complete a programming assignment ?
He is indeed a friend with the same age, but he is an Arduino genius and he wants me to practice, he allows me to find help tho (researching, asking, tutorials, etc.)
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(stopLoop == false){
for(int x = 1; x <= y; x++) {
Serial.println(y);
delay(100);
}
stopLoop = true;
}
}
This code only works if x = 1 and y = 15 or if the number's difference is 14, so it that is why I think there probably way better ways to do this.
Surely you need 3 values? The 2 to choose between to display, and 15 for how many times to display?
I did it as below, with the whole thing in setup() so there's no need to force it to stop after once through loop(), although I applaud your use of the flag to do that.
byte a = 12;
byte b = 12;
byte theBiggerNumber;
void setup()
{
Serial.begin(9600);
Serial.print("The numbers are ");
Serial.print(a);
Serial.print(" and ");
Serial.print(b);
if (a > b)
{
theBiggerNumber = a;
}
else
{
theBiggerNumber = b; //or equal
}
Serial.println(" and here are 15 copies of the bigger (or equal) number:");
for (byte i = 0; i < 15; i++)
{
Serial.print(theBiggerNumber);
Serial.print(" ");
}
Serial.println(" ");
} //setup
void loop()
{
} //loop