Anybody help with a bit of C++ to Python please?
Anybody help with a bit of C++ to Python please?
Author
Discussion

TonyRPH

Original Poster:

13,560 posts

200 months

Thursday 4th January 2018
quotequote all
I'm trying to create this Arduino C++ loop in Python.

for (float freq = 15.0; freq < 20000.0; freq = freq + (freq / 100) * 12.2462) {
Serial.print(freq);
}

How can I do this in Python please?

Thanks!

plasticpig

12,932 posts

257 months

Thursday 4th January 2018
quotequote all
Something like

freq = 15
while freq < 2000:
print(freq)
freq = freq + (freq / 100) * 12.2462


If you actually want to output to a serial port then you would need to use the pyserial package.

I am by no means an expert at Python though so there may be a better way of doing it.






TonyRPH

Original Poster:

13,560 posts

200 months

Thursday 4th January 2018
quotequote all
Thanks but this seems to just go into a loop I can't get out of.

I have to kill it from another terminal.

I tried debugging it but it just seems to continually display the initial starting value of freq.


Taita

8,032 posts

235 months

Thursday 4th January 2018
quotequote all
Put an Else at the end?

https://stackoverflow.com/questions/3295938/else-c...

Not pretty.... but if it works.

TonyRPH

Original Poster:

13,560 posts

200 months

Thursday 4th January 2018
quotequote all
Sadly it didn't work for me.

I just got a Python error.

Should anyone want to compile this in c++ this is the full code (tried and tested) (unless someone can show me how to write this to the serial port on Linux and then read back the resulting output in real time... spin

  1. include <string>
  2. include <iostream>
using namespace std;

int main() {
for (float freq = 15.0; freq < 20000.0; freq = freq + (freq / 100) * 12.2462) {
cout << freq << "\n";
}
return 0;
}

Can compile with 'g++ -o looptest test.cpp'

Output should be:

15
16.8369
18.8988
21.2132
23.811
26.727
30
33.6739
37.7976
42.4264
47.622
53.4539
60
67.3477
75.5952
84.8528
95.244
106.908
120
134.695
151.19
169.705
190.488
213.815
240
269.391
302.381
339.411
380.976
427.631
479.999
538.781
604.761
678.822
761.952
855.262
959.999
1077.56
1209.52
1357.64
1523.9
1710.52
1920
2155.12
2419.04
2715.29
3047.8
3421.04
3839.99
4310.25
4838.09
5430.57
6095.61
6842.09
7679.98
8620.49
9676.17
10861.1
12191.2
13684.2
15360
17241
19352.3

plasticpig

12,932 posts

257 months

Thursday 4th January 2018
quotequote all
TonyRPH said:
Thanks but this seems to just go into a loop I can't get out of.

I have to kill it from another terminal.

I tried debugging it but it just seems to continually display the initial starting value of freq.
Worked for me


TonyRPH

Original Poster:

13,560 posts

200 months

Thursday 4th January 2018
quotequote all
I see you're on Windows - I also tried Python3 on Linux to no avail.

This however works.


#!/usr/bin/python

def my_range(start, end):
while start <= end:
yield start
start += (start / 100) * 12.2462

for x in my_range(15.0, 20000.0):
print(x)


Thanks all.

ETA: I even tried this with /usr/bin/python3.4 to no avail - but I note PlasticPig has Python 3.6 - the highest I have is 3.4 - I guess it must be that?


Edited by TonyRPH on Thursday 4th January 19:58

plasticpig

12,932 posts

257 months

Thursday 4th January 2018
quotequote all
TonyRPH said:
I see you're on Windows - I also tried Python3 on Linux to no avail.

This however works.


#!/usr/bin/python

def my_range(start, end):
while start <= end:
yield start
start += (start / 100) * 12.2462

for x in my_range(15.0, 20000.0):
print(x)


Thanks all.
Worked on Linux for me as well:


Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
>
15.0
16.83693
18.89881412166
21.213200696626725
23.811011680337028
26.72695579273446
29.99999225302431
33.67385130431417
37.79761848274309
42.426390437376774
47.62201106311881
53.453897781930465
59.99996901210123
67.34768521726117
75.59521744433741
84.85275896300585
95.24399753113347
106.90776795679113
119.99990703631569
134.695335651797
151.19039584638736
169.70547410252766
190.4879458720714
213.81548069945703
239.99975209687392
269.3906017381613
302.38071360822005
339.4108605581099
380.97579336377714
427.630850970692
479.99938024226486
538.7810643454931
604.7612710473709
678.821545822374
761.9513899668735
855.2614810849968
959.9985125816277
1077.5618504293989
1209.5222297566838
1357.6427410571468
1523.9023864124872
1710.5225204573333
1919.9965293575792
>

TonyRPH

Original Poster:

13,560 posts

200 months

Thursday 4th January 2018
quotequote all
How odd.

This is my exact script

#!/usr/bin/python3.4

freq = 15
while freq < 20000:
print(freq)
freq = freq + (freq / 100) * 12.2462


But yet this doesn't run.

Did I do something wrong?

Edited by TonyRPH on Thursday 4th January 20:02

plasticpig

12,932 posts

257 months

Thursday 4th January 2018
quotequote all
TonyRPH said:
How odd.


But yet this doesn't run.

Did I do something wrong?

Edited by TonyRPH on Thursday 4th January 20:02
Yes!


#!/usr/bin/python3.4

freq = 15
while freq < 20000:
print(freq)
freq = freq + (freq / 100) * 12.2462


Note that the "freq =" is indented the same as the print function. Indentation is important in Python as it marks blocks of code. I should have used code tags on my original post to keep the indentation....






Edited by plasticpig on Thursday 4th January 20:10

frisbee

5,639 posts

142 months

Thursday 4th January 2018
quotequote all
A slightly different method:


def freq_range(start, end):
while start<end:
yield start
start += (start / 100) * 12.2462

for freq in freq_range(15.0, 20000.0):
print freq

TonyRPH

Original Poster:

13,560 posts

200 months

Thursday 4th January 2018
quotequote all
Thanks all

@frisbee - yes that's almost identical to the one I got working earlier.

@plasticpig - it was indeed the indentation

I can 'do' (after a fashion) a couple of languages but I'm a total Python novice.

The indentation 'thing' has bitten my ass a couple of times now.