ProjectBandya

  • Home
  • Projects
    • Micro-Controller
      • 8051
      • ARDUINO
      • AVR
      • ARM
    • Micro-Processor
      • Raspberry Pi
      • BeagleBone Black
      • ARM 9
      • ARM 11
    • Power Electronics
    • Digital Electronics
    • Components
  • Programming
    • C Programming
    • C++ Programming
    • Java Programming
    • Python Programming
  • Articles
    • Articles
    • Great Inventions
    • Great Theories
  • Store
    • IC'S
    • Passive Compnents
    • Batteries
  • Forum
  • Download
  • About us
  • Contact us
  • In News
In this lesson, you will learn how to make sounds with your Arduino. First you will make the Arduino play a 'musical' scale and then combine this with a photocell, to make a Theremin-like instrument that changes the pitch played as you wave your hand over the photocell.





Parts

1 kΩ Resistor (brown, black, red stripes) - 1




Piezo sounder - 1




Photocell (Light Dependent Resistor) - 1




Half-size Breadboard - 1




Arduino Uno R3 - 1




Jumper wire pack - 1





Circuit Connection


For the first part of this lesson, the only thing on the breadboard is the Piezo buzzer. One pin of the piezo sounder goes to GND connection and the other to digital pin 12.





Program your Arduino with the following sketch:


Copy Code

  1. /*

  2. Adafruit Arduino - Lesson 10. Simple Sounds

  3. */

  4.  

  5. int speakerPin = 12;

  6.  

  7. int numTones = 10;

  8. int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};

  9. // mid C C# D D# E F F# G G# A

  10.  

  11. void setup()

  12. {

  13. for (int i = 0; i < numTones; i++)

  14. {

  15. tone(speakerPin, tones[i]);

  16. delay(500);

  17. }

  18. noTone(speakerPin);

  19. }

  20.  

  21. void loop()

  22. {

  23. }




To play a note of a particular pitch, you specify the frequency. See the following section on sound. The different frequencies for each note are kept in an array. An array is like a list. So, a scale can be played by playing each of the notes in the list in turn.
The 'for' loop will count from 0 to 9 using the variable 'i'. To get the frequency of the note to play at each step, we use 'tone[i]'. This means, the value in the 'tones' array at position 'i'. So, for example, 'tones[0]' is 261, 'tones[1]' is 277 etc.
The Arduino command 'tone' takes two parameters, the first is the pin to play the tone on and the second is the frequency of the tone to play.
When all the notes have been played, the 'noTone' command stops that pin playing any tone.
We could have put the tone playing code into 'loop' rather than 'setup', but frankly the same scale being played over and over again gets a bit tiresome. So in this case, 'loop' is empty.
To play the tune again, just press the reset button.

Sound

Sound waves are vibrations in the air pressure. The speed of the vibrations (cycles per second or Hertz) is what makes the pitch of the sound. The higher the frequency of the vibration, the higher the pitch.


Middle C is usually defined as a frequency of 261 Hz. If you turn a digital output on and off again 261 times every second then that output will be middle C.
To hear the output, we need to attach something that will convert the electrical signal into sound waves. This can be done with a loudspeaker or as we have used here a piezo sounder.
Piezo sounders use a special crystal that expands and contracts as an electrical signal passes through it. This will generate a tone that we can hear.

Pseudo-Theremin

The Theramin (http://en.wikipedia.org/wiki/Theremin) is a musical instrument that makes spooky synthesized sounds as you wave your hands in front of it. It was used in the theme music for the original Star Trek series.
We are going to make a similar instrument, albeit a lot less musical, but it will change the pitch of the note as you wave your hand in front of it.
We can leave the piezo sounder where it is, attached directly to the Arduino, but we will need the breadboard for the photocell and resistor that are going to control the pitch.


Arduino Code Sketch


Upload the following code on to your Arduino board.


Copy Code

  1. /*

  2. Adafruit Arduino - Lesson 10. Pseudo Thermin

  3. */

  4.  

  5. int speakerPin = 12;

  6. int photocellPin = 0;

  7.  

  8. void setup()

  9. {

  10. }

  11.  

  12. void loop()

  13. {

  14. int reading = analogRead(photocellPin);

  15. int pitch = 200 + reading / 4;

  16. tone(speakerPin, pitch);

  17. }




The sketch is actually really straightforward. We simply take an analog reading from A0, to measure the light intensity. This value will be in the range of something like 0 to 700.
We add 200 to this raw value, to make 200 Hz the lowest frequency and simply add the reading divided by 4 to this value, to give us a range of around 200Hz to 370Hz.
Try changing the value 4 in the line below to lower and higher values.  

Copy Code

  1. int pitch = 200 + reading / 4;


This will expand or restrict the range of frequencies. 

 Returning to the first sketch, try and modify it to play a tune. Hint, you can just change the values in the 'tones' array. Note that if you change the number of notes from 10 notes, then you will need to change 'numTones' accordingly.

Special Operators in C:

  • Below are some of special operators that C language offers.
 S.noOperatorsDescription
1
&
This is used to get the address of the variable.
Example : &a will give address of a.
2
*
This is used as pointer to a variable.
Example : * a  where, * is pointer to the variable a.
3
Sizeof ()
This gives the size of the variable.
Example : size of (char) will give us 1.

Example program for & and * operators in C:

  • In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get the value of the variable that the pointer is pointing to. Please refer C – pointer topic to know more about pointers.
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q’s value using ptr variable */
printf(“%d”, *ptr);
return 0;
}

