Skip to content

rs_blinker.ino does not work on Elegoo Mega 2560 R3 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
joshuasv opened this issue Dec 2, 2024 · 0 comments
Open

rs_blinker.ino does not work on Elegoo Mega 2560 R3 #1

joshuasv opened this issue Dec 2, 2024 · 0 comments

Comments

@joshuasv
Copy link

joshuasv commented Dec 2, 2024

The code provided in the rs-blinker.ino does not work on my Elegoo Mega 2560 R3. It did not switch the LED. After a bit of tweaking I got it working, code below.

Question. Do you see any faults on this code that may not yield the intended results?

#define LED_PIN 2 // Pin number for the onboard LED (Arduino Uno)

const unsigned long long freq = 50; // So I can see it

void setup() {
  pinMode(LED_PIN, OUTPUT); // Set the LED pin as output

  // Configure Timer1
  cli(); // Disable global interrupts
  
  TCCR1A = 0; // Set Timer1 to Normal mode (no PWM)
  TCCR1B = 0; // Clear Timer1 control register B
  
  // Set compare match register for 1Hz (1-second interval)
  // OCR1A = 15624; // (16 MHz / (1024 * 1Hz)) - 1
  OCR1A = (F_CPU) / (1024 * freq) - 1; 

  
  // Set prescaler to 1024 and enable CTC mode
  TCCR1B |= (1 << WGM12); // Configure for CTC mode
  TCCR1B |= (1 << CS12) | (1 << CS10); // Set prescaler to 1024

  // Enable Timer1 compare interrupt
  TIMSK1 |= (1 << OCIE1A);

  sei(); // Enable global interrupts
}

// Interrupt Service Routine for Timer1 Compare Match A
ISR(TIMER1_COMPA_vect) {
  digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle LED
}

void loop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant