Saturday, August 7, 2010

Arduino based Oscilloscope + LED matrix display














code:
**************************************************************************
int counter=0;
int sv;
int mem[200];
int t=0;
int sv2=0;
int ss=0;
int d=0;
int de=0;
int mode;
int sv3=0;
int c=0;
void setup()
{
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(0,OUTPUT);
mode=analogRead(4);
mode=map(mode,0,1024,0,3);

de=analogRead(3);
de=map(de,1,1024,0,10);
//de=map(de,1,1024,-1,2);
//de=pow(10,de);
delay(100);
if (mode==1){
while (t<=200)
{
sv=analogRead(0);
delay(de);
sv=map(sv,0,1024,0,7);
mem[t]=sv;
t=t+1;
}
}
///////////////////////
if (mode==0){
while (t<=200)
{
sv=analogRead(0);
delay(de);
sv=map(sv,0,1024,0,20);
mem[t]=sv;
t=t+1;
}
}
}
void loop()
{
if (d==10)
{
d=0;
sv2=analogRead(1);
sv3=analogRead(2);
sv3=map(sv3,0,1024,0,20);
sv2=map(sv2,0,1024,0,200);
//////////// refreshing the seting/////
de=analogRead(3);
mode=analogRead(4);
mode=map(mode,0,1024,0,3);
de=map(de,1,1024,0,10);
//de=map(de,1,1024,-1,2);
// de=pow(10,de);
///////////////////////////
}
if (mode==2)
{
if (c==10)
{
sampling();
c=0;
}
}
c=c+1;
d=d+1;
desplay();
}
void desplay(){
t=sv2;
ss=sv2+6;
delay(1);
while (t<=ss)
{
delay(0);
sv=mem[t];
writel();
t=t+1;
delay(1);
}
}
void sampling(){
t=0;
if (mode==1){
while (t<=200)
{
sv=analogRead(0);
delay(de);
sv=map(sv,0,1024,0,7);
mem[t]=sv;
t=t+1;
}
}
///////////////////////
if (mode==2){
while (t<=200)
{
sv=analogRead(0);
delay(de);
sv=map(sv,0,1024,0,7);
mem[t]=sv;
t=t+1;
}
}
///////////////////////
if (mode==0){
while (t<=200)
{
sv=analogRead(0);
delay(de);
sv=map(sv,0,1024,0,20);
mem[t]=sv;
t=t+1;
}
}
////////////////////////
}
void writel(){
digitalWrite(0,LOW);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
if (mode==0)
{
sv=sv3-sv;
}
if (sv==0)
{
digitalWrite(6,HIGH);
}
if (sv==1)
{
digitalWrite(5,HIGH);
}
if (sv==2)
{
digitalWrite(4,HIGH);
}
if (sv==3)
{
digitalWrite(3,HIGH);
}
if (sv==4)
{
digitalWrite(2,HIGH) ;
}
if (sv==5)
{
digitalWrite(1,HIGH);
}
if (sv==6)
{
digitalWrite(0,HIGH) ;
}
sr();
}
void sr()
{
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
if (counter==0)
{
digitalWrite(7,LOW);
}
if (counter==1)
{
digitalWrite(8,LOW);
}
if (counter==2)
{
digitalWrite(9,LOW);
}
if (counter==3)
{
digitalWrite(10,LOW);
}
if (counter==4)
{
digitalWrite(11,LOW);
}
if (counter==5)
{
digitalWrite(12,LOW);
}
if (counter==6)
{
digitalWrite(13,LOW);
counter=-1;
}
counter=counter+1;
}
****************************************
NOTE:
By turning the PR4 the voltage at analog pin # 4 changes and it causes the mode of the oscilloscope to change.
here are different modes :
Mode # 1 : 0-1.5V ===> Capturing one set of samples and showing them in a wide vertical range ( in this mode, the vertical shift feature is enable )
Mode # 2: 1.5v-3.3v ===> Capturing one set of samples and showing them on display with fixed rang ( the vertical shift is disable)
Mode #3 : 3.3v-5v ===> Capturing a set of samples every 100 ms and showing them on screen with fixed range.

