...

C++Blocks
Code Block
languagecpp
themeConfluence
void setup()
{
  // initialize serial to a baud rate of 9600. 
  // Ensure that the same baud rate is set in the serial monitor.
  Serial.begin(9600); 
}

void loop ()
{
  Serial.print("Hands on ") ; 
  // does not insert a newline at the end; 
  // next message continues on the same line
  Serial.print("Learning ") ; 
  // does not insert a newline at the end; 
  // next message continues on the same line
  Serial.println("is Fun!!!") ; 
  // inserts a newline at the end; 
  // next message is printed on a new line
}]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:width="370"><ri:attachment ri:filename="image2020-1-21_21-43-17.png" /></ac:image></p><p>Note: Blocks allow only 9600 as baud rate; there is no baud rate setting.</p></div></td></tr><tr><td><p>Try the example above for the following two cases</p><p>(i) Serial.println for all three messages</p><p>(ii) Serial.print for all three messages</p></td><td><p>Try the example above for the following two cases</p><p>(i) with newline for all three messages</p><p>(ii)&nbsp;without newline for all three messages</p></td></tr></tbody></table><p>If you see strange-looking characters in the serial monitor, your bit / baud rate set in the Serial Monitor most likely does not match the one used in your Arduino sketch.</p><h3><strong>analogRead()&nbsp;</strong><strong>Serial Example</strong></h3><p><br /></p><table><colgroup><colh3><p>Printing to Serial console is a very effective mechanism in debugging your code. You can print out the values of variables at different points in your code, which can then be compared with the expected values. This might be of help in identifying logical issues with your code.</p><p>It is also useful in monitoring the readings from various sensors so that you can decide on the thresholds etc, i.e., to fine-tune your design / code. The following example prints out the reading obtained from the analog sensor connected to pin A0 (for example, an LDR connected to pin A0 as is the case in&nbsp;the&nbsp;<ac:link><ri:page ri:content-title="Analog Input" /></ac:link> page).</p><table class="wrapped"><colgroup><col /><col /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="831e9a20-21bc-43d0-8771-c29edaf2eb6a"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(analogRead(A0));
  delay(500); 
  // Read and print the value 2 times in a second.
}]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:width="400"><ri:attachment ri:filename="image2020-1-21_23-6-43.png" /></ac:image></p></div></td></tr></tbody></table><h3>Screencast</h3><p><ac:structured-macro ac:name="widget" ac:schema-version="1" ac:macro-id="b0b4fbf2-958e-4916-9c77-56cba00e3143"><ac:parameter ac:name="url"><ri:url ri:value="http://youtube.com/watch?v=q1DmNqlrY9U" /></ac:parameter></ac:structured-macro></p><p><br /></p><p><br /></p><p><br /></p>