Go Down

Topic: Zero crossing detection (Read 17103 times) previous topic - next topic

taurian

Code: [Select]
#include <Timerthree.h>
#include <Timerone.h>

volatile int counterValue;
int requestValue;

void setup() {
  attachInterrupt(0, resetCounter, CHANGE);
  cli();                      //Disable global interrupts
  TCCR3A = 0;                 //Set register to 0
  TCCR3B = 0;                 //Set register to 0
  OCR3A = 640;                //Compare match register to get desired timer count (50Hz mains)
  TCCR3B |= (1 << WGM12);     //Turn on CTC mode
  TIMSK3 |= (1 << OCIE3A);    //Enable timer compare interrupt
  TCCR3B |= (1 << CS10);      //Start timer
  sei();                      //Enable global interrupts
  pinMode(7, OUTPUT);
}

ISR(TIMER3_COMPA_vect) {
  counterValue++;
  counterValue &= 0xff;
  if (counterValue==requestValue) {
    digitalWrite(7, HIGH);
  } else {
    digitalWrite(7, LOW);
  }
}

void resetCounter()
{
  counterValue=0;
}

void loop(){
  requestValue=map(analogRead(0), 0, 1023, 0, 255);
}


I already have the libraries when compiling the code but got an error says that the variables are not declared...
Example: sketch_mar13a: 11: error: 'OCR3A' was not declared in this scope
sketch_mar13a: 13: error: 'TIMSK3' was not declared in this scope
sketch_mar13a: 13: error: 'OCIE3A' was not declared in this scope


thank you

Techone

@taurian

Thank for the schematic. Did you connect the ground to the battery ?   Is like :  pos 9 neg   ---- GND --- pos 9 neg
The gnd is the middle of the battery connection. Did you connected it there ? That is the right place. The schematic look "fine", but I just wonder... 

raxz

I'm not using any libraries.
Following code compiles ok.
Binary sketch size: 3188 bytes (of a 126976 byte maximum)
Code: [Select]
volatile int counterValue;
int requestValue;

void setup() {
  attachInterrupt(0, resetCounter, CHANGE);
  cli();                      //Disable global interrupts
  TCCR3A = 0;                 //Set register to 0
  TCCR3B = 0;                 //Set register to 0
  OCR3A = 640;                //Compare match register to get desired timer count (50Hz mains)
  TCCR3B |= (1 << WGM12);     //Turn on CTC mode
  TIMSK3 |= (1 << OCIE3A);    //Enable timer compare interrupt
  TCCR3B |= (1 << CS10);      //Start timer
  sei();                      //Enable global interrupts
  pinMode(7, OUTPUT);
}

ISR(TIMER3_COMPA_vect) {
  counterValue++;
  counterValue &= 0xff;
  if (counterValue==requestValue) {
    digitalWrite(7, HIGH);
  } else {
    digitalWrite(7, LOW);
  }
}

void resetCounter()
{
  counterValue=0;
}

void loop(){
  requestValue=map(analogRead(0), 0, 1023, 0, 255);
}

taurian

Quote
I'm not using any libraries.
Following code compiles ok.
Binary sketch size: 3188 bytes (of a 126976 byte maximum)


Thank you now I'm trying to adapt the code to my project after any doubt I'll give news

thank:)

taurian

@techone

I have not connected to any battery my input signal going to pull the generator functions ..
but did not realize your question

thank

Techone

Your circuit is using op-amps. It need a dual power line ( + line, - line ) and the gnd is the mid point of the batteries connection. And the gnd has to be connected with the gnd to work properly.

I hope you understand.

taurian

The battery I used to say to this is to feed the polarization of the m-amp.
  The polarization and I'm using 5v and 0v and I use the v 5 the arduino.

Thank Techone

taurian

This and my code to read an analog value but the result is not good.

thank

Code: [Select]

#include <TimerOne.h>

int ledPin =2;    // LED connected to digital pin 13
int analogPin=3;

float a;
void setup()   {               
  Serial.begin(9600);
  pinMode(ledPin,INPUT);
  pinMode(13, OUTPUT); 
  Timer1.initialize(131000);
  attachInterrupt(0,periodo,RISING);


}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                   
{
  a=analogRead(analogPin);
  Serial.println(a);
}

void periodo()
{
 
  digitalWrite( 13, digitalRead( 13 ) ^ 1 );

}


raxz

What is not good? What You expect? What You get?

taurian

I wanted to see the values ??vary depending on the analog input signal and this does not happen that the values ??are repeated.

thank

Go Up