Serial.begin()

Hello, I understand that Serial.begin() specifies how many bits per second of data will be sent in communication with your computer and arduino, but why is this not essential for my code?

What code would that be ?

I’m just asking in general, but here is my code:

#include <IRremote.h>

int IRpin = 11; // pin for the IR sensor
IRrecv irrecv(IRpin);
decode_results results;
int First = 8;
int Second = 13;
int Third = 5;
int Fourth = 7;
int Fifth = 2;
int Sixth = 4;

void setup()
{

pinMode(First, OUTPUT);
pinMode(Second, OUTPUT);
pinMode(Third, OUTPUT);
pinMode(Fourth, OUTPUT);
pinMode(Fifth, OUTPUT);
pinMode(Sixth, OUTPUT);
irrecv.enableIRIn();
Serial.begin(9600);
}

void loop() {

if (irrecv.decode(&results)) {
Serial.println(results.value);

irrecv.resume();

//-----------------------------------------------------------------------------------------------------------------------------

if(results.value ==16724175){
digitalWrite(First, HIGH);
}

if(results.value== 16718055)
{
digitalWrite(Second,HIGH);
}

if(results.value ==16743045){
digitalWrite(Third, HIGH);
}

if(results.value ==16726215){
digitalWrite(Fifth,HIGH);
}

if(results.value ==16716015){
digitalWrite(Fourth, HIGH);
}

if(results.value ==16734885){
digitalWrite(Sixth, HIGH);
}

if(results.value ==16728765){

digitalWrite(First, HIGH);
delay(60);
digitalWrite(Second, HIGH);
delay(60);
digitalWrite(Third,HIGH);
delay(60);
digitalWrite(Fourth, HIGH);
delay(60);
digitalWrite(Fifth,HIGH);
delay(60);
digitalWrite(Sixth, HIGH);
delay(60);

digitalWrite(Fifth, LOW);
digitalWrite(Fourth, LOW);
digitalWrite(Third, LOW);
digitalWrite(Second, LOW);
digitalWrite(First, LOW);
delay(60);

digitalWrite(Fifth, HIGH);
delay(60);
digitalWrite(Fourth, HIGH);
delay(60);
digitalWrite(Third, HIGH);
delay(60);
digitalWrite(Second, HIGH);
delay(60);
digitalWrite(First, HIGH);
delay(60);

digitalWrite(Fifth, LOW);
digitalWrite(Fourth, LOW);
digitalWrite(Third, LOW);
digitalWrite(Second, LOW);
digitalWrite(Sixth, LOW);
delay(60);

digitalWrite(Second, HIGH);
delay(60);
digitalWrite(Third,HIGH);
delay(60);
digitalWrite(Fourth, HIGH);
delay(60);
digitalWrite(Fifth,HIGH);
delay(60);
digitalWrite(Sixth, HIGH);
delay(60);

digitalWrite(Fifth, LOW);
digitalWrite(Fourth, LOW);
digitalWrite(Third, LOW);
digitalWrite(Second, LOW);
digitalWrite(First, LOW);
delay(60);
digitalWrite(Fifth, HIGH);
delay(60);
digitalWrite(Fourth, HIGH);
delay(60);
digitalWrite(Third, HIGH);
delay(60);
digitalWrite(Second, HIGH);
delay(60);
digitalWrite(First, HIGH);
delay(60);
digitalWrite(Fifth, LOW);
digitalWrite(Fourth, LOW);
digitalWrite(Third, LOW);
digitalWrite(Second, LOW);
digitalWrite(First, LOW);
digitalWrite(Sixth, LOW);
digitalWrite(First, HIGH);
delay(60);
digitalWrite(Second, HIGH);
delay(60);
digitalWrite(Third,HIGH);
delay(60);
digitalWrite(Fourth, HIGH);
delay(60);
digitalWrite(Fifth,HIGH);
delay(60);
digitalWrite(Sixth, HIGH);
delay(60);
digitalWrite(Fifth, LOW);
digitalWrite(Fourth, LOW);
digitalWrite(Third, LOW);
digitalWrite(Second, LOW);
digitalWrite(First, LOW);
delay(60);
digitalWrite(Fifth, HIGH);
delay(60);
digitalWrite(Fourth, HIGH);
delay(60);
digitalWrite(Third, HIGH);
delay(60);
digitalWrite(Second, HIGH);
delay(60);
digitalWrite(First, HIGH);
delay(60);
digitalWrite(Fifth, LOW);
digitalWrite(Fourth, LOW);
digitalWrite(Third, LOW);
digitalWrite(Second, LOW);
digitalWrite(First, LOW);
digitalWrite(Sixth, LOW);
digitalWrite(First, HIGH);
delay(60);
digitalWrite(Second, HIGH);
delay(60);
digitalWrite(Third,HIGH);
delay(60);
digitalWrite(Fourth, HIGH);
delay(60);
digitalWrite(Fifth,HIGH);
delay(60);
digitalWrite(Sixth, HIGH);
delay(60);

digitalWrite(Fifth, LOW);
digitalWrite(Fourth, LOW);
digitalWrite(Third, LOW);
digitalWrite(Second, LOW);
digitalWrite(First, LOW);

delay(60);
digitalWrite(Sixth, HIGH);
delay(60);
digitalWrite(Fifth, HIGH);
delay(60);
digitalWrite(Fourth, HIGH);
delay(60);
digitalWrite(Third, HIGH);
delay(60);
digitalWrite(Second, HIGH);
delay(60);
digitalWrite(First, HIGH);
delay(60);

}

if(results.value ==16730805){
digitalWrite(8, HIGH);
digitalWrite(13, HIGH);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
digitalWrite(5, HIGH);
}

else if(results.value == 16753245) // Your OFF button value
{
digitalWrite(13,LOW);
digitalWrite(8,LOW);
digitalWrite(7, LOW);
digitalWrite(2,LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
}
}

Whether I take out Serial.begin(9600); or not my code still runs. I’m asking why this isn’t an essential statement.

The program will work to turn the LEDs on/off without the Serial.begin() but it will not print the received value on the Serial monitor. That is the only way in which the program communicates with the PC. The upload process does not depend on the Serial.begin()

@OP

(1) If you do not include Serial.begin(9600); instruction in your sketch, you will not be able to show any result on the Serial Monitor.

(2) If you include this partial instruction Serial.begin(); (without argument), the sketch is not compiled.

(3) In your posted codes, there is a Serial.println() instruction; how can you say that this instruction works without the inclusion of Serial.begin(9600);? In my case, it does not work.

In my case, it does not work./quote]
That depends on your definition of "works"

Of you do not want to see the Serial output (possibly originally put there for debugging) then its works to control the LEDs

UKHeliBob:
That depends on your definition of "works"

GolamMostafa:
In your posted codes, there is a Serial.println() instruction; how can you say that this instruction works without the inclusion of Serial.begin(9600);? In my case, it does not work.

It has referred to Serial.println(); function which has not worked without having Serial.begin(9600); instruction in my sketch. I think that the main verb (work) has already been exhausted; there is no need to define it further.