To capture new samples in the modes # 1 and 2 the Arduino has to be restarted.

At the end, the switches number 1 and 2 must be disconnect while Arduino is receiving data from computer. the pins number 0 and 1 are used as TX and RX , so connecting them to LEDs interrupts the communication between PC and Arduino

The DC offset Switch is used to disconnect the PR5 from input line when a normal input is needed ( By connecting offset switch , the input range is changed from 0-5V to -5_+5 which can be adjusted with PR5) . Be aware that offset mode only work when in put is an AC signal.



Check my video in YouTube :




Monday, May 24, 2010

Aurduino Oscilloscope

having a oscilloscope has been a dream for me since i started leaning electronics, because i can't afford buying an Oscilloscope( i never buy second hand electrical equipment ). Therefore i decided to solve my problem by myself, However i kinda failed.
The Arduino Oscilloscope has many problems such as its range ( ~ 1Khz :) ) and its scale ( there is no scale on the screen). the input also must be between 0 to 5 volt which is not a problem ( it can be increased by voltage dividers or using transistor). After all there are the Code for arduino and visual basic .... have some fun

**********************************Aurduino**************************

int incomingByte = 0;
const int analogInPin = 0;

int sensorValue = 0; // value read from the pot

void setup()
{
Serial.begin(115200);
pinMode(13,OUTPUT);
}


void loop()
{
sensorValue = analogRead(analogInPin);


Serial.println(sensorValue);


incomingByte=0;
delay(1);

}

NOTE: if u erase the delay, the computer will be left behind the data. Therefore the screen will be 20 sec behind what is actually happen .

*************************************Visual basic**************************


********* 3 text boxes and and a bottom are needed *****************
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

sp1.PortName = TextBox1.Text

sp1.Open()
Dim myPen As Pen = New Pen(Color.GreenYellow, 5)

sp1.Write(1)
Dim i As Integer
Dim u As Decimal
Dim x As Decimal
Dim z As Integer
Dim x1 As Decimal
Dim voltager As Integer
Dim timerange As Integer
Dim timerangex As Integer

voltager = Val(TextBox2.Text)
timerange = Val(TextBox3.Text)

Dim a As String

Dim n As Single
n = 0
Me.CreateGraphics.Clear(Color.Black)
Do
n = n + 1
If n = (800 / timerange) Then
n = 0
Me.CreateGraphics.Clear(Color.Black)
End If

a = sp1.ReadLine

z = Val(a)
x = 5 / 1024 * z
u = (5 * voltager)

x = x * voltager
x = u - x


Me.CreateGraphics.DrawLine(myPen, timerange * n, x1 - 1, timerange * n, x)

x1 = x



Loop




sp1.Close()

End Sub

End Class
*******************************************************

Friday, May 14, 2010

Infrared Radar

this is a infrared radar which has 1m rang. the radar is consisted of a servo, Sharp IR sensor and audrino board. the reason i used the audrino board is that i wanted to do a fast work and not spend time on making a PCB or soldering.However learning references of the language used by Aurdino compiler took time. i have tried many time in past to measure the distance by using IR transistor and Ir transmitter LED, but the result wasn't really good. the longest distance that i could measure was 10 cm which was effected by light of the sun.Three very important factors have caused my experimences to fail: 1-the angel between the transmitter and receiver 2- the type of transmitter and reviver 3- time ( it takes so much time to find problems and fix the circuit which you has designed ).. After all, i decided to use Sharp IR sensor.
As I've said, the language used for programming the microcontroller is C which was complied by audrino compiler
here is the cod:


-------------------------------------------------------------------------------------
"#include
int incomingByte = 0;
const int analogInPin = 0;
const int analogOutPin = 9;
int sensorValue = 0; // value read from the pot
int outputValue = 0;
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position

void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}


void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{

sensorValue = analogRead(analogInPin);
if (sensorValue>300){
digitalWrite(13,HIGH);
}
if (sensorValue<300){
digitalWrite(13,LOW);
}




Serial.print(sensorValue);
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(",");
Serial.println(pos);
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();

}
while (incomingByte != 49) {
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();

}
}


incomingByte=0;
delay(1); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{

sensorValue = analogRead(analogInPin);

if (sensorValue>300){
digitalWrite(13,HIGH);
}
if (sensorValue<300){
digitalWrite(13,LOW);
}

Serial.print(sensorValue);
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(",");
Serial.println(pos);
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();

}
while (incomingByte != 49) {
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();

}
}

incomingByte=0;
delay(1); // waits 15ms for the servo to reach the position
}
}
-------------------------------------------------------------------------------------
the out put is a line consisted of angle and the value of sensor => value,angle
exp: 425,180
the microcontroller also needs to receive the character "1" ( 65 in ASCII) to move to next angle ( it must be send after finishing of whole process by computer to draw the graph)
-------------------------------------------------------------------------------------
VISUAL BASIC 2008
the windows based program to show the graph was written on visual basic
note: two textboxs, a bottom , and serialport control is needed


here is the code for visual basic 2008:
-------------------------------------------------------------------------------------
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bit As Bitmap = New Bitmap(Me.Width, Me.Height)
Dim g As Graphics = Graphics.FromImage(bit)
Dim myPen As Pen = New Pen(Color.GreenYellow, 5)
Dim myPen2 As Pen = New Pen(Color.Yellow, 3)
Dim myPen3 As Pen = New Pen(Color.Blue, 3)
sp1.Open()
sp1.Write(1)
Dim i As Integer
Dim f As Decimal
Dim j As Decimal
Dim x As Integer
Dim z As Integer
Dim w As Decimal

Dim a As String
Dim b As Integer
Dim k As String
Dim c As Integer
Dim u As String
Dim y As String
Me.CreateGraphics.Clear(Color.Black)
Me.CreateGraphics.DrawRectangle(myPen2, 350, 350, 10, 10)
Me.CreateGraphics.DrawRectangle(myPen2, 0, 0, 190, 10)
Dim n As Single
n = 0
Do
n = n + 1
If n = 180 Then
n = 0
Me.CreateGraphics.DrawRectangle(myPen2, 0, 0, 190, 10)
Me.CreateGraphics.Clear(Color.Black)
End If
Me.CreateGraphics.FillRectangle(Brushes.Red, n, 0, 10, 10)
a = sp1.ReadLine
c = 0
u = ""
y = ""
For b = 1 To Len(a)
k = Mid(a, b, 1)
If k = "," Then
c = 1
End If
If c = 1 Then
If Not k = "," Then
u = u + k
End If
End If
If c = 0 Then
y = y + k
End If
j = Val(u) / 180 * 3.14
w = j
f = Val(y) / 2
f = 350 - f
x = 350 - f * Math.Cos(j)
z = 350 - f * Math.Sin(w)




Next
TextBox1.Text = TextBox1.Text + y + u
TextBox2.Text = TextBox2.Text + CStr(x) + " " + CStr(w) + " " + CStr(z) + "..."
Me.CreateGraphics.DrawLine(myPen, x - 2, z - 2, x, z)

Me.CreateGraphics.DrawLine(myPen3, 350, 350, x, z)
sp1.Write(1)
Loop
Me.CreateGraphics.DrawRectangle(myPen2, 350, 350, 10, 10)
Me.CreateGraphics.FillRectangle(Brushes.Yellow, 350, 350, 10, 10)

sp1.Close()

End Sub


Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub
End Class
---------------------------------------------------------------------------------
to find X and Y for drawing the sin and cos has been used.IT's a simple math concept
x=cos(angel)*length
y=sin(angle)*length





------------------------------------Circuit-----------------------------------------


the board is so easy :))))))))))) just connect the data output of IR receiver to analog port #0 of aurdio . the servo also needs to be connected to the pin number 9
CHECK THE VIDEO CLIP OF THIS PROJECT

*****************ALL PROGRAMS HAS BEEN WRITTEN BY DANIAL MOHAMMADI************