I want to change this code from attiny13 to arduino uno

#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>

volatile unsigned char prev_input=0, input=0;
volatile unsigned char flag=0, pot=0;

ISR(PCINT0_vect) {
  prev_input = input;
  input = PINB;
  flag=1;
}
ISR(ADC_vect){
  pot = ADCH;
}
void adc_setup (void){
  // Set the ADC input to PB2/ADC1
  ADMUX |= (1 << MUX0);
  ADMUX |= (1 << ADLAR);
  // Set the prescaler to clock/128 & enable ADC & ADC interrupts & Auto-trigger enabled
  ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0) | (1 << ADEN) | (1 << ADIE) | (1 << ADATE);
  // (Auto-trigger) conversion will start by Pin Change Interrupt Request
  ADCSRB |= (1 << ADTS2) | (1 << ADTS1);
  // disable digital input pin
  DIDR0 |= 0x04;
}

int main() {
  DDRB = 0b00000010;
  PORTB = 0b00011111;
  PCMSK |= (1<<PCINT0);	// pin change mask
  GIMSK |= (1<<PCIE);	// enable PCINT interrupt
  sei(); // enable Global interrupts
  adc_setup();

  while (1) {
     if(flag) {
       flag=0;
       if(!(input&0x01)){
         char unsigned n;
         for(n=pot;n>0;n--){
           _delay_us(40);
         } 
	 if(!flag && !(input&0x01)){
           PORTB &= 0xfd;
	   if(pot<100){
             _delay_us(1000);
	   } else {
             _delay_us(100);
	   }
           PORTB |= 0x02;
	 }
       }
     }
  }
  return 0;
}

this code for dimmer

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Please edit your post, select all code and click the <CODE/> button; next save your post. This will make your code easier to read, easier to copy and the forum software will display it correctly.


Have you tried to compile your code for an Uno? What are the results? If you got error messages, please post them using code tags.

The pin change interrupt registers in the Atmega328 have other names than in the Attiny13

When you change this:

PCMSK |= (1<<PCINT0);// pin change mask
GIMSK |= (1<<PCIE);  // enable PCINT interrupt

Into this:

PCMSK0 |= (1<<PCINT0);  // pin change mask
PCICR  |= (1<<PCIE0);    // enable PCINT interrupt

It will compile, but you will have to figure out finding and setting the pins correct yourself. I have no time to do that for you.

1. RV2 is your Dimmer -- Correct?

2. You roatate the Pot RV4; as a result, the brightness of your Dimmer channges; correct?

3. Now, briefly describe the working principle of the circuit that you have posted in post #1.

4. How much is V1 -- is it 110V or 220V?

5. How have you drived 5V supply for your ATtiny13?

6. Is everything in the 110V/220V domain or is there any isolation?

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