Go Down

Topic: [Cerebot] blinking several led (structure & interrupt needed) (Read 885 times) previous topic - next topic

GiovanniLudovico

Apr 03, 2013, 02:58 am Last Edit: Apr 03, 2013, 10:19 am by GiovanniLudovico Reason: 1
hi, i wrote a code for my Cerebot,
it should light a led that blinks when i press a button and turn it off when i press again
i wrote a simple edition with just one led and it works fine,
i added a led and it gives nothing . i want to use it to light my apartment, so i need to make it work with more than one led (they are not simple led, but groups of them that we consider as one).
here comes the one that doesn't work:

Code: [Select]
//note that the two buttons are connected to the interrupt pin AND on the pull up
int puls_led[2]={42,43};// my two button on my cerebot
int led[2]={64,65};// two of my four leds

typedef struct {
 int puls;
 volatile int stato;
} utile;

utile stanza[2]; //struct that represent my two rooms (actually my two on board leds)


int i,j;


void setup() {                
attachInterrupt(EXT_INT0, controllo ,RISING);


 pinMode(puls_led[0],INPUT);
 pinMode(puls_led[1],INPUT);

 pinMode(led[0],OUTPUT);
 pinMode(led[1],OUTPUT);

 stanza[0].puls =LOW;
 stanza[1].puls =LOW;

 

}
void loop() {
 delay(100); // this delay generate a sort of clock when i want to see the light of the led
 for (i=0;i==1;i++)
 {
   if(stanza[i].puls==1)// if i want it (see the interrupt later)
 {
   stanza[i].stato =! stanza[i].stato; //change to generate the clock
   digitalWrite(led[i],stanza[i].stato);
 }
   else
   digitalWrite(led[i],0);//if i dont want it, stay calm
   }
}
void controllo()
{
 for(j=0;j==1;j++)

{
 if (digitalRead(puls_led[j])==1)// discover wich on of the two buttons has generated the interrupt
 stanza[j].puls =! stanza[j].puls;// i want the light on on that led!
}
}
 


here the one that works
Code: [Select]
int puls_led=42;
int led=64;

typedef struct {
 int puls;
 volatile int stato ;
 int ratio;
} utile;

utile stanza;



int i,j;


void setup() {                
attachInterrupt(EXT_INT0, controllo ,RISING);
pinMode(puls_led,INPUT);

pinMode(led,OUTPUT);
stanza.stato = LOW;

}

void loop() {
 
 delay(100);

   if(stanza.puls==1)
 {
   stanza.stato =! stanza.stato;
   digitalWrite(led,stanza.stato);
 }
 else
 digitalWrite(led,0);
   
}
void controllo()
{
 if (digitalRead(puls_led)==1)
 stanza.puls =! stanza.puls;

}


can you help me? maybe i make something simple wrong!
THANK YOU

GiovanniLudovico

solved! right code here (the for cicle was wrong :~)
Code: [Select]
//note that the two buttons are connected to the interrupt pin AND on the pull up
int puls_led[2]={42,43};// my two button on my cerebot
int led[2]={64,65};// two of my four leds

typedef struct {
  int puls;
  volatile int stato;
} utile;

utile stanza[2]; //struct that represent my two rooms (actually my two on board leds)


int i,j;


void setup() {               
attachInterrupt(EXT_INT1, controllo ,RISING);


  pinMode(puls_led[0],INPUT);
  pinMode(puls_led[1],INPUT);

  pinMode(led[0],OUTPUT);
  pinMode(led[1],OUTPUT);

  stanza[0].puls =LOW;
  stanza[1].puls =LOW;

 

}
void loop() {
  delay(10); // this delay generate a sort of clock when i want to see the light of the led
  for (i=0;i<=1;i++)
  {
    if(stanza[i].puls==1)// if i want it (see the interrupt later)
  {
    stanza[i].stato =! stanza[i].stato; //change to generate the clock
    digitalWrite(led[i],stanza[i].stato);
  }
    else
    digitalWrite(led[i],0);//if i dont want it, stay calm
    }
}
void controllo()
{
 
  for(i=0;i<=1;i++)

{
  if (digitalRead(puls_led[i])==HIGH)// discover wich on of the two buttons has generated the interrupt
  {
    stanza[i].puls =! stanza[i].puls;// i want the light on on that led!
   
  }
}

}
 

Go Up