Jump to content
Electronics-Lab.com Community

"period and duty cycle" - electronics lab"


Recommended Posts

The URL is:


Following the link from youtube does not lead to anything useful.

The code has numinous errors. It would never run,

for spuddo:

This is a snapshot of the serial monitor displaying the output of the code below.


This is the circuit that I used. The duty control on the left. The two resistors limit the range of the output. You do not want zero duty cycle for example. The period control on the right. The resistor limits the period from going to zero for example.


The code:

// 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.println("  milliseconds");

  Serial.print("duty ");
  Serial.println("  milliseconds");
  Serial.print("on time ");
  Serial.println("  milliseconds");

  Serial.print("off time ");
  Serial.println("  milliseconds");  

  Serial.print(" duty cycle ");
  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++)
     for (int i=0; i <iHighDashes; i++)
   Serial.println(); //space at bottom





Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...