inflection detection

Status
Not open for further replies.

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;
}
 
bitsout!=bitsout is just a comparison of bitsout with itself. Try "bitsout = !bitsout"

This doesn't look right:
Code:
pinMode(ledPin, bitsout);
bitsout should be OUTPUT?

Pete
 
float val = sin(phase) * 2500.0 + 2050.0; // Does this really generate a sine wave offset by half of the DAC range ?

IValue=IValue>>3; // is this the cause of the mismatched scale between input and output ?
 
this was to try and get what looked like noise out of the results:
IValue=IValue>>3;

I think the root problem is the scaling. the idea is to run a SIN Wave into the ADC, then output it to the DAC. I have 2 teencys connected with #1 being the generator and # 2 the sample and hold. the input to the Sample and hold is 1VPP but the output is 0.5VPP. Here is the Sample and Hold code:

int IinputPin = A0;
const int buttonPin = 12;
int ledPin = 13; // select the pin for the LED
int IValue = 0; // variable to store the value coming from the sensor
int buttonState = 0;


void setup()
{
analogWriteResolution(12);
analogWriteResolution(12);
pinMode(buttonPin,INPUT_PULLUP);
}

void loop() {
digitalWrite(ledPin, bitsout);
IValue = analogRead(IinputPin);


buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
analogWrite(A14, IValue); }
}
 
Code:
void setup()
{
[B]analogWriteResolution(12);         both of these statements are the same
analogWriteResolution(12);[/B]
pinMode(buttonPin,INPUT_PULLUP);
}

Shouldn't one of those be analogReadResolution(12) ?
 
And again I think you should print out the values your sine formula is producing.

float val = sin(phase) * 2500.0 + 2050.0;
 
Status
Not open for further replies.
Back
Top