Posts

Showing posts from 2015

Blinking LED on Lillypad Arduino

Image
1. Connect VCC,GND of FTDI board to Lilypad 2. Connect RX of FTDI Board to TX of Lilypad 3. Connect TX of FTDI Board to RX of Lilypad as per the below diagram 4.Open Arduino IDE. Files->Examples->Basic->Blink. 5. Select proper serial port and set Board to Lilypad Arduino (Atmega328p) 6. Burn the program. If you get the following errror: avrdude: stk500_recv(): programmer is not responding Press Reset Button while programmer is running.

How to run a command on boot in Raspberry Pi

Hi guys, If you want to run some service/script/command once the raspberry pi starts booting. You have to edit /etc/rc.local file Suppose if I want to start motion service after raspberry pi successfully booted. I will open rc.local file requires sudo sudo vim /etc/rc.local service motion start Now each time raspberry pi boots, it will start the motion service

Raspberry PI remote Web Server

Equipment Required: 1. Raspberry Pi 2. 8GB SD Card 3. Ethernet Card 4. Web Camera In order to make a raspberry pi remote web cam server, we have to install a package known as motion. Motion is a program that monitors the video signal from camera. It also able to detect whether a part of the picture has changed or not. First step is to download and install motion: sudo apt-get install motion Now we have to make some configuration changes Open motion.conf (/etc/motion/motion.conf) through nano or vim Make the changes for the following lines: daemon on webcam_localhost off webcam_maxrate 100 framerate 100 width 640 height 480 Then edit the motion file (/etc/default/motion) and change it to the following: start_motion_daemon=yes start the service sudo service motion start Now open it in browser: <ipaddr>:8081

Configuring DHCP and Static IP on Raspberry Pi Ethernet

In order to configure Ethernet DHCP on Raspberry Pi: Open /etc/network/interfaces through any text editor: and modify anything related to iface eth0 iface eth0 inet dhcp For getting a static IP Address on ethernet: iface eth0 inet static address 192.168.1.4 netmask 255.255.255.0 gateway 192.168.1.1 The above changes will assign an ip address of 192.168.1.4 to eth0 interface of Raspberry pi

Raspberry Pi: Error mounting: mount: /dev/sdb1: can't read superblock

If you face this error when you connect your memory card after performing dd operation. It means you have not un mounted the drive before performing the dd operation. So in order to solve this, you have to perform dd operation again , but before that sudo umount /dev/sdx This solved my problem

Sorting in Vectors C++

We will use the builtin function sort() to sort the vector. Header File: #include <algorithm> Synatax: sort(first, last); first -> first position in the sequence. last  -> last position in the sequence Sample Code: #include <iostream> #include <cstdlib> #include <ctime> #include <vector> #include <algorithm> using namespace std; void createVector(vector<int> &vect) {     srand(time(NULL));     for (int i = 0; i < 10; i++)         vect.push_back(rand()%100 + 1); } void displayVector(vector<int> vect) {     for (int i = 0 ; i < vect.size(); i++)     {         cout << vect[i] << endl;     } } int main() {     vector<int> int_vector;     createVector(int_vector);     displayVector(int_vector);     cout << endl;     sort(int_vector.begin(), int_vector.end());     displayVector(int_vector);     cout << endl;     return 0; }

Searching in a vectors

Sample Code : #include <iostream> #include <vector> #include <cstdlib> #include <ctime> using namespace std; void  createVect(vector<int> &vect) {     srand(time(NULL));     for(int i = 0; i < 100; i++)     {         vect.push_back(rand()%100 + 1);     } } int searchVect(vector<int> vect, int searchValue) {     int found = -1;     //Manual Search     for(int i=0;i<vect.size();i++)     {         if (vect[i]  == searchValue)                     return i;     }     //Using Vector function     found = vect.at(searchValue);     return found; } int main() {     vector<int> int_vect;     int num, found;     createVect(int_vect);     cout << "Enter the number to be searched"<< endl;     cin >> num;     found = searchVect(int_vect, num);     if (found == -1)         cout << num  << " is not found" << endl;     else         cout << num &

Generating Random Numbers in C++

To generate random numbers c++ provides a function rand(), which is present in the header file #include <cstdlib> rand() function returns an integer in the range 0 to RAND_MAX(2147483647). If you want the rand() function to generate random numbers only in the range between 0 to 10. you can write rand()%10 + 1 The problem with using only rand() function is that you will start observing the same sequence of numbers after execution. If you want to get a different sequence of numbers ,you have to perform a operation called seeding . The function which performs seed is srand() and takes integer as an argument. Keep in mind that you should call the srand() function only once.  A commonly used technique is to seed the random number generator using clock. The time() function will return the number of seconds elapsed since Jan1, 1970. You can pass this as an argument to srand(). Sample Code: #include <iostream> #include <cstdlib> #include <ctime> u

Introduction to vectors in C++

What is a vector? Vector in C++ is similar to arrays, You can say vectors are more than arrays. It performs dynamic array functionality with taking care of memory management(freeing,allocating) Vector combines the advantage of both static and dynamic arrays. It will automatically deletes the memory allocated for the vector like static array and like dynamic array you can have variable size, there is no need of specifying the size at the beginning. In order to use vectors in your programs, you have to include vector header file #include <vector> As vectors are present in std namespace, you have to declare using namespace std; To declare a vector of integers vector<int> var_name; If you have not specified using namespace std; std::vector<int> var_name; Now to check the size of the vector, you can use size() member function of the vector class. var_name.size(); Initially it will return 0. #include <iostream> #include <vector> u

Predicate Function in C++

Definition: Function that return a boolean(true or false) value is called predicate function. There are two variations of predication functions: 1. Unary Predicate : Function takes one argument and returns true or false 2. Binary Predicate : Function takes two arguments and returns true or false Lets take some example to understand this Code1: Check whether number is even or not #include <iostream> using namespace std; bool isEven(int number) {     return ((number%2) == 0); } int main() {     int num;     cout << "Enter a number";     cin >> num;     if (isEven(num))     {         cout << num << " is Even" << endl;     }     else{         cout << num << " is Odd" << endl;     }     return 0; } Code2: Check whether character is vowel or not #include <iostream> using namespace std; bool isEven(int number) {     return ((number%2) == 0); } bool isVowel(

Difference between Pointers and References in C++

1. You can assign a pointer variable multiple times, but the same cannot be done with a reference variable. It should be assigned only during initialization. Eg: Re Assigning Pointer variables can be possible       int num1 = 10;       int num2  = 20;       int *ptr = &num1;       ptr = &num2; Re Assigning Reference variables is not possible       int num1 = 10;       int num2 = 20;       int &ref;      ref = num2;  //Error 2. You have to use * operator to dereference a pointer variable, but there is no need of any operator to dereference a reference variable(Automatic Indirection), you can directly use it as a normal variable. 3. You can have multiple levels of indirections in pointers(i.e., pointer to pointer to pointer) but the same cannot be done with references. with references there is only one level of indirection.         int num1  = 10;         int *ptr = &num1;         int **ptr = &ptr;         int &refer = num1;         int

Reference Parameters in C++ Tutorial

With Reference parameters you are actually creating an alias(another name) to the existing variable. Use case where Reference Parameters are required: 1. Return More than one Value: A function can return only one value, what if you are having a situation where you function has to modify more than one value and return it to the caller. By using reference parameters you can modify more than one value in the function. What happens when a reference parameter is declared? When you pass a reference parameters , instead of copying the actual value, the memory address is copied into the formal parameter. How to differentiate between a simple parameter and a reference parameter? We use an ampersand(&) operator to indicate the parameter is a reference parameter.  Eg: int &a; //Reference parameter       int a; // Not a reference parameter Swap Example Using Reference Parameters: #include <iostream> using namespace std; void swap(int &

How do I replace tabs with spaces in Visual Studio

Image
If you have a requirement of adding spaces while pressing tabs in visual studio. Go to Tools->Options->Text Editor -> [Language] -> Tabs Select Insert spaces and click OK

Corflags tool Tutorial

Corflags tools is used to know whether your .exe or .dll is meant to run only on a specific platform or under WOW64. This tool will be very useful if you are planning to run the application on 32 and 64 bit machines. This tool is automatically installed with Visual Studio. Open Visual Studio Command Prompt. Syntax: corflags AssemblyName Eg: Corflags.exe AssemblyName.dll Some of the fields present in the output are: Version : .NET Version that the binary was built with CLR Header: 2.0 means either 1.1 or 1.1 while 2.5 means 2.0 PE &32 Bit: The platform this binary was compiled to run with: Any CPU: PE = PE32 and 32BIT = 0 X86 : PE = PE32 and 32BIT = 1 x64: PE = PE32+ and 32BIT = 0 ILONLY: If the assembly contains unmanaged code the value is 0 else 1. Signed: Whether this assembly was signed using a stronger key. References: 1. http://blogs.msdn.com/b/gauravseth/archive/2006/03/07/545104.aspx

Diskpart Utility Tutorial

Diskpart:  Diskpart utility is a command line utility that allows you to manage and configure your hard disks, volumes, or partitions How to format the pendrive using diskpart utility if it is not getting listed in the Windows Explorer? Steps to be followed Open command prompt in administrator mode type "diskpart" type "list disk" which displays all disks present on the computer type "select disk 1", if you want to the select the second disk, you can choose any number type "clean", which will remove all the partition or volume formatting from the disk with focus type "create partition primary", which creates a primary partition on the current basic disk. after you create the partition, the focus automatically shifts to the new partition type "active", which marks the partition as active type "format FS=NTFS LABEL="ANY NAME" QUICK", performs a quick format type "assign" which

MFC Tutorial Part 1

Simple.h : #include <afxwin.h> class CSimpleApp:public CWinApp { public: BOOL InitInstance(void); }; Simple.cpp : #include "Simple.h" BOOL CSimpleApp::InitInstance(void) { return TRUE; } CSimpleApp Application;

Matrix Class C++ Example

Define matrix class with dynamic memory allocation for 2-D matrix. Include constructors, destructor, overload + (m1+m2, m1+3 & 4+m2), -, << and >> operators. Also overload new and delete operators by using malloc & free functions internally #include<iostream> using namespace std; #include<cstdlib> #include<new> class matrix { int **array; int row; int column; public: //default constructor matrix() { array=NULL; row=0; column=0; } //single parameter constructor(square matrix) matrix(int length) { int i,j; array=(int **)new int[length]; row=length; column=length; for(i=0;i<length;i++) array[i]=new int[length]; for(i=0;i<length;i++) for(j=0;j<length;j++) array[i][j]=0; } //double parameterized constructor(rectangular matrix) matrix(int r,int c) { int i,j; row=r; column=c; array=(int **)new int[r]; for(i=0;

Shallow Copy and Deep Copy Example

Define string class with dynamic memory allocation for string. Define default constructor, parameterized constructors, copy constructor, destructor, Overload +, [], =, <<, >> operators. Observe the behavior of shallow copying and deep copying. #include<iostream> using namespace std; #include<string.h> class cstring { char *sptr; int length; public: cstring() { sptr=NULL; length=0; } cstring(const char *s) { sptr=new char[strlen(s)+1]; strcpy(sptr,s); length=strlen(s); } cstring(const cstring &s) { sptr=new char[s.length+1]; strcpy(sptr,s.sptr); length=s.length; } void print_cstring() { cout<<sptr<<endl; } void scan_cstring() { sptr=new char[10]; cin>>sptr; } ~cstring() { if(sptr!=NULL) delete []sptr; } cstring operator=(cstring c) { delete []sptr; sptr=new char[strlen(c.sptr)+1]; strcpy(sptr,c.sptr); length=c.length; retu