Digital

Digital Signals

A digital signal is represented by a binary value. Either 1 or 0, HIGH or LOW, ON or OFF. This makes digital signals very easy to analyze and use, but it also makes them less accurate than an analog signal, which can read multiple values of data. One advantage of a digital signal is they do not produce noise…it’s either 1 or 0, nothing in between.


One way to understand the difference between analog and digital signals is a light switch. A standard on/off switch would be an example of how a digital signal works. The switch itself isn’t digital, but the on/off state it represents is. While a dimmer switch, which adjusts the level of the light, is an example of how an analog signal works.

Digital()

The Digital() function takes 1 argument, which is what pin is being used.

Digital(pin)

Let’s look at an example of how a digital signal is used inside code by turning on an LED. First, we need to create the LED object using the Digital() function. The word led or LED is a special word that accesses the onboard LED when called inside the Digital() function

var led = Digital(LED);
led = Digital(LED)

Output

We’ve created the led object in code by using var, but now we need to send it to the Out() function to actually turn it on and run the code. We do this by putting our led object as the first value, which is an argument, inside the Out() function. We need to add a second argument to turn it ON. Remember this is digital, so we use the number 1 to represent ON. This is referred to as setting the pin HIGH.

var led = Digital(LED);
Out(led,1);
led = Digital(LED)
Out(led,1)

We can now run the code and the LED on the BrainPad will turn ON.  Great!  Now let’s go one step further and make it blink! To make the LED blink, we’re going to add a few more lines to our code. We need to create a way to repeat a section of code over and over again without having to type the same code multiple times. We do this by creating an endless loop. This means after the code reaches the end, it will go back to the top of the selected code section and run it again and again…FOREVER.  This is done by using the while() method, which is also known as a while-loop. The only value we add inside the while() function we’ll be using is the word true. This tells the program to execute the code inside the loop while true, basically looping FOREVER. We also should indent the code inside the while() function to make the code more readable so we can see all code inside the loop.

var led = Digital(LED);

while(true){
    Out(led,1);
}
led = Digital(LED)
 
while True:
    Out(led,1)

Even though we’ve added all this extra code, nothing is different from the first section of code we created. If you run the code, the exact same thing will happen. The LED will turn on and stay on. We need to tell the LED to turn off in our code. Since were dealing with a digital signal, and 1 is ON. Can you guess what number OFF might be?

If you said 0, then you are correct. We need to add a second Out() function that tells the LED to turn OFF.

var led = Digital(LED);

while(true){
    Out(led,1);
    Out(led,0);
}
led = Digital(LED)

while True:
    Out(led,1)
    Out(led,0)

If you run the above code, what do you think will happen? Now the LED NEVER turns ON! Well, it does turn on and off so fast you can’t actually see it. This is because computers run our code VERY FAST. We need to add a pause in between turning the LED ON and OFF.  We’ll use the Wait() function and tell the BrainPad to wait for 1 second, before turning the LED OFF. We also need to add a Wait() function after the LED turns OFF too, because remember the code will loop and so we would never see the LED  turn OFF.

var led = Digital(LED);

while(true){
    Out(led,1);
    Wait(1);
    Out(led,0);
    Wait(1);
}
led = Digital(LED)

while True:
    Out(led,1) 
    Wait(1)
    Out(led,0) 
    Wait(1)

We now have a complete blinking LED program using the BrainPad’s Digital() function as an output using the Out() function.

Input

Just like we control a pin state, we can also read a pin’s state. This pin can be connected to a switch, for example, and we would need to know if the switch is on or off. Another example is motion sensor, where a pin on the sensor will be activated when it detects motion.

var motion = Digital(P1);

while(true){
    if(In(motion) == 1);
    Print("ALARM");
    Wait(0.1);
}
motion = Digital(P1)

while True:
    if(In(motion) == 1)
        Print("ALARM")
    Wait(0.1)

BrainStorm

Do you have any idea why we create the led object before we created the while() loop? This is because we only need to create the led object once in our code when it runs the first time. We then want to repeat everything inside the while() loop. What happens if we turn OFF(0) the led first, and then turn it ON(1)? Try changing the value inside one of the Wait() methods and see what happens.