Link to home
Start Free TrialLog in
Avatar of nickclaridge
nickclaridge

asked on

Urgent but Simple....I think. Aid in manipulating these two code segments.

hi....this first section of code draws a 70mm length of a 8 - 12 Ghz sine wave. The frequency is changed using a scroll bar. I'm not sure how it works...I'm new to programming, but it does what it needs to.

Private Sub DrawWave()
 
  Me.PicWave.Cls
  ' number of sine waves per box = 70mm/wavelength(mm), ie 2.33 for 10ghz
 
  Dim NumWaves As Single
  Dim Wavelength As Double
  Dim GHZFreq As Double
  Dim x As Single
  Dim y As Double
  Dim Freq As Single
 
  GHZFreq = scrGunDiode.Value / GHZFactor
  Wavelength = 300 / GHZFreq
  NumWaves = WIDTHmm / Wavelength
 
  For x = 0 To WIDTHmm Step 0.1
     
     y = Sin(x * NumWaves * (360 / WIDTHmm) * PI / 180) * AMPLITUDE
     Me.PicWave.PSet (x, y), vbRed
 
  Next x
 
  Me.lblGunDiodeHz = CStr(Val(scrGunDiode.Value / GHZFactor)) & " GHz"

End Sub


The next section of code is designed to convert the 8 - 12 GHz signal, convert it into the corresponding wavelength. It then changes the original wavelength to the wavelength it changes to when it passes througha waveguide, called ComputeWaveGuideWavelength.
Public Function ComputeWavelength(dblFrequency As Double) As Double

  ComputeWavelength = 3 * 10 ^ 8 / dblFrequency

End Function

Public Function ComputeWaveGuideWavelength(dblFrequency As Double) As Double

  Dim dblOrigWaveLength As Double

  dblOrigWaveLength = ComputeWavelength(dblFrequency)

  ComputeWaveGuideWavelength = dblOrigWaveLength / Sqr(1 - ((dblOrigWaveLength / 0.04572) ^ 2))

End Function

these pieces of code were given to me by two different experts and hence they use different variable names. What I'm trying to do is instead of drawing the original wavelength is to draw the wavelength generated using ComputeWaveGuideWavelength. The second snippet of code is in a code module, not in the form module. How and what would I change to achieve this???

Thanks, Nick
Avatar of nickclaridge
nickclaridge

ASKER

The scroll bar should still be able to change the original wavelength. This should then cause ComputeWaveGuideWavelength to calculate the new waveguide wavelength which it passes to the DrawWave function. Not being too familiar with programming I'm not sure how to do this.
If I understand what you are trying to do, try inserting this line:

Wavelength = ComputeWaveGuideWavelength(Wavelength)

after the original line that begins "Wavelength = "

It also looks like the Sqr function call in ComputeWaveGuideWavelength will fail if the calculation (1 - ((dblOrigWaveLength / 0.04572) ^ 2)) yields a negative number. It is possible you will never have a problem that, but if you receive a run-time error 5 : "Invalid procedure call or argument", that may well be the problem. I don't know if taking the absolute value (Abs()) of the result will yield a valid result.

I did receive a run-time error 5 and it pointed me to the line with the sqr function. How do I take the absolute value of the result. Could you show me please.
Thanks
Nick
No that line should not produce a negative value but it still produces a run-time 5 error. Do you know why? The value produced by 1 - ((dblOrigWaveLength / 0.04572) ^ 2)) is less than 1.
Abs(1 - ((dblOrigWaveLength / 0.04572) ^ 2)))

Less than 1 shouldn't matter.
Hi again. If the rin-time error 5 is produced its not because its square rooting a negative number.
What values are you using for AMPLITUDE and GHZFactor? Is there a specific range you are restricting the scroll bar scrGunDiode to?
What values are you using for AMPLITUDE and GHZFactor? Is there a specific range you are restricting the scroll bar scrGunDiode to?
The line should end up looking like this:

ComputeWaveGuideWavelength = dblOrigWaveLength / Sqr(Abs(1 - ((dblOrigWaveLength / 0.04572) ^ 2)))
The scroll bar is restricted to values between 8 * 10^9 and 12 * 10^9. It should never produce a value less than 1.
if you are absolutely sure that the sqrt argument is SUPPOSED to be between 0 and 1, then make sure that you're using the right constant UNITS in your expression.

Remember that your units for wavelength here is mm, and make sure that constant isn't for meters, if it is, convert it.

The fact that I can get a positive number for the expression if I multiply the originalwavelength by 1000 suggests that the millimeter/meter effect is in play...




and technically, your scroll bar is limited between 8*GHZfactor and 12*GHZFactor (ie 800 and 1200) and your values from it converted into GHz frequencies in the calculations...

In case thats coming into play...
I meant divided by 1000
Should the scroll bar used to change the original wavelength use the same variable name in both parts of code? i.e.scrGunDiode.Value? This is the initial value of the frequency. Should this be tied into the second snippet of code? I hope this makes sense. Is it this that is causing the run-time 5 error?
If I put in the line with the Abs() function it doesn't draw a wave at all but a series of dots on the graph.
ASKER CERTIFIED SOLUTION
Avatar of GPrentice00
GPrentice00

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
GPrentice00,
Thanks for that. So it will allow me to display the waveguide wavelength in the picture box....is that right. i'll stick it in and see what happens.

Nick
Thanks again my friend. You really have been helpful. Lets hope I can get to grips with all this code you have given me.

Nick