# sine analog compare triangle signal to pwm

sorry..i try to read sine wave and triangle wave from analog input signal to produce pwm output signal.

the triangle signal need offset ( example 1V) how to write program to offset this signal
also to invert sine wave from negative cycle to positif cycle.
need help...

What frequency ?

sine read from generator 50Hz ( amplitude 3V )
triangle read from generator 5Khz ( amplitude 1V)

Do you have a function generator to generate the sinewave and triangle wave ?

yes i have function generator to sine wave and triangle wave

for example

int sinpin=A0;
int tripin=A5;
int pulse1=13;
int pulse2=7;

then i want offset this triangle wave 1V and invert sine wave from positive to negative cycle

Do you have this function generator at home ?

i have this at university.

try the Map function

i try tu use this program but dont know this correct or not

int SinPin = A0; // select the input pin for sin generator
int TriPin= A5; // select the input pin for the tri generator
int PulsePin1 = 13;
int PulsePin2= 7;

void setup() {
// declare the pulsePin as an OUTPUT:
pinMode(PulsePin1, OUTPUT);
pinMode(PulsePin2, OUTPUT);
pinMode(SinPin,INPUT);
pinMode(TriPin,INPUT);

}

void loop() {
int analogsin = analogRead(SinPin); // original sinewave
int analogtri= 1+ analogRead(TriPin); // triangle OFFSET 1V
int analognegsin= -1* analogRead(SinPin); // invert sine

if (analogsin>=analogtri) {
digitalWrite( PulsePin1, HIGH);

}
if ( analogsin<=analogtri){
digitalWrite(PulsePin1,LOW);

}
if( analognegsin>=analogtri){
digitalWrite( PulsePin2, HIGH);
}
else{
digitalWrite(PulsePin2, LOW);
}
}

Read my last post. I gave a suggestion. Use it.

thnaks

how about if i want make PI controller? then make pwm pulse as output? i show schematic need advice…

I think you mean PID controller.
Take a look at this PID library for arduino.
http://playground.arduino.cc/Code/PIDLibrary

if have arduino due, then want to use analog pin as input but my input if directly reading data may be cannot because
have voltage over 3.3 V example

analog A0 assign reading 24V AC peak to peak
Analog A1 assign reading 25V DC
Analog A2 assign reading 5V triangle Peak to peak
Analog A5 assign reading 5A AC current

my question is how to decrease this reading it could be can connection to due analog pin..any circuit can reduce this voltage or current..

No Arduino can handle a -ve voltage on any of its pins.

``````int analogtri= 1+ analogRead(TriPin); // triangle OFFSET 1V
``````

The above does NOT give an offset of 1V. Read up on analogRead().

Mark

if have arduino due, then want to use analog pin as input but my input if directly reading data may be cannot because
have voltage over 3.3 V example

analog A0 assign reading 24V AC peak to peak
Analog A1 assign reading 25V DC
Analog A2 assign reading 5V triangle Peak to peak
Analog A5 assign reading 5A AC current

my question is how to decrease this reading it could be can connection to due analog pin..any circuit can reduce this voltage or current..