Thursday, 5 November 2015

Processing GUI Button

Code:
import controlP5.*;
ControlP5 controlP5;

int buttonValue = 0;
void setup() {
size(400,400);
controlP5 = new ControlP5(this);
controlP5.addButton("White").setValue(255).setPosition(100,100).setSize(200,19);
controlP5.addButton("Black").setValue(0).setPosition(100,120).setSize(200,19);
}
void draw() {
background(buttonValue);
}

void controlEvent(ControlEvent event) {
  println("the Event Triggered is");
println(event.getName());
}



public void White( ){
println("white button");
buttonValue = 255;
}
public void Black(){
println("black button");
buttonValue = 0;
}

Output:


Thursday, 29 October 2015

Simple Java Addition Program

Simple Java Addition Program 

package ex1;

import javax.swing.JOptionPane;

public class Addition {

public static void main(String[] args)
{
JOptionPane.showMessageDialog(null,"This is a Simple Addition Example \n Click OK to Continue");

String a_value = JOptionPane.showInputDialog("Enter the Value of A");
String b_value = JOptionPane.showInputDialog("Enter the value of B");
//converting String to Integer
int a = Integer.parseInt(a_value);
int b = Integer.parseInt(b_value);

int result = a+b;

//Converting Integer to String
String result_val = "" + result;

JOptionPane.showMessageDialog(null, "the value of A + B is \n" + result_val +"\n Thankyou");

}

}


Output..


Tuesday, 25 August 2015

converting True color RGB image into grayscale Image

In matlab there is a function rgb2gray() to convert rgb image into gray scale image.
we can do this manually also.


  1. rgb = imread('Sun.jpg');
  2. figure('name','Original Image')
  3. imshow(rgb);

  4. gray_manual = 0.2989 * rgb(:,:,1) + 0.5870 * rgb(:,:,2) + 0.1140 * rgb(:,:,3);
  5. figure
  6. imshow(gray_manual);

Output
Original Image

Converted GrayScale Image
Formula to do this is
Intensity = 0.2980 * red + 0.5870 * green + 0.1140 * blue


Matlab Image Processing

I am trying to do learn image processing using matlab..
I find tutorials here http://www.bogotobogo.com/Matlab/Matlab_Tutorial_Digital_Image_Processing_2_RGB_Indexed_Color_Info.php

Below program explains converting true color rgb image into index image and show the difference between mapping the index image into various levels.

code
  1. image = imread('Sun.jpg');
  2. figure('name','Original Image')
  3. imshow(image);

  4. [IND,map] = rgb2ind(image,32);
  5. figure('name','image with 32  index')
  6. imshow(IND),colormap(map);

  7. [ind_132,map_132] = rgb2ind(image,132);
  8. figure('name','image with 132 index')
  9. imshow(ind_132),colormap(map_132);
you can also found that sum image on the above link or here 

results
Original Image

Image with 32 Index
Image with 132 Index


Thursday, 25 June 2015

For loop in C : Features I never heard of

We usually use for loop when there is a known number of iterations... Where the while loop is when number of iterations not known in advance.

For loop allow us to define Three expressions in its syntax.

1.Initialization
2.Testing condition
3.change or update

Initialization : It is done Just once,when the for loop just starts.
so it will done its job even though the condition is false in the first iteration.

Testing Condition : It is evaluated before each potential execution of loop
If the condition is true then statements in the loop are executed. If not the it will jump from the loop(the loop is terminated).

Change or Update : It is Evaluated at the end of each loop.

In its syntax it is having three Expressions Separated by Semicolon (;) every time i put a colon instead if semicolon which eventually gives me an syntax error.

For loop Must have Two semicolons even though there are no Expressions

for( ;  ;  )
{

}

is considered as infinite loop

initialization will run only once at the beginning of for loop even though the condition is false.

#include<stdio.h>
#include<conio.h>
int main(void)
{
int a = 6;

for(printf("a = %d \n",a);a<3;) // condition is false
{
printf("entered for loop\n");
}

printf("for loop is terminated\n");
getch();
return 0;
}

output :

Please note that condition is false at the first test But initialisation is executed.

Comma Operator :
Comma operator is useful when we wanted to Include more than One initialization or update expression in for loop.
#include<stdio.h>
#include<conio.h>
int main(void)
{
int i,j;

for(printf("i\tj\n"),i=0,j=0;i<3;i++,j++)
{
printf("%d\t%d\n",i,j);
}

printf("for loop is terminated");
getch();

return 0;

}//end of main

Output :

please note that we cant use colon operator in test condition.






Saturday, 30 May 2015

Seven Segment LED display with Arduino and Proteus

In a simpler view seven segment led display have seven led's with their combination we can display decimal numerals.

when connecting a common cathode in a proteus i have no idea which pin is associated to the respective led in display so i write a little code to do that work for me.

Code :
/*
Arduino Sketch to find out which pin is Associated with corresponding LED in 7 segment display
Author : Kunchala Anil

 It is a common cathode 7 Segment Display.
 connect the common cathode to ground and all the remaining pins to the Arduino Digital pins 2 to 8
 */

int pin = 2;
int pin_dummy = 2;
void setup()
{
  Serial.begin(9600);
  Serial.println("Hello World");
  for(int i = 2; i <= 8; i++)
  {
    pinMode(i,OUTPUT);
    Serial.print("pin \t");
    Serial.print(i);
    Serial.println("\t is set as Output");
  }//end of for loop

}//end of setup