Output:

50

Example program for sizeof() operator in C:

  • sizeof() operator is used to find the memory space allocated for each C data types.
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf(“Storage size for int data type:%d \n”,sizeof(a));
printf(“Storage size for char data type:%d \n”,sizeof(b));
printf(“Storage size for float data type:%d \n”,sizeof(c));
printf(“Storage size for double data type:%d\n”,sizeof(d));
return 0;
}

Output:

Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8

  • Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
  • Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
…
  • Example:
Increment operator :  ++ i ;    i ++ ;
Decrement operator :  - – i ;   i – - ;
…

Example program for increment operators in C:

  • In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.
//Example for increment operators

#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}

Output:

1 2 3 4 5 6 7 8 9

 Example program for decrement operators in C:

  • In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator and output is displayed as “20 19 18 17 16 15 14 13 12 11”.
//Example for decrement operators

#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}

Output:

20 19 18 17 16 15 14 13 12 11

Difference between pre/post increment & decrement operators in C:

  • Below table will explain the difference between pre/post increment and decrement operators in C.
 S.no
Operator typeOperatorDescription
1Pre increment++i
Value of i is incremented before assigning it to variable i.
2Post-incrementi++
Value of i is incremented after assigning it to variable i.
3Pre decrement– –i
Value of i is decremented before assigning it to variable i.
4Post_decrementi– –
Value of i is decremented after assigning it to variable i.

 Example program for pre – increment operators in C:

//Example for increment operators

#include <stdio.h>
int main()
{
int i=0;
while(++i < 5 )
{
printf("%d ",i);
}
return 0;
}

Output:

1 2 3 4
  • Step 1 : In above program, value of “i” is incremented from 0 to 1 using pre-increment operator.
  • Step 2 : This incremented value “1″ is compared with 5 in while expression.
  • Step 3 : Then, this incremented value “1″ is assigned to the variable “i”.
  • Above 3 steps are continued until while expression becomes false and output is displayed as “1 2 3 4″.

Example program for post – increment operators in C:

#include <stdio.h>
int main()
{
int i=0;
while(i++ < 5 )
{
printf("%d ",i);
}
return 0;
}

Output:

1 2 3 4 5
  • Step 1 : In this program, value of  i ”0″ is compared with 5 in while expression.
  • Step 2 : Then, value of “i” is incremented from 0 to 1 using post-increment operator.
  • Step 3 : Then, this incremented value “1″ is assigned to the variable “i”.
  • Above 3 steps are continued until while expression becomes false and output is displayed as “1 2 3 4 5″.

Example program for pre - decrement operators in C:

#include <stdio.h>
int main()
{
int i=10;
while(--i > 5 )
{
printf("%d ",i);
}
return 0;
}

Output:

9 8 7 6
  • Step 1 : In above program, value of “i” is decremented from 10 to 9 using pre-decrement operator.
  • Step 2 : This decremented value “9″ is compared with 5 in while expression.
  • Step 3 : Then, this decremented value “9″ is assigned to the variable “i”.
  • Above 3 steps are continued until while expression becomes false and output is displayed as “9 8 7 6″.

Example program for post - decrement operators in C:

#include <stdio.h>
int main()
{
int i=10;
while(i-- > 5 )
{
printf("%d ",i);
}
return 0;
}

 Output:

