Changing boards issue

I'm doing a school project, and i have tested the code with arduino uno and nano (atmega328p) and it worked perfectly, but the code don't work with arduino mega2560 wich i will need to use because of memory issue, here is the code for arduino nano/uno that i used:

#define IRpin 2
#define MAXPULSE 65000
#define RESOLUTION 20

int IRledPin = 12;

bool receptor = false;

uint16_t pulses[250][2];
uint8_t currentpulse = 0;
uint8_t pulse_size = 0;

void setup()
{
  pinMode(IRledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  {
    char comando = Serial.read();

    if (comando == '0')
    {
      receptor = false;

      Serial.write('0');
      //Serial.println("Do nothing...");
    }

    if (comando == '1')
    {
      receptor = false;

      Serial.write('1');
      //Serial.println("Now sending...");

      SendChannelUpCode();
      delay(50);
    }

    if (comando == '2')
    {
      receptor = true;

      Serial.write('2');
      //Serial.println("Now receiving...");
    }
  }

  if (receptor == true)
  {
    receive();
  }
}

//==============================================================

void receive(void)
{
  uint16_t highpulse, lowpulse;
  highpulse = lowpulse = 0;

  while (IRpin_PIN & _BV(IRpin))
  {
    highpulse++;
    delayMicroseconds(RESOLUTION);

    if ((highpulse >= MAXPULSE) && (currentpulse != 0))
    {
      printpulses();
      pulse_size = currentpulse;
      currentpulse = 0;

      receptor = false;
      Serial.write('d');

      return;
    }
  }

  pulses[currentpulse][0] = highpulse;

  while (!(IRpin_PIN & _BV(IRpin)))
  {
    lowpulse++;
    delayMicroseconds(RESOLUTION);

    if ((lowpulse >= MAXPULSE) && (currentpulse != 0))
    {
      printpulses();
      pulse_size = currentpulse;
      currentpulse = 0;

      receptor = false;
      Serial.write('d');

      return;
    }
  }

  pulses[currentpulse][1] = lowpulse;
  currentpulse++;
}

//==============================================================

void printpulses(void)
{
  Serial.println(currentpulse);

  for (uint8_t i = 0; i < currentpulse; i++)
  {
    Serial.print("delay: ");
    Serial.println(pulses[i][0] * RESOLUTION, DEC);
    Serial.print("pulse: ");
    Serial.println(pulses[i][1] * RESOLUTION, DEC);
  }

  pulse_size = currentpulse;
}

//==============================================================

void pulseIR(long microsecs)
{
  cli();

  while (microsecs > 0)
  {
    digitalWrite(IRledPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(IRledPin, LOW);
    delayMicroseconds(10);
    microsecs -= 26;
  }

  sei();
}

//==============================================================

void SendChannelUpCode(void)
{
  for (uint8_t i = 0; i < pulse_size; i++)
  {
    delayMicroseconds(pulses[i][0] * RESOLUTION);
    pulseIR(pulses[i][1] * RESOLUTION);
  }

  Serial.write('d');
}

note: I'm using on pin 2 a IR sensor, VS1838, and on pin 12 an ir led. To adapt for mega i've tried to switch PIND to one of the GPIOs of the microcontroller (PINA, PING, PIND, PINE...) and i also have changed the pin on #define IRpin, but it didn't work.

this is not working code, neither UNO nor MEGA.
the sketch, did you write it by yourself?

Did you copy the top #define:

#define IRpin_PIN PIND

The code compiles with the lost #define... I don't know if it works

You didn't define 'IRpin_PIN' so your sketch will not compile for UNO or Mega.

If you accidentally left out:
#define IRpin_PIN PIND
when you copied the code for posting, you will need to use different values on the Mega. In the UNO, Pin 2 is PD2 but on the Mega it's PE4 so you would need:

#define IRpin_PIN PINE
#define IRpin 4

Of course the 'IRpin' value no longer matches the Arduino pin number so you might be better off using:

#define IRpin 2
#define IRpin_PIN PINE
#define IRpin_BIT PE4

and change:
while (IRpin_PIN & _BV(IRpin))
to
while (IRpin_PIN & _BV(IRpin_BIT))

Yes that was the problem, the fact was that it should be _BV(4) instead of _BV(2) (pin 2 of arduino mega) thank you.

Half of it, it was originaly 2 codes to read and send an IR signal, and I put both together, it did work on UNO and NANO, but the problem was actually the bit on _BV(), so now it works on MEGA too, thank you anyway.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.