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;

int analogsin = analogRead(sinpin);
int analogtri=analogRead(tripin);

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 :slight_smile:

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..
need advise...

Google "Voltage Divider"

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..
need advise...

What is the Vcc for the DUE (3.3 OR 5V ?)
Probably 10k ohm resistor to the input, 2k resistor to GND, and where the two resistors connect in the middle is the output to the
arduino analog input. If necessary increase the 10k or reduce the 2k resistor to bring the analog input voltage down more. Use the
ratio of the two resistors 2/10 as a reference. Multiply the value obtained by analogRead by the recipricol of the voltage divider ratio to convert to the real voltage value.
24V in to the above voltage divider will give 4V out to the arduino . If that is still too high then change the 2k to 1.5k and then
24V in will give 3.1 volts out to the arduino. 3.1V x[ (10k+1.5k)/1.5k] = 24V (conversion to obtain actual voltage measured BEFORE the voltage divider)