9 8 7 6 5
  • Step 1 : In this program, value of  i ”10″ is compared with 5 in while expression.
  • Step 2 : Then, value of “i” is decremented from 10 to 9 using post-decrement operator.
  • Step 3 : Then, this decremented value “9″ is assigned to the variable “i”.
  • Above 3 steps are continued until while expression becomes false and output is displayed as “9 8 7 6 5″. 
>

Conditional or ternary operators in C:

  • Conditional operators return one value if condition is true and returns another value is condition is false.
  • This operator is also called as ternary operator.
Syntax     :        (Condition? true_value: false_value);
Example  :        (A > 100  ?  0  :  1);
.
  • In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.

Example program for conditional/ternary operators in C:

#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf(“x value is %d\n”, x);
printf(“y value is %d”, y);
}

Output:

x value is 1
y value is 2

Bit wise operators in C:

  • These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
  • Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift) and >> (right shift).

Truth table for bit wise operation Bit wise operators

xy x|yx & yx ^ yOperator_symbolOperator_name
00000&Bitwise_AND
01101|Bitwise OR
10101~Bitwise_NOT
11110^XOR
 <<Left Shift
>>Right Shift
  • Consider x=40 and y=80. Binary form of these values are given below.
x = 00101000
y=  01010000
  • All bit wise operations for x and y are given below.
x&y = 00000000 (binary) = 0 (decimal)
x|y = 01111000 (binary) = 120 (decimal)
~x = 11111111111111111111111111 11111111111111111111111111111111010111
.. ..= -41 (decimal)
x^y = 01111000 (binary) = 120 (decimal)
x << 1 = 01010000 (binary) = 80 (decimal)
x >> 1 = 00010100 (binary) = 20 (decimal)
Note:
  • Bit wise NOT : Value of 40 in binary is 00000000000000000000000000000000 00000000000000000010100000000000. So, all 0′s are converted into 1′s in bit wise NOT operation.
  • Bit wise left shift and right shift : In left shift operation “x << 1 “, 1 means that the bits will be left shifted by one place. If we use it as “x << 2 “,  then, it means that the bits will be left shifted by 2 places.

Example program for bit wise operators in C:

  • In this example program, bit wise operations are performed as shown above and output is displayed in decimal format.
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf(“AND_opr value = %d\n”,AND_opr );
printf(“OR_opr value = %d\n”,OR_opr );
printf(“NOT_opr value = %d\n”,NOT_opr );
printf(“XOR_opr value = %d\n”,XOR_opr );
printf(“left_shift value = %d\n”, m << 1);
printf(“right_shift value = %d\n”, m >> 1);
}

Output:

AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
  • These operators are used to perform logical operations on the given expressions.
  • There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
S.noOperatorsNameExampleDescription
1&&logical AND(x>5)&&(y<5)It returns true when both conditions are true
2||logical OR(x>=10)||(y>=10)It returns true when at-least one of the condition is true
3!logical NOT!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false

 Example program for logical operators in C:

#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf(“&& Operator : Both conditions are true\n”);
}
if (o>p || p!=20)
{
printf(“|| Operator : Only one condition is true\n”);
}
if (!(m>n && m !=0))
{
printf(“! Operator : Both conditions are true\n”);
}
else
{
printf(“! Operator : Both conditions are true. ” \
“But, status is inverted as false\n”);
}
}

Output:

&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
  • In this program, operators (&&, || and !) are used to perform logical operations on the given expressions.
  • && operator – “if clause” becomes true only when both conditions (m>n and m! =0) is true. Else, it becomes false.
  • || Operator – “if clause” becomes true when any one of the condition (o>p || p!=20) is true. It becomes false when none of the condition is true.
  • ! Operator  – It is used to reverses the state of the operand.
  • If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by “!” operator.
  • So, “! (m>n and m! =0)” returns false (0).

Relational operators in C:

  • Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.
S.noOperators
Example 
Description
1>x > yx is greater than y
2<x < yx is less than y
3>=x >= yx is greater than or equal to y
4<=x <= yx is less than or equal to y
5==x == yx is equal to y
6!=x != yx is not equal to y

Example program for relational operators in C:

  • In this program, relational operator (==) is used to compare 2 values whether they are equal are not.
  • If both values are equal, output is displayed as ” values are equal”. Else, output is displayed as “values are not equal”.
  • Note : double equal sign (==) should be used to compare 2 values. We should not single equal sign (=).
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf(“m and n are equal”);
}
else
{
printf(“m and n are not equal”);
}
}

Output:

m and n are not equal
Newer Posts Older Posts Home

Comming soon

About

Carriers at ProjectBandya

Copyright © 2015 ProjectBandya

Created By Mohit Ingale