//
// A rework of the code from the video: https://www.youtube.com/watch?v=bIz9ektLunE
//
void virtual_Scope();
int iDutyCyclePin = 2; //set input pin for the left pot = duty cycle
int iPeriodPin = 1; //set input pin for the right pot = peroid
int iLedPin = 13; //ser pin for output to LED
//float fFrequency; //frequency = how many full periods per second or per 1000 millisedonds
float fPeriod = 10; //value from the left pot
float fDuty = 1;
long int liPeriod, liDuty;
int iDutyPercent;
long int liHigh_Time, liLow_Time;
void setup()
{
pinMode(iLedPin, OUTPUT); //declare ledpin as OUTPIN
//Serial Port begin
Serial.begin (9600);
}
void loop()
{
fPeriod = analogRead(iPeriodPin); //read the value from the right pot = period
fDuty = analogRead(iDutyCyclePin); //read the value from the left pot = duty cycle
//limit the range of the duty cycle to 20 to 80 percent of the period because that is what works!
fDuty = (fDuty*9/10000) * fPeriod ; //with 5 volt supply
liDuty = int(fDuty); //for printouts below
liPeriod = int(fPeriod);
//iDutyPercent = 100*liDuty/1024; //cal duty cycle as percent for serial monitor ????
iDutyPercent = 100*liDuty/liPeriod; //cal duty cycle as percent for serial monitor
// fFrequency = (1000/float(liPeriod)); //cal frequency in units of Hertz for serial monitor ???
// liHigh_Time = liDuty*liPeriod/1024; //cal high = on time ???
liHigh_Time = liDuty; // high = on time
liLow_Time = liPeriod - liHigh_Time; //cal low = off time
Serial.println(); //new line
Serial.print("period ");
Serial.print(liPeriod);
Serial.println(" milliseconds");
Serial.print("duty ");
Serial.print(liDuty);
Serial.println(" milliseconds");
Serial.print("on time ");
Serial.print(liHigh_Time);
Serial.println(" milliseconds");
Serial.print("off time ");
Serial.print(liLow_Time);
Serial.println(" milliseconds");
Serial.print(" duty cycle ");
Serial.print(iDutyPercent);
Serial.println(" % ");
// Serial.print("frequency "); //what is frequency here?
// Serial.print(fFrequency);
// Serial.println(" Hertz");
digitalWrite(iLedPin, HIGH); //set the ledPin on
delay(liHigh_Time); //pause the program for high_time in microseconds
digitalWrite(iLedPin, LOW); //set the ledPin off
delay(liLow_Time); //pause the program for low_time in microseconds
virtual_Scope(); //display trace
delay(4000); //slow down the scrolling
}
void virtual_Scope()
{
//display a line of pulses
int iHighDashes, iLowDashes;
int iCount;
//let one dash = 30 ms
iHighDashes = liHigh_Time/30;
iLowDashes = liLow_Time/30;
iCount = 60/(iHighDashes+iLowDashes); //try to make the lengths similar
// Serial.println(String(iCount) + " "+ String(iLowDashes) + " " + String(iHighDashes) ); //new line for space at top
Serial.println(); //space at top
while (iCount--)
{
for (int i=0; i <iLowDashes; i++)
{
Serial.print("_");
}
for (int i=0; i <iHighDashes; i++)
{
Serial.print("-");
}
}
Serial.println(); //space at bottom
}