Monday, 8 October 2018

Arduino - II

After the first post on Arduino, we will look at commonly used data types and the serial monitor output of these data types on Arduino in this segment. For all the work in this post, we will use the same Arduino Uno and Arduino IDE used in the last post.

In this post, we will take a look at how data generated by Arduino board can be seen on the console. This is important because apart from viewing the data generated by any sensor, a developer can use this feature for debugging the code. We use a very simple program shown below:

// data types

int int_a = 10, int_b = 25;
boolean boolean_c = true, boolean_d = 0;
char char_e = 'a', char_f = 97;
unsigned char unsigned_char_g = 255;
unsigned int unsigned_int_h = 65535;
word word_i = 65535;
long long_j = 2147483647L;
unsigned long unsigned_long_k = 4294967295;
float float_l = 1.1234;
double double_m = 1.1234;
char string_n[7] = "arduino";     // string as array of char
int array_int_o[] = {2, 4, 8, 3, 6}; // array of int
enum enum_p {uno,mega};
enum_p type = uno;
String string_q = "Hello Arduino"; //String object

void setup() {
  Serial.begin(9600); // opens serial port and sets data rate to 9600 bits per second
  Serial.println(int_a);
  Serial.println(int_b);
  Serial.println(int_b,BIN);
  Serial.println(int_b,OCT);
  Serial.println(int_b,DEC);
  Serial.println(int_b,HEX);
  Serial.println(boolean_c);
  Serial.println(boolean_d);
  Serial.println(char_e);
  Serial.println(char_f);
  Serial.println(unsigned_char_g);
  Serial.println(unsigned_int_h);
  Serial.println(word_i);
  Serial.println(long_j);
  Serial.println(unsigned_long_k);
  Serial.println(float_l);
  Serial.println(float_l,1);
  Serial.println(float_l,2);
  Serial.println(float_l,3);
  Serial.println(float_l,4);
  Serial.println(double_m);
  Serial.println(double_m,1);
  Serial.println(double_m,2);
  Serial.println(double_m,3);
  Serial.println(double_m,4);
  Serial.println(string_n);
  Serial.println(array_int_o[1]);
  Serial.println(type);
  Serial.println(string_q);
}

void loop() {
  // put your main code here, to run repeatedly:

}

For integers, we can specify the base as an optional parameter. For float and double, we can specify number of digits to be shown after the decimal as an optional parameter. Once the above code is compiled and uploaded, we get the confirmation that the code has been uploaded to Arduino board:













We can access the serial monitor either by navigating from Tools menu or by clicking the Serial Monitor icon on right top as shown below:









Once the Serial Monitor comes up, we see below output:


















Let us now juxtapose the results against the println commands to validate the outputs:

























Note that booleans are printed as integers and that floats and decimals are shown with two decimals by default. String is not really a data type but an object in Arduino. In the program we have set the baud rate to 9600 and Serial Monitor showed the output at this baud rate only. Let us now set the baud rate to 4800 at the right bottom in the Serial Monitor. We get some strange characters in Serial Monitor:













Setting baud rate to 19200 shows below output in Serial Monitor:













Setting baud rate back to 9600 shows the correct output that we got earlier. With this, we conclude the second post on Arduino

Friday, 5 October 2018

Arduino - I

In the first post on Internet of Things, we explore Arduino. Arduino is an inexpensive, platform independent, open-source electronics platform based on easy-to-use extensible hardware and extensible software. More details on Arduino are here. The language reference for coding in Arduino can be seen here. Arduino IDE is used for coding and the details are here. One can either use the online version or the local version. Link to the online version is here. The binaries for local installation are here. One can download the software corresponding to their OS: Windows, Mac OS X or Linux.

For the all the work in this post, we will be using the very elementary Arduino Uno. Details about Arduino Uno are here. To learn more about Arduino anatomy, one can see here. We will be using USB to power this board. For the IDE, we will use a local install on Windows. We have used this installer. The installation is very straight forward. For any help with installation, one can refer this link. The version is of IDE is shown below:

















At this point, we will assume that we are all set up with the board and the IDE. We enter code as shown in IDE below:




















To support running of the above code, we need a green LED, a red LED, a USB cable, connecting wires, a bread board, and an Arduino Uno board. The corresponding circuit is shown below.




















The circuit itself is elementary. Once we connect the USB cable to the laptop, we can set the board to Arduino Uno as shown below:

























We then set the port as shown below:















Once these two settings are done, we can save the code to a folder and file by navigating File -> Save As... and entering Signal in the window as shown below:






















This file is now saved as Signal.ino under Signal.ino folder. We can now compile this program as shown below:

















Once we compile, we can see the output Done compiling as shown below:










We can then upload the program to the Arduino board by navigating Sketch -> Upload or clicking the -> button just below Edit Menu as shown below:
















Once the code is uploaded, we can see the output as Done uploading as shown below:










We can now see the red and green LEDs light up alternately and not together simulating a traffic signal:






































This concludes our first post on Arduino

Monday, 17 September 2018

Solidity - II

After the first post on Solidity in Remix, we look at the basic types that are encountered while programming in Solidity. We will be using the online Remix IDE for all the work in this post.

Let us see the program below that shows us the default values of the basic data types:

pragma solidity ^0.4.25;

contract Types {
        bool Boolean;
        int SignedInteger;
        uint UnsignedInteger;
        address Address;
        byte Byte;
        string String;
        function getDefaultValues() public view returns (bool, int, uint, address, byte, string) {
    return (Boolean, SignedInteger, UnsignedInteger, Address, Byte, String);
        }
}

The function defaultValues() is a getter function. view is used bool is be used to define boolean variables in Solidity. int stands for signed integers of 256 bits. We can define int of 8 bits as int8 and then multiples of 8 like int16, int24 and so on till int256. uint represents unsigned integers and is of 256 bits. unit can also be represented in the same way as int: uint8, unit16, uint24, ..., unit256. address type defines an Ethereum address and is of 20 bytes. byte is byte array and is equal to bytes1. Like int, it also variants like bytes2, bytes3, and so on till bytes32. string type defines strings. Compiling the above contract in Remix gives the below result:













Running and deploying we see below result:













Note that there is no output for string data type. Now let us do a few operations on the default values:

pragma solidity ^0.4.25;

contract Types {
        bool Boolean;
        int SignedInteger;
        uint UnsignedInteger;
        address Address;
        byte Byte;
        string String;
        function changedValues() public view returns (bool,int,uint,address,byte,string) {
    return (!Boolean, SignedInteger-1, UnsignedInteger+2, Address, Byte|0x10, String);
        }
}

Running above program shows below result:


We can compare the results with the results of the earlier run and see that values of 4 variables have changed. In the last example, we show the classic overflow and underflow examples in int8:

pragma solidity ^0.4.25;

contract IntTypes {
        int8 i=1;
        int8 j=2;
        int8 k=127;
        int8 l=-128;
        int8 m=3;
        function setInt() public {
            i++;
            j--;
            k++;
            l--;
            return;
        }
        function getInt() public view returns (int8,int8,int8,int8,int8) {
        return (i, j, k, l, m++);
        }
}

Let us run the above program. When we try to deploy the program, we get two buttons called setInt and GetInt in line with the function names as shown below:











Now, click on getInt button to see default values as shown below:













Now that i, j, k, and l are in line with the values that we have set when we defined these variables. But, the ++ increment operator fails on m as it does not increment m and returns the same value that is set when it is defined. Now, click on setInt button and then getInt again to see the results below:












Note that i is incremented from 1 to 2, j is decremented from 2 to 1, k overflows and is reset to -128, l underflows and is reset to 127. The overflow and underflow occur as the range of values that int8 can take are from -128 to 127. Note again that m remains the same and is not incremented. While ++ increment operator or -- decrement operator work well within the body of the function, they do not operate on the variable in the return clause.

This concludes the second post on Solidity