void loop()
{
  Serial.println("Please enter y");
  while(!Serial.available())
  {
    //wait until user enters the data
  }//end of while
  if(Serial.available())
  {
    if(Serial.read() == 'y')
    {
      if(pin_dummy <= 8)
      {
        digitalWrite(pin_dummy,LOW);
        Serial.println("you entered y");
        digitalWrite(pin,HIGH);
        Serial.print("pin ");
        Serial.print(pin);
        Serial.println(" is activated");
        pin_dummy = pin; // pin_dummy is used to OFF the LED before ON next led in display
        pin++;
      }
      else{
        Serial.println("Maximum pin number is reached");
        pin = 2;
        pin_dummy =2;
        Serial.println("Pin values are RESET");
      }//end of If else

    }
    else{
      Serial.println("you entered wrong character");
    }//end of If Else 
  }//end of If 

}//end of loop

after executing the above code i get the following combination
GFEDCBA for 2,3,4,5,6,7,8 pins respectively.
The following code is used to display the 0-9 in common cathode seven segment display.

/*
hEXA DECIMAL ENCODING FOR DISPLAYING NUMBERS 0 TO 9
 FOR COMBINATION - GFEDCBA to pins 2,3,4,5,6,7,8 
 */
/*
 Hex codes for displayiing respective values
 */
const int ZERO = 0X7E;
const int ONE = 0X30;
const int TWO = 0X6D;
const int  THREE = 0X79;
const int  FOUR = 0X33;
const int  FIVE = 0X5B;
const int  SIX = 0X5F;
const int  SEVEN = 0X70;
const int   EIGHT = 0X7F;
const int  NINE = 0X7B;

//pins initialization
int pin[] = {
  2,3,4,5,6,7,8};

//calculating the no of pins
int no_pins = sizeof(pin)/sizeof(pin[0]);


void setup(){
  Serial.begin(9600);
  //setting all pins as Outputs
  for(int i = 0; i < no_pins;i++)
  {
    pinMode(pin[i],OUTPUT);

  }//end of for loop
}//end of setup


void loop(){
  for(int i=0;i<=9;i++)
  {
    switch(i)
    {
    case 0:
      display_it(ZERO);
      break;

    case 1:
      display_it(ONE);
      break;

    case 2:
      display_it(TWO);
      break;

    case 3:
      display_it(THREE);
      break;

    case 4:
      display_it(FOUR);
      break;

    case 5:
      display_it(FIVE);
      break;

    case 6:
      display_it(SIX);
      break;

    case 7:
      display_it(SEVEN);
      break;

    case 8:
      display_it(EIGHT);
      break;

    case 9:
      display_it(NINE);
      break;

    default :
      Serial.println("something went wrong");

    }//end of switch
  }//end of for loop()
}//end of loop


void display_it(const int value)
{
  for(int i=2,j=0;i<=8;i++,j++)
  {
    digitalWrite(i,bitRead(value,j));

  }
  delay(1000);
}

Proteus Schematic is :









size of Array Vs No of elements in Array

If we wanted to determine the size of array.. the first thought we get is use of sizeof operator.

for the beginner level the size of array means number of elements in array.. which is a false assumptions.

Normally there is so much difference between size of array and number of elements in the array.
lets look at the arduino reference about size of


sizeof :
Description
The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array.
Syntax
sizeof(variable)
Parameters
variable: any variable type or array (e.g. int, float, byte)
As it says it returns the number of Bytes Occupied by an Array in the Memory 

lets try simple code:

int ex_array[ ] = {1,2}; //define array
int ex_array_length = sizeof(ex_array);//find length using sizeof
void setup(){
Serial.begin(9600);
Serial.print("the value sizeof returns is ");
Serial.println(ex_array_length);
}

void loop(){
//do nothing 
}

The Output is :
the value sizeof return is 4

But the ex_array is having only two elements and the program returns 4.
It means that the array named ex_array[ ] occupied 4 bytes of memory. since int is 16 bits which is equivalent to 2 bytes and two integer variables occupy 4 bytes of memory.

To find the No of elements in Array:
To find number of elements in that array we need to divide the value sizeof returns by sizeof datatype of that array.

so No of elements in array = sizeof(array)/sizeof(datatype of array)


int ex_array[ ] = {1,2}; //define array
int ex_array_elements = sizeof(ex_array)/sizeof(int);//find length using sizeof
void setup(){
Serial.begin(9600);
Serial.print("the number of elements in array is ");
Serial.println(ex_array_elements);
}

void loop(){
//do nothing 
}

The Output is:
the number of elements in array is 2

which solves our present problem...

But if we accidently changed the datatype of array there will be nasty bug if we didn't change the bottom datatype also..

so to avoid that problem we can we something like this
No of elements in array = sizeof(array)/sizeof(first number in array)

int ex_array[ ] = {1,2}; //define array
int ex_array_elements = sizeof(ex_array)/sizeof(ex_arry[0]);//find length using sizeof
void setup(){
Serial.begin(9600);
Serial.print("the number of elements in array is ");
Serial.println(ex_array_elements);
}

void loop(){
//do nothing 
}