EvilZone

Programming and Scripting => Projects and Discussion => : Raavgo November 04, 2013, 02:31:11 PM

: [Arduino] Need help with a square-wave generator
: Raavgo November 04, 2013, 02:31:11 PM
Hey guys,I have got some problems with my school project.This project is about a square-wave generator.
My currently my project is separated into 2 parts:
Part 1 works smoothly:

:
volatile int frequenz = 4000;     //set frequencyint
i = 0; 
     
void setup(){ 
DDRD = DDRD | B00000100;   //set port 2   
noInterrupts();           //disable Interrupts 
TCCR1A = 0; 
TCCR1B = 0; 
TCNT1 = 0; 
OCR1A = frequenz; 

TCCR1B |=(1<<WGM12); //CTC mode p 133   
TCCR1B |=(1<<CS10);  //no prescale p 135 
TCCR1B |=(0<<CS11); 
TCCR1B |=(0<<CS12);   
TIMSK2 = ( 1<<OCIE1A ) ; // Enable interrupt when Timer reaches OCRA 
interrupts();
 
Serial.begin(9600); 
}

void loop(){}   
ISR(TIMER1_COMPA_vect)  {         
PORTD ^=0x04;  //toggles the pin 2 
}
}


[

and the 2nd part runs smoothly too:
:

volatile long frequenz = 0;     
char Data[7];
int i = 0;       


void setup()
{
  Serial.begin(9600); 
}


void loop()
{
  setFrequenz();
  Serial.println(frequenz);



void setFrequenz(){
  if( Serial.available())
  {
    char ch = Serial.read();
    if(i < 6 && isDigit(ch) )
    {
      Data[i] = ch;
      i++;
    }
    else
    {
      Data[i] = NULL;
      frequenz = atoilData);
      i = 0;
    }
  }
}

but when I now try to combine them I just doesn't work anymore.
instead of the number inputted I get 0jc all the time
I merged it together like this:
:

volatile long frequenz = 0;     
char Data[7];
volatile boolean pin = LOW;


int i = 0;       


void setup()
{
  DDRD = DDRD | B00000100;   //set port 3
 
  noInterrupts();           //disable Interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;


  OCR1A = frequenz;
  TCCR1B |=(1<<WGM12); //CTC mode p 133
 
  TCCR1B |=(1<<CS10);  //no prescale p 135
  TCCR1B |=(0<<CS11);
  TCCR1B |=(0<<CS12);
 
  TIMSK2 = ( 1<<OCIE1A ) ; // Enable interrupt when Timer reaches OCRA
  interrupts();
 


  //DDRD &= ~_BV(PD2); pin 2 = 1
  //DDRD |= _BV(PD2); pin 2 = 0
 
  Serial.begin(9600); 
}


void loop()
{
  noInterrupts();
  setFrequenz();
  Serial.println(frequenz);
  Interrupts();

 
ISR(TIMER1_COMPA_vect)
  {   
      PORTD ^=0x04;
  }


void setFrequenz(){
  if( Serial.available())
  {
    char ch = Serial.read();
    if(i < 6 && isDigit(ch) )
    {
      Data[i] = ch;
      i++;
    }
    else
    {
      Data[i] = NULL;
      frequenz = atol(Data);
      i = 0;
    }
  }
 
}



So where is my mistake is there someone who knows what to do?

: Re: [Arduino] Need help with a square-wave generator
: Traitor4000 November 06, 2013, 02:17:03 PM
Ok I'm going to be completely useless but this is C or C++ cause I certainly haven't seen volatile used however I'm have not nearly mastered C so I may have not seen this before. 

Ok yeah looked it up. I kind of see what your doing but can't help you.
ps. At your school you learn C! Lucky we learn java...
: Re: [Arduino] Need help with a square-wave generator
: Raavgo November 06, 2013, 02:54:14 PM
Ok I'm going to be completely useless but this is C or C++ cause I certainly haven't seen volatile used however I'm have not nearly mastered C so I may have not seen this before. 

Ok yeah looked it up. I kind of see what your doing but can't help you.
ps. At your school you learn C! Lucky we learn java...


Yeah we use C with arduino because the Arduino commands are too slow.
I think the Interrupt handling is the mistake but I had no time to try it until now.

Well I had to learn C and Assembler because I'm in a higher technical college of electronics (sounds far cooler than it really is), and we need it because we work alot with microcontrollers.

I like C very much but the problem is that there are a lot of coding languages far easier for tasks like Game development, App development, interaction with the web, etc... (of course it is possible to code such tasks in C but they are far easier in C#).
: Re: [Arduino] Need help with a square-wave generator
: vezzy November 06, 2013, 03:54:03 PM
ps. At your school you learn C! Lucky we learn java...

Not lucky at all.
: Re: [Arduino] Need help with a square-wave generator
: Raavgo November 06, 2013, 05:39:21 PM
Not lucky at all.


What do you mean by that?

: Re: [Arduino] Need help with a square-wave generator
: vezzy November 06, 2013, 06:50:07 PM
In case you didn't notice, I'm not exactly a fan of Java.
: Re: [Arduino] Need help with a square-wave generator
: Raavgo November 06, 2013, 08:11:51 PM
In case you didn't notice, I'm not exactly a fan of Java.


agreed I thought you talk about C  ;)

: Re: [Arduino] Need help with a square-wave generator
: Traitor4000 November 08, 2013, 04:30:17 AM
Let me add a comma to my statement You guys learn C! Lucky, we learn Java... The dot dot dot means I don't like java I'm saying he is lucky!


That would be odd sentence structure to say I am lucky to say that Java was better you might say:
You guys learn C! Luckily, we learn Java...


sorry went a bit grammar nazi there
: Re: [Arduino] Need help with a square-wave generator
: techb November 08, 2013, 11:44:37 AM
Honestly, I know java is hated. I don't care much for the execution of it myself, but really I think it is over hated. People hate it cause other people hate it, like twilight. People hate it cause other people hate it.

Like with twilight, the story is good, but the presentation is horrid. Java is the bastard child, it is used so much and never gets recognition. Android uses it, it is preferred over native stuff for app dev from the core devs themselves. It is taught in college for OOP for paradigm alone. It is a very useful language and is used by a lot of people, and makes industry apps.

Now, I don't really code Java unless I need it, but you can't knock something without using it. People who knock it are either people who use it everyday or people who never use it.