 # Lissajous curves on an Oscilloscope

Greetings all. Just finished a program to display Lissajous Curves on an Oscilloscope. Used part of Johngineer’s www.johngineer.com christmas tree code and design to help with the x-y vector control of the oscilloscope. Check it out and comments would be great. I’m new to this so it would be nice to see how I could improve the display. Chris

``````/*
Display nice looking Lissajous curves on an oscilloscope
http://en.wikipedia.org/wiki/Lissajous_curve for more details.
Author: Chris Gozzard (cgozzard@yahoo.com)
Created: 24-5-13
License: This code CC-BY-SA 3.0 and is unsupported.

the following circuit is on both PWM ports (5 and 6)

R
PWM OUT ----/\/\/\-----+------------ OUTPUT
|
=== C
|
GND

R = 10k
C = 0.1uF

Many thanks to John M. De Cristofaro for the original idea (based on his Oscilloscope Christmas Tree circuit)
*/
#include <TrueRandom.h> // Just a nice libary that is better than random on the arduino.
float x_pos; // controls the x position for the Oscilloscope
float y_pos; // controls the y position for the Oscilloscope
int x_out = 5; // we are going to use pin 5 and 6...
int y_out = 6;
float x_phase = 0; // adjusting the phase of the waves over time gives a nice evolving look.
int A = 1; // A and B sets the frequency relationship between the X and Y waves
int B = 1;
int change = 30000; //Change A and B after 30000 loops
long t = 1; //time

void setup()  {
// declare x and y to be an outputs:
pinMode(x_out, OUTPUT);
pinMode(y_out, OUTPUT);
// this next section changes the PWM frequency - don't mess with it!
TCCR0A = (	1<<COM0A1 | 0<<COM0A0 |		// clear OC0A on compare match (hi-lo PWM)
1<<COM0B1 | 0<<COM0B0 |		// clear OC0B on compare match (hi-lo PWM)
1<<WGM01  | 1<<WGM00);		// set PWM lines at 0xFF

TCCR0B = (	0<<FOC0A | 0<<FOC0B |		// no force compare match
0<<WGM02 |			// set PWM lines at 0xFF
0<<CS02	 | 0<<CS01 |		// use system clock (no divider)
1<<CS00 );

TIMSK0 = (	0<<OCIE0B | 0<<TOIE0 |
0<<OCIE0A );
}

// the loop routine runs over and over again forever:
void loop()  {
if (t == change){

A = TrueRandom.random(1,4); // A changes to a value between 1 and 4
B = TrueRandom.random(1,4); // B changes to a value between 1 and 4
t = 0;
}

x_phase = t*0.0003; //changes the phase of wave A slightly each loop.
x_pos = 126 + (126*sin((A*t*0.1) + x_phase)); // calculates the x position
y_pos = 126 + (126*sin(B*t*0.1)); // calculates the y position

analogWrite(x_out, x_pos); // outputs the x position
analogWrite(y_out, y_pos); // outputs the y position
t++; // increases the time by 1
} // starts us all over again!
``````

No picture?

I'm in the lab tomorrow and will gladly take one..

a small idea idea in the code, removed some comments

``````/*
Display nice looking Lissajous curves on an oscilloscope
http://en.wikipedia.org/wiki/Lissajous_curve for more details.
Author: Chris Gozzard (cgozzard@yahoo.com)
Created: 24-5-13
License: This code CC-BY-SA 3.0 and is unsupported.

the following circuit is on both PWM ports (5 and 6)

R
PWM OUT ----/\/\/\-----+------------ OUTPUT
|
=== C
|
GND

R = 10k
C = 0.1uF

Many thanks to John M. De Cristofaro for the original idea (based on his Oscilloscope Christmas Tree circuit)
*/

#include <TrueRandom.h> // Just a nice libary that is better than random on the arduino.

float x_pos = 0;    // controls the x position for the Oscilloscope
float y_pos = 0;    // controls the y position for the Oscilloscope

const int x_out = 5;
const int y_out = 6;

float x_phase = 0; // adjusting the phase of the waves over time gives a nice evolving look.

int A = 1;             // A and B sets the frequency relationship between the X and Y waves
int B = 1;

unsigned int maxLoops = 30000;    //Change A and B after 30000 loops

unsigned long t = 1; //time

uint8_t x_amplitude = 126;
uint8_t y_amplitude = 126;

void setup()
{
pinMode(x_out, OUTPUT);
pinMode(y_out, OUTPUT);

// this next section changes the PWM frequency - don't mess with it!
TCCR0A = (	1<<COM0A1 | 0<<COM0A0 |		// clear OC0A on compare match (hi-lo PWM)
1<<COM0B1 | 0<<COM0B0 |		// clear OC0B on compare match (hi-lo PWM)
1<<WGM01  | 1<<WGM00);		// set PWM lines at 0xFF

TCCR0B = (	0<<FOC0A | 0<<FOC0B |		// no force compare match
0<<WGM02 |			// set PWM lines at 0xFF
0<<CS02	 | 0<<CS01 |		// use system clock (no divider)
1<<CS00 );

TIMSK0 = (	0<<OCIE0B | 0<<TOIE0 |
0<<OCIE0A );
}

// the loop routine runs over and over again forever:
void loop()
{
if (t == maxLoops)
{
A = TrueRandom.random(1, 4);
B = TrueRandom.random(1, 4);
t = 0;
}

// read the amplitude from analogPort
// phase is also an option
x_amplitude = map(analogRead(A0), 0, 1023, 0, 126);
y_amplitude = map(analogRead(A1), 0, 1023, 0, 126);

x_phase = t * 0.0003;                             //changes the phase of wave A slightly each loop.
x_pos = 126 + (x_amplitude * sin((A*t*0.1) + x_phase) );
y_pos = 126 + (y_amplitude * sin(B*t*0.1) );

analogWrite(x_out, x_pos);
analogWrite(y_out, y_pos);

t++;
}
``````

fun!

Fair point! Did go a little over-board with the comments. :blush: I'l post a video up later of it working.

A lot of comments if often an indication that you need to rename variables or functions. It should not rephrase the obvious from code.

My first rule of comments is it should never contradict the code. My second rule is that it should explain the WHY, not the HOW. In case of the timer code above which uses a lot of abbreviations, the how makes sense

100% cool !

I have changed the code slightly and removed the need for the TrueRandom library.

``````/*
Display nice looking Lissajous curves on an oscilloscope
http://en.wikipedia.org/wiki/Lissajous_curve for more details.
Author: Chris Gozzard (cgozzard@yahoo.com)
Created: 2-6-15
License: This code CC-BY-SA 3.0 and is unsupported.

the following circuit is on both PWM ports (5 and 6)

R
PWM OUT ----/\/\/\-----+------------ OUTPUT
|
=== C
|
GND

R = 10k
C = 0.1uF

Many thanks to John M. De Cristofaro for the original idea (based on his Oscilloscope Christmas Tree circuit)
*/
float x_pos; // controls the x position for the Oscilloscope
float y_pos; // controls the y position for the Oscilloscope
int x_out = 5; // we are going to use pin 5 and 6...
int y_out = 6;
float x_phase = 0; // adjusting the phase of the waves over time gives a nice evolving look.
int A = 1; // A and B sets the frequency relationship between the X and Y waves
int B = 1;
int change = 30000; //Change A and B after 30000 loops
long t = 1; //time

void setup()  {
// declare x and y to be an outputs:
pinMode(x_out, OUTPUT);
pinMode(y_out, OUTPUT);
// this next section changes the PWM frequency - don't mess with it!
TCCR0A = (	1<<COM0A1 | 0<<COM0A0 |		// clear OC0A on compare match (hi-lo PWM)
1<<COM0B1 | 0<<COM0B0 |		// clear OC0B on compare match (hi-lo PWM)
1<<WGM01  | 1<<WGM00);		// set PWM lines at 0xFF

TCCR0B = (	0<<FOC0A | 0<<FOC0B |		// no force compare match
0<<WGM02 |			// set PWM lines at 0xFF
0<<CS02	 | 0<<CS01 |		// use system clock (no divider)
1<<CS00 );

TIMSK0 = (	0<<OCIE0B | 0<<TOIE0 |
0<<OCIE0A );
}

// the loop routine runs over and over again forever:
void loop()  {
if (t == change){

A = random(1,4); // A changes to a value between 1 and 4
B = random(1,4); // B changes to a value between 1 and 4
t = 0;
}

x_phase = t*0.0003; //changes the phase of wave A slightly each loop.
x_pos = 126 + (126*sin((A*t*0.1) + x_phase)); // calculates the x position
y_pos = 126 + (126*sin(B*t*0.1)); // calculates the y position

analogWrite(x_out, x_pos); // outputs the x position
analogWrite(y_out, y_pos); // outputs the y position
t++;
}
``````

+1 :)