Arduino Locker

The goal of this project is to make a device that can open my school locker at the press of a button. I plan on using two adafruit trinkets. When the button on the remote is pressed, a 555 timer (monostable) will allow voltage to pass to the trinket, which will send the signal over IR to the receiver, which basically works the same, but in reverse. Here is the code, let me know if it will work and how I might improve it. REMOTE:

int irPin = 0;							//Ir LED
int code[] = {0,1,0,0,1,1,1,0,1,0};		//array of values to by transmitted

void setup(){
	pinMode(irPin,OUTPUT);
	digitalWrite(irPin,HIGH);			//Send a long pulse to wake up the receiver
	delay(100);
	digitalWrite(irPin,LOW);
}

void loop(){
	for(x=1;x<=10;x++){
		if (code[x]==1){				//the for loop goes through the array and transmitts each value
			digitalWrite(irPin,HIGH);
		}
		else{
			digitalWrite(irPin,LOW);
		}
	}
	delay(1000000);						//wait until the timer capacitor keeping the power supply on turns off
}

RECEIVER:

int irPin = 0;								//IR decoder
int m1Pin = 1;								//Motor trigger transistor
int test = 0;								//store value of irPin to this
int check = 0;								//value of code received
int code[] = {0,2,2,2,7,13,20,20,29,29};	//code received = {0,1,0,0,1,1,1,0,1,0}

void setup()
{
	pinMode(m1Pin, OUTPUT);
	pinMode(irPin, INPUT);
	while (digitalRead(irPin)==1){			//wait for the long "wake up" pulse to end
		delay(1);
	}
}

void loop()
{
	for(int x = 1; x <= 10; x++){			//go through 10 cycles
		test = digitalRead(irPin);			//read the ir decoder
		check = check + (x * test);			//add tested value to the checksum
		if (check != code[x]){				//If the checksum doesn't match the set code
			break;							//exit the for loop
		}
		if (x == 10 and check == code[x]){	//if it's the last cycle and the checksum stil matches the code
			digitalWrite(m1Pin,HIGH);		//open the locker
		}
	}
	delay(1000000);							//wait until the timer capacitor keeping the power supply on turns off
}

Thanks!

for(x=1;x<=10;x++){

will fail as indices are in C zero based. Use

for(x=0;x<10;x++){

Does the 555 really save more power than putting the Trinkets to sleep and waking them on interrupt?

tylernt: Does the 555 really save more power than putting the Trinkets to sleep and waking them on interrupt?

how would I do that?

robtillaart:
for(x=1;x<=10;x++){

will fail as indices are in C zero based. Use

for(x=0;x<10;x++){

thanks!

Hex4d617474:

tylernt: Does the 555 really save more power than putting the Trinkets to sleep and waking them on interrupt?

how would I do that?

http://www.gammon.com.au/forum/?id=11497

http://www.gammon.com.au/forum/?id=11488

tylernt:

Hex4d617474:

tylernt:
Does the 555 really save more power than putting the Trinkets to sleep and waking them on interrupt?

how would I do that?

Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors

Gammon Forum : Electronics : Microprocessors : Interrupts

Alright thanks! I’ll write an update when I have time to change the code

Sorry for the delay, but school has a way of slowing down everything else in life… Anyways here’s the updated code with interrupts, let me know if it looks alright!

Locker:

int irPin = 0; //IR decoder
int m1Pin = 2; //Motor trigger transistor
int test = 0; //store value of irPin to this
int check = 0; //value of code received
int code[] = {0,2,2,2,7,13,20,20,29,29}; //code received = {0,1,0,0,1,1,1,0,1,0}
const int ledpin=1; //led for debugging purposes
volatile boolean flag;

void isr () // Interrupt Service Routine (ISR)
{
 while (digitalRead(irPin)==1){ //wait for the long "wake up" pulse to end
 delay(1);
 for(int x = 0; x <= 9; x++){ //go through 10 cycles
 test = digitalRead(irPin); //read the ir decoder
 check = check + (x * test); //add tested value to the checksum
 if (check != code[x]){ //If the checksum doesn't match the set code
 break; //exit the for loop
 }
 if (x == 9 and check == code[x]){ //if it's the last cycle and the checksum stil matches the code
 digitalWrite(m1Pin,HIGH); //open the locker
 }
 }
 flag = true; //ISR finished
}

void setup ()
{
 pinMode(m1Pin, OUTPUT);
 pinMode(irPin, INPUT);
 pinMode(ledpin,OUTPUT);
 attachInterrupt (0, isr, CHANGE);   // attach interrupt handler
}

void loop ()
{
  if (flag) //If the ISR executed
    {
     digitalWrite(ledpin,HIGH); //light the LED
 
    }
 set_sleep_mode (SLEEP_MODE_PWR_DOWN);   //Shut off
  sleep_enable();
  
  sleep_cpu()
}

Remote:

int irPin = 0; //IR decoder
int buttonPin = 1; //Unlock Button
int ledPin=2; //led for debugging purposes
int code[] = {0,2,2,2,7,13,20,20,29,29}; //code received = {0,1,0,0,1,1,1,0,1,0}
volatile boolean flag;

void isr () // Interrupt Service Routine (ISR)
{
 for(x=0;x<=9;x++){
 if (code[x]==1){ //the for loop goes through the array and transmitts each value
 digitalWrite(irPin,HIGH);
 }
 else{
 digitalWrite(irPin,LOW);
 }
 }
 flag = true; //ISR finished
}

void setup ()
{
 pinMode(ledPin, OUTPUT);
 pinMode(irPin, OUTPUT);
 attachInterrupt (1, isr, CHANGE);   // attach interrupt handler
}

void loop ()
{
  if (flag) //If the ISR executed
    {
     digitalWrite(ledpin,HIGH); //light the LED
 
    }
 set_sleep_mode (SLEEP_MODE_PWR_DOWN);   //Shut off
  sleep_enable();
  
  sleep_cpu()
}