Good day! I am new in arduino and I am working on a project that uses interrupt without using pins.
I want to interrupt and initiate another job in the ISR using command.
Example:
If I receive data the interrupt will initate.
Thanks!
Good day! I am new in arduino and I am working on a project that uses interrupt without using pins.
I want to interrupt and initiate another job in the ISR using command.
Example:
If I receive data the interrupt will initate.
Thanks!
Look at the processor’s data sheet for a list of interrupts. Their is one that gets triggered when a serial character enters the UART, it is used normally to put the data into a buffer but you can do anything you want with it.
You have not said why you want to do this, and I would not be surprised if there is no need for an interrupt to achieve what you want.
You can trigger an interrupt by driving one of the external interrupt pins high (or low) with digitalWrite(). But if you know to do that, then why bother with the interrupt.
Have a look at how millis() is used to manage timing without blocking in Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. That way it is easy to check for and respond to incoming data.
...R
The only useful interrupts that don't deal with pins that I know of are timer interrupts. They indeed interrupt other commands, but the program will go right back to where it was when the ISR is finished.
The only useful interrupts that don't deal with pins that I know of are timer interrupts.
Well what about the interrupts when the A/D has finished a conversion, or the pin change interrupt that will go off when any pin changes state?
Or the watch dog interrupt or the brownout interrupt? The UASART empty or full, the SPI transfer complete, the EEPROM ready or the Analog Comparator interrupt? Or the I2C interrupt and finally the Store Program Memory Ready interrupt?
Mind you the original question
I am working on a project that uses interrupt without using pins
Does sound like homework.
Good I added "that I know of" there But don't the pin change interrupts have to do with the pins? Would make those not qualify. Most others are also I/O related and arguably work with the pins - depending on how strict the requirement is. That still leaves the watchdog timer, the store program memory ready and the EEPROM ready ones.
It indeed very much sounds like homework. Odd requirement with no description of actual use case.
The problem is, OP mentions that it is upon receiving data which has to go through the pins, but the interrupt can not use pins.
The thing is that I am using five(5) audio samples which will be played one at a time.
The sample will play when a specific data will be received(from Serial).
The audio is stored in PortC played using DAC(R2R resistor ladder).
I tried using if statements with delay() and the sample is inaudible, I guess time has to do with this.
So what I want to do is that when the data is received, interrupt will initiate, reset the timer and play the audio sample.
Isn't it a lot easier (and better sound quality) to use a WAV player or real ADC for this? Of course timing is critical if you want to do this.
wvmarle:
Isn't it a lot easier (and better sound quality) to use a WAV player or real ADC for this? Of course timing is critical if you want to do this.
I agree, however the sample is small that it can be stored in the mcu without using external card. Also, I will not be using an SD card because I don't have one( I ordered from e-bay last year but never heard about my order yet) so I came up with this option. Another thing is that I can't use Serial mp3-module because I am alreadt using two serials (SoftwareSerial & AltSoftSerial) for a single arduino mega 2560.
You have the three built-in hardware serial ports in use as well then?
I am using Software and AltSoftSerial. But I am not using the hardware serials. I have read that Serial cannot be done simultineously so I didn't bother using the serial. Is it possible to do it with the hardware serial?
You have three hardware Serial ports so you should be able to connect and use three Serial devices at the same time without any issues. That's what they are for.
What makes you think you can not use more than one hardware Serial, but instead you can bitbang two software Serial ports on a 16 MHz processor?
Also you should be able to set up more than one SoftwareSerial connection, no need to mess around with multiple libraries for that.
asymptote:
I tried using if statements with delay() and the sample is inaudible, I guess time has to do with this.
So what I want to do is that when the data is received, interrupt will initiate, reset the timer and play the audio sample.
Maybe time to show the code?
The audio is stored in PortC
No it is not.
PortC is an output port it can not store anything.
I tried using if statements with delay() and the sample is inaudible, I guess time has to do with this.
What ever it is it is not time that is the problem. You need to post a schematic of how this D/A is wired up to the Arduino and what you have connected it to.
asymptote:
I am using Software and AltSoftSerial. But I am not using the hardware serials. I have read that Serial cannot be done simultineously so I didn't bother using the serial. Is it possible to do it with the hardware serial?
As others have pointed out you have it the wrong way round, it is two instances of software emulators of the serial port that you can't have running at the same time, all the hardware serial ports will work at the same time.
The sample will play when a specific data will be received(from Serial).
What sort of command? And why did you think an interrupt would help you? It won't. For most applications interrupts offer little advantage over polling?
when the data is received, interrupt will initiate, reset the timer and play the audio sample.
No that is not how interrupts work.
@sterretje The audio sample is in ".h" file
const PROGMEM byte back[] = { 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7F, ....... };
(the whole code is too long so I leave it that way)
This is how I sampled it one by one:
#include "back.h"
void setup() {
// put your setup code here, to run once:
DDRC = 255;
}
short i = 0;
void loop() {
// put your main code here, to run repeatedly:
PORTC = pgm_read_byte(&(back[i++]));
if ( i >= sizeof(back))
i = 0;
delayMicroseconds(62);
}
If I use simple if statement it'll work, however if I use delay the sample is inaudible. I know it has to do with time but I don't know exactly what to do.
#include "back.h"
#include "up.h"
void setup() {
// put your setup code here, to run once:
DDRC = 255;
}
short i = 0;
void loop() {
// put your main code here, to run repeatedly:
UP();
delay(1000);
BACK();
}
void BACK() {
PORTC = pgm_read_byte(&(back[i++]));
if ( i >= sizeof(back))
i = 0;
delayMicroseconds(62);
}
void UP() {
PORTC = pgm_read_byte(&(up[i++]));
if ( i >= sizeof(up))
i = 0;
delayMicroseconds(62);
}
@Grumpy_Mike I connected R2R ladder in PORTC of arduino mega.
What ever it is it is not time that is the problem.
If it has nothing to do with it, then why can't I hear when I use delay?
asymptote:
#include "back.h"
#include "up.h"
void setup() {
// put your setup code here, to run once:
DDRC = 255;
}
short i = 0;
void loop() {
// put your main code here, to run repeatedly:
UP();
delay(1000);
BACK();
}
void BACK() {
PORTC = pgm_read_byte(&(back[i++]));
if ( i >= sizeof(back))
i = 0;
delayMicroseconds(62);
}
void UP() {
PORTC = pgm_read_byte(&(up[i++]));
if ( i >= sizeof(up))
i = 0;
delayMicroseconds(62);
}
That code appears to write a single byte to PortC, wait 62us, then wait another 1000,000us, write another byte from a different data array, wait 62us and go back to the start. I'm not surprised you can't hear anything. The highest frequency this can produce is 0.5Hz. Well below the limits of even an elephant's hearing.
I can't see any attempt there to read any commands from Serial.
Can you show the code which does play one of the sounds? Can you show your attempt to read from Serial? What is the actual command being sent?
of arduino mega
So now you tell us. Quite honestly I wonder why you expect us to know vital pieces of information like that?
Holding back on information wastes everybody’s time. And you have still not told us what the heck that ladder is connected to.
SoftwareSerial BTSerial(12, 13);
void setup() {
// put your setup code here, to run once:
DDRC = 255;
BTSerial.begin(38400);
}
short i = 0;
void loop() {
// put your main code here, to run repeatedly:
if (BTSerial.available() > 0) {
state = BTSerial.read();
}
if (state == '1') {
UP();
delay(1000);
}
else if (state == '2') {
BACK();
delay(1000);
}
else if (state == '3') {
DOWN();
delay(1000);
}
}
BACK() {
PORTC = pgm_read_byte(&(back[i++]));
if ( i >= sizeof(back)) {
i = 0;
delayMicroseconds(62.5);
}
}
UP() {
PORTC = pgm_read_byte(&(up[i++]));
if ( i >= sizeof(up)) {
i = 0;
delayMicroseconds(62.5);
}
}
DOWN() {
PORTC = pgm_read_byte(&(up[i++]));
if ( i >= sizeof(up)) {
i = 0;
delayMicroseconds(62.5);
}
}
This is the code and I attached the connections to the arduino mega.
I am using a bluetooth HC05 to get data then for a different value of data outputs different audio sample.
It is about time you came clean and say what you are trying to do here and why.
That code is simply a nonsense, what do you think it will do? It will not output samples that is for sure.
Also you can’t connect the output of that ladder to drive headphones. If you can hear anything from those headphones then you have not connected them directly their must be an amplifier involved somewhere.