Skip to main content

Tired of Repeated Recurring Arduino Serial Monitor Values? Try this-

The Serial monitor in the Arduino IDE is essential for simulating outputs and diagnosing problems in the circuit. However, depending on what your project does, the Serial monitor, not only helps you, but can also potentially drive you crazy with constant bombardment of serial values like this-





This makes it hard for you to extract the relevant values you're looking for.
Luckily, there's a simple solution to the problem. A minor change in code can get it to work perfectly. 
NOW, if you simple want to just slow down the fast updating Serial Monitor, you can simply add a small delay after each print.
  Serial.println(outputValue);

  delay(100);\

Definitely, that's not the way to go about important values that you are dealing with.
So here's how you transform your code, to a serial friendly code.

Let's say you're reading values from an LDR using an analog pin as an INPUT pin and simultaneous printing them on the Serial monitor using a code like this-
void loop() {
// Variable declaration etc etc.....
Serial.println(analogRead(LDRPin));
// The remaining stuff..
}
Now if you wish to see the serial values only if there is a change observed in the consecutive value, You'll have to write something like this-
int lastValue,newValue;
int threshold=200; // The minimum change you're looking for, 

void loop() {
// Variable declarations, etc etc
newValue=analogRead(resistorPin);
if (newValue-lastValue > threshold) {
    Serial.print("Value increased to ");
    Serial.println(newValue);
    lastValue=newValue;
    };
if (lastValue-newValue < threshold) {
    Serial.print("Value decreased to ");
    Serial.println(newValue);
    lastValue=newValue;
    };
// remaining code...
}
Note that in the above code any consecutive values that differ by less than 200 won't be printed! This value can be adjusted by changing  threshold to the required value.
The basic idea of the code snippet is that we are using two variables, one that stores the present value of the pin and the other, that stores the previous/last value of the pin.by measuring the difference between the two, you can understand whether the pin has undergone any significant change of value.
This is just perfect if you're using an LDR, to detect the lighting conditions of the room. Also notice that after each iteration, we update lastValue to newValue making the code reusable again for the next loop iteration!




Comments

Post a comment

Popular posts from this blog

Distortion Pedal Electronics (Explained for beginners!)

If you are into DIY guitar pedals and want to start designing your own effects, this article might help you open a few doors. I write this article as an EE student who earlier struggled with understanding these circuits and would often simply copy schematics off the internet. 

This article is intended for-
1. Absolute beginners who like tinkering with electronics
2. Anyone who has been learning analog circuits and is looking for a jumpstart project.

Also before I start I'd like to mention, for anyone who wants to get a rock-solid foundation in Analog electronics, I highly recommend reading the book, 'Electronic Principles' by AP Malvino and David Bates.

Distortion Pedals come in all flavors and sizes, however, how the distortion effect is achieved in any of the pedals is more or less similar. Let's first see how the output of the pedal compares to its input.

The raw signal coming from the guitar is first amplified a little, and then the peaks of the amplified signal are clip…

5 Effective Online Tools that will Spice up your Arduino Projects!

Arduino has made electronics projects easier than ever. The question now is- Can we make the process of building projects faster and more enjoyable? Let's have a look at some of the interesting softwares/online tools developed for the Maker Community to make tinkering more effective and documenting projects easier!

1. TinkerCad

TinkerCAD's recently created circuit simulator allows you to arrange your Arduino/Attiny circuits in an easy 'drag and drop' environment and test them virtually without having to make them. This can be really helpful, for testing projects that use components which you are yet to buy. And after all, who doesn't like tinkering?


Try out Tinkercad-Circuits here!

But that's not what Tinkercad is famous for. It offers an amazingly easy to use 3D design tool for Makers, once again in a simple 'Drag and Drop' interface! You can directly download the CAD file, once you're done designing. If you've ever thought of making your own uniqu…

TV Remote Controlled Servo Motor-Using Arduino!

Greetings!
We use our TV remotes to control the functions of the TV's, but when the TV is not in use, they're absolutely useless. Today, we are going to learn how to add more functionality to ordinary IR based remotes, in this one we shall see how to control servo motors. There are so many (SO MANY) different applications of this project that one can think of. Using the following tutorial, you can make one yourself!

1. Watch the video tutorial-


2. Things you will need-

Note that you may use a different IR receiver other than TSOP1738, if it isn't available. Usually you can find this on websites like amazon and ebay.
3. The circuit diagram!

4. The IR Remote Library http://z3t0.github.io/Arduino-IRremote/
5. The Arduino Code- Just copy paste this code into your Arduino IDE

// Written by Siddharth Kothari, curiousmotor.com// Enjoy your journey Exploring Arduino!#include <IRremote.h>#include <IRremoteInt.h>#include <Servo.h>intRECV_PIN=11;//IR receiver pinintgndIR=10;/…