philip.porhammer
Well-known member
I have 2 teencs, one generates aSIN wave and the other is supposed to turn an LED on only on the falling edge of the tone's slope
first code section is the tone generator, second is the falling slope detect. the slope detect output is noisy and the scale of the output on pin A14 is not the same scale os the driving tone.
Sig Gen:
float phase = 0.0;
float twopi = 3.14159 * 2;
elapsedMicros usec = 0;
int ledPin = 13;
void setup() {
analogWriteResolution(12);
pinMode(ledPin, OUTPUT);
}
void loop() {
float val = sin(phase) * 2500.0 + 2050.0;
analogWrite(A14, (int)val);
phase = phase + 0.02;
if (phase >= twopi) phase = 0;
digitalWrite(ledPin, HIGH);
while (usec < 2200) ; // wait
usec = usec - 2200;
digitalWrite(ledPin, LOW);
while (usec < 2200) ; // wait
usec = usec - 2200;
}
this is the is the falling slope detect:
int IinputPin = A0;
int ledPin = 13;
int IValue = 0;
int QValue = 0;
int bitsout = 0; //led toggles on each sample
int holdlastvalue = 0;
int RED=0;
int green=1;
void setup()
{
analogWriteResolution(12);
analogWriteResolution(12);
pinMode(ledPin, bitsout);
pinMode(RED, OUTPUT);
pinMode(green, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(green, LOW);
}
void loop() {
delayMicroseconds(410);
digitalWrite(ledPin, bitsout);
IValue = analogRead(IinputPin);
IValue=IValue>>3;
if(holdlastvalue >IValue)
{
analogWrite(A14, IValue);
digitalWrite(RED, HIGH);
}
else{digitalWrite(RED, LOW);
}
if(bitsout==0){bitsout=1;}else{bitsout=0;} //?? how do I just invert the bit... this has got to be the hard way should not this work: bitsout!=bitsout; ?
holdlastvalue=IValue;
}
first code section is the tone generator, second is the falling slope detect. the slope detect output is noisy and the scale of the output on pin A14 is not the same scale os the driving tone.
Sig Gen:
float phase = 0.0;
float twopi = 3.14159 * 2;
elapsedMicros usec = 0;
int ledPin = 13;
void setup() {
analogWriteResolution(12);
pinMode(ledPin, OUTPUT);
}
void loop() {
float val = sin(phase) * 2500.0 + 2050.0;
analogWrite(A14, (int)val);
phase = phase + 0.02;
if (phase >= twopi) phase = 0;
digitalWrite(ledPin, HIGH);
while (usec < 2200) ; // wait
usec = usec - 2200;
digitalWrite(ledPin, LOW);
while (usec < 2200) ; // wait
usec = usec - 2200;
}
this is the is the falling slope detect:
int IinputPin = A0;
int ledPin = 13;
int IValue = 0;
int QValue = 0;
int bitsout = 0; //led toggles on each sample
int holdlastvalue = 0;
int RED=0;
int green=1;
void setup()
{
analogWriteResolution(12);
analogWriteResolution(12);
pinMode(ledPin, bitsout);
pinMode(RED, OUTPUT);
pinMode(green, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(green, LOW);
}
void loop() {
delayMicroseconds(410);
digitalWrite(ledPin, bitsout);
IValue = analogRead(IinputPin);
IValue=IValue>>3;
if(holdlastvalue >IValue)
{
analogWrite(A14, IValue);
digitalWrite(RED, HIGH);
}
else{digitalWrite(RED, LOW);
}
if(bitsout==0){bitsout=1;}else{bitsout=0;} //?? how do I just invert the bit... this has got to be the hard way should not this work: bitsout!=bitsout; ?
holdlastvalue=IValue;
}