Hello, Guys.
I need a bomb for strike boll game.
In this code many mistakes and troubles i know.
Can any one say me why timer1 not starting, why int0 reaches after power on, why this not sleep.
#include <TimerOne.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#define FALSE false
const int service_lock_pin = 14; // в atmega8 это PC0
const int start_pin = 2; //int0
const int explode_pin = 15; //PC1
const int faster_pin = 16;
const int stop_pin = 17;
const int led_pin = 18;
const int relay_pin = 19;
int freq = 1;
int slp = 0;
int ep = 0;
int fp = 0;
int sp = 0;
int neutralized = false;
volatile bool b_working = FALSE;
volatile int count = 0;
byte port = NULL;
bool flash = false;
void setup()
{
//выполним настройку портов.
pinMode (service_lock_pin,INPUT); //HIGH=lock
pinMode (start_pin,INPUT); //LOW=start, wakeup( int0)
pinMode (explode_pin,INPUT); //LOW=explode
pinMode (faster_pin, INPUT); //LOW=faster
pinMode (stop_pin,INPUT); //LOW=stop, sleep
pinMode (led_pin,OUTPUT); //work indication
pinMode (relay_pin,OUTPUT); //relay device
b_working = false;
neutralized = false;
digitalWrite (relay_pin,LOW);
digitalWrite(led_pin,LOW);
cli();
Timer1.initialize (100);
Timer1.attachInterrupt(Timer1_ISR);
Timer1.stop();
sei();
}
void Timer1_ISR (void)
{
count++;
flash = !flash;
digitalWrite(led_pin,flash);
if (count>=60)
{
Timer1.stop();
detachInterrupt(0);
digitalWrite(relay_pin,HIGH);
digitalWrite(led_pin,HIGH);
b_working = false;
return;
}
}
void wakeUpNow(void)
{
sleep_disable();
detachInterrupt(0);
b_working = true;
//digitalWrite(led_pin,HIGH);
Timer1.start();
}
void loop()
{
if (neutralized)
{
delay(1000);
return;
}
sleep_enable();
attachInterrupt(0, wakeUpNow, HIGH);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_cpu();
/* wake up here */
//digitalWrite(led_pin,HIGH);
while (b_working)
{
slp = digitalRead(service_lock_pin);
ep = digitalRead(explode_pin);
fp = digitalRead(faster_pin);
sp = digitalRead(stop_pin);
if (ep==LOW)
{
//digitalWrite(relay_pin,HIGH);
//b_working = false;
//return;
}
if (sp==LOW)
{
//neutralized = true;
//b_working = false;
//return;
}
if (fp==LOW)
{
//Timer1.stop();
//Timer1.setPeriod(500000);
//Timer1.start();
}
}
sleep_disable();
}
this code should initialize and goto suspend mode. After int0 we need start 30 seconds timer and after - 'boom'. one wire we need to restart timer on 2x frequency. other one must 'boom' now. and other one must neutralize the bomb.
ok. start from the beginning . I need to interrupt a high level . Customizable with your foot on the input pull-up and connect it to ground via a switch. If the connection to the ground at the foot of the interrupt should be set high.
Again, it all happens in Proteus !!!
Prerevanie fires right after the power is turned on .
const int service_lock_pin = 14; // в atmega8 это PC0
const int start_pin = 2; //int0
const int int2_pin = 3;
const int explode_pin = 15; //PC1
const int faster_pin = 16; //PC2
const int stop_pin = 17; //PC3
const int led_pin = 18; //PC4
const int relay_pin = 19; //PC5
void int0_interrupt(void)
{
Serial.println("int0 on high level");
}
void setup()
{
pinMode (service_lock_pin,INPUT); //HIGH=lock
pinMode (start_pin,INPUT_PULLUP); //LOW=start, wakeup( int0)
pinMode (explode_pin,INPUT); //LOW=explode
pinMode (faster_pin, INPUT); //LOW=faster
pinMode (stop_pin,INPUT); //LOW=stop, sleep
pinMode (led_pin,OUTPUT); //work indication
pinMode (relay_pin,OUTPUT); //relay device
pinMode (int2_pin,INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Initializing...OK");
Serial.println("Adding Int0 unterrupt.");
attachInterrupt(0,int0_interrupt,HIGH);
}