Jump to content
Electronics-Lab.com Community
spuddo

"period and duty cycle" - electronics lab"

Recommended Posts

Given what he shows in the video one can emulate  the code easily. If that is what you would like.

Share this post


Link to post
Share on other sites

Hi HarryA , thanks for your reply.

I am new to programming.

I did copy what I saw from the video.  Uploaded it and it did not work as  described.

It looked like a few lines of code were missing.

Any thoughts?

Best regards.

 

 

Share this post


Link to post
Share on other sites

The URL is:

https://www.youtube.com/watch?v=bIz9ektLunE

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.

monitor.png.409e9603a3f7d81fbfc5b2591d5fc7dd.png

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.

circuit.png.2db7927c2c504e8f594d69ecc0aef3cf.png

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.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
}

 

 

 

 

Share this post


Link to post
Share on other sites

Hi.

Thanks for your reply HarryA.

That program needed some surgery.

Ran yours and it achieved its aims.

I am needing a audio version , say five octaves that allows freq and duty cycle to vary independently.

Any thoughts.

Best regards.

Share this post


Link to post
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.

Guest
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.

Loading...

×
×
  • Create New...