RF transmittance of the Sine wave.

Hello, I'm having a problem with Transmitting a RF signal composed of a sine wave, Ive wired the antenna to digital port 5.
heres my code:

float Time;
double Sin;
int Ss;

void setup(){Serial.begin(9600);}
void loop() {
Time += 0.1;
Serial.println(Ss);
Sin = sin(Time);
Ss = (int) Sin;
digitalWrite(5,Ss*1024);
delay(100);
}

I'm puzzled. On the playground page it says that the "sin" function accepts float and gives double. I attempted to convert the double to int, Then print and send. I get response from the variable "Sin", But nothing from Ss.

Thanks in advance.

  • Nitrousoxide

P.s- Wow. When a usb cable is active, It creates a LOT of interferance.
P.p.s. Oh and i tired analogWrite. Thats why its sending out on 5.

Needs more explanation. Exactly what is going wrong?

Sin = sin(Time);
Ss = (int) Sin;

sin expects values in radians.
Values of "Ss" will be either -1, 0 or 1.

Ah, Ok, Sorry about that, Its not transmitting the sine wave. Im attempting to seed a sine wave into my transmitter, But im having trouble with coverting the variable type and outputting it. It seems to loose its value when its converting from "Sin" (Apparently double) to "Ss" (int), Using "cast". "Loose", Meaning i try printing the Sine variable. It returns what i expect it to. But when i print Ss, I get a big fat zero. Hope this helps.(ask if you still dont get it)

AWOL, I know what sin is/does/outputs. Im just having trouble converting it to an int and sending it.(As i said. in the playground it says it outputs double.)

That brought up a thought, can i just output double, And what about analogWrite, whats better, analog or digital?

What kind of input does your transmitter expect?

What I was trying to point out is that you are converting the value returned by "sin" to an "int".
That value will be -1 (very rarely) 0 (most of the time) or 1 (also, very rarely).

You need to scale the value up, and offset the negative values.

What, My giant coil of wire on a carboad toilet paper roll?
;D :o

How so, AWOL, I attempted to scale up the value by multiplying by 1024.

Sorry it just hit me (NOTE: I am very tired.), Do you mean use something like abs() to offset?

How so, AWOL, I attempted to scale up the value by multiplying by 1024

But by then it was too late - you'd already got an "int", which, as I've pointed out would only ever have the value -1, 0, or +1.
You need to scale whilst it is still a "double"

Thanks ill try that. Also im going to attempt to use a general purpose OP apmlifier chip to amplify the signals.

Nope, Nothing, I moved the multiplication to here:

Ss = (int) Sin*1024;

But nothing happened, Still the same.
Plus, I cant see why you cant scale it up whilst its an int.

digitalWrite(5,Ss*1024);

Just re-read it - that's not doing anything sensible; "digitalWrite" writes a single bit.

Pardon, Im not getting you. I had modified the code.(That one line)

See the problem is that im just reporting 0 in Ss.

Ss = (int) Sin*1024

No! You're converting it to an "int"!

Ss = Sin*1024;

Ok, In the peak of confusion, I got it to work. I realied what your trying to say, You just sisnt say it well, Your saying that digitalWrite and analogWrite can accept doubles, Not just int, so i can remove the conversion statement (im assuming thats what you meant). Anyway. if not, It works, i just didnt convert it.

I appreciate the help AWOL. Sorry if i shortened your life span by 3 years due to frustration.

But i still dont get it, why wasnt it converting it correctly?

Your saying that digitalWrite and analogWrite can accept doubles

No, not at all.
"digitalWrite" effectively only accepts single bits (HIGH or LOW).
"analogWrite" accepts all values from 0 (off) through to 255 (fully on), so a value of 128 gives a 50:50 duty cycle.

But i still dont get it, why wasnt it converting it correctly?

If the values returned by "sin" lie in the range -1..0..+1, then converting them to an "int" will simply truncate them, so nearly all the values will be zero.
Thus, "Ss = (int)Sin * 1024;" will first truncate "Sin" (nearly always zero, remember) and then multiply by 1024.
1024 * 0 = 0.
However, "Ss = Sin * 1024;" will first promote 1024 to a double (1024.0), multiply by "Sin" and then convert the result to an "int", again by truncation.

HTH

Ah ok thats odd because it IS accepting doubles accordig to this:

float Time;
double Sin;
int Ss;

void setup(){Serial.begin(9600);}
void loop() {
Time += 0.1;
Serial.println(Ss);
Sin = sin(Time);
Ss = Sin*1024;
analogWrite(5,Ss);
delay(100);
}

I thought digital is from 0 to 1024?
And analog is accepting more than 255.

Im confused. Is the duty cycle the value?

I get it now, Sorry. But still i can feed digital and analog write values way beyond their range. and they still work. why?

Duty cycle is related to the value.
analogWrite (pin, 1)
will give you a duty cycle of 1/255.
analogWrite (pin, 128)
will give you a duty cycle of 128/255.
and so on.

[EDIT] The values are probably being truncated or clipped within the "analogWrite"/"digitalWrite" routines.

Im not too sure about the definition "truncated', But i think you mean its mapping it so that if its greater than 255 it will equal 255. And im assuming digital is 0 = low, 1024 = high, and inbetween is ... yeah?

In digital, there is no inbetween.

There are 10 types of people; those who understand binary, and those who don't. :wink: