Author Topic: [Arduino] Need help with a square-wave generator  (Read 2252 times)

0 Members and 2 Guests are viewing this topic.

Offline Raavgo

  • Peasant
  • *
  • Posts: 88
  • Cookies: 12
  • On my way from a n00b to a PRO
    • View Profile
[Arduino] Need help with a square-wave generator
« on: 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:
  • The input
  • The actual square-wave generator
Part 1 works smoothly:

Code: [Select]
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:
Code: [Select]

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:
Code: [Select]

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?

« Last Edit: November 05, 2013, 03:14:45 pm by Raavgo »

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: [Arduino] Need help with a square-wave generator
« Reply #1 on: 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...
« Last Edit: November 06, 2013, 02:20:18 pm by Traitor4000 »
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline Raavgo

  • Peasant
  • *
  • Posts: 88
  • Cookies: 12
  • On my way from a n00b to a PRO
    • View Profile
Re: [Arduino] Need help with a square-wave generator
« Reply #2 on: 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#).
« Last Edit: November 06, 2013, 02:54:33 pm by Raavgo »

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: [Arduino] Need help with a square-wave generator
« Reply #3 on: November 06, 2013, 03:54:03 pm »
ps. At your school you learn C! Lucky we learn java...

Not lucky at all.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Raavgo

  • Peasant
  • *
  • Posts: 88
  • Cookies: 12
  • On my way from a n00b to a PRO
    • View Profile
Re: [Arduino] Need help with a square-wave generator
« Reply #4 on: November 06, 2013, 05:39:21 pm »
Not lucky at all.


What do you mean by that?


Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: [Arduino] Need help with a square-wave generator
« Reply #5 on: November 06, 2013, 06:50:07 pm »
In case you didn't notice, I'm not exactly a fan of Java.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Raavgo

  • Peasant
  • *
  • Posts: 88
  • Cookies: 12
  • On my way from a n00b to a PRO
    • View Profile
Re: [Arduino] Need help with a square-wave generator
« Reply #6 on: 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  ;)


Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: [Arduino] Need help with a square-wave generator
« Reply #7 on: 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
« Last Edit: November 08, 2013, 04:32:15 am by Traitor4000 »
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [Arduino] Need help with a square-wave generator
« Reply #8 on: 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.
>>>import this
-----------------------------