Thanks for all your tips and feedback they helped a lot. I have been trying my best to finish this project but i have another problem with the Atmege8. When i try to turn and of a couple of leds in my main code they emit very dim light, but when I strip all the other code out and only upload just the blink part the glow just as bright as when i couple them to 5v. here is my main code:
#include "functions.h"
#include <avr/interrupt.h>
#include <avr/io.h>
void setup() {
Serial.begin(300);
//Status LED's
DDRD |= (1 << LED_Low);
DDRD |= (1 << LED_High);
DDRD |= (1 << LED_OK);
DDRD |= (1 << LED_DIS);
//Mux pins
DDRB |= (1 << MUX_0);
DDRB |= (1 << MUX_1);
DDRB |= (1 << MUX_2);
DDRC |= (0 << ThermistorPin); //Thermistor pin
DDRB |= (1 << Buzzer); //Buzzer
//BMSPinSelect
// interrupt setup
GICR |= (1 << INT0); // enable INT0;
MCUCR &= ~((1 << ISC01) | (1 << ISC00)); // INT0 low level trigger
sei(); // enable global interrupt
DDRD = 0; // all PD pins configured as input
PORTD = (1 << PD2); // enable pull-up on the INT0 pin
}
void loop() {
//Measure all the sensors
//Thermistor();
//Battery();
//SmokeDetector();
//Check all the values if they are good.
//Check();
PORTD|=(1<<LED_Low);
delay(1000);
PORTD|=(1<<LED_High);
delay(1000);
PORTD|=(1<<LED_OK);
delay(1000);
PORTD|=(1<<LED_DIS);
delay(1000); // wait for a second
PORTD&=~(1<<LED_Low);
delay(1000);
PORTD&=~(1<<LED_High);
delay(1000);
PORTD&=~(1<<LED_OK);
delay(1000);
PORTD&=~(1<<LED_DIS);
delay(1000); // wait for a second
}
//Interupt loop
ISR (INT0_vect) {
// Serial.println("Interupt detected");
Serial.print("*");
for (byte dataCount = 0; dataCount <= 7; dataCount++) {
Serial.print(0);
Serial.print(":"); //Module number and thermistor pin separator symbol
Serial.print(dataCount);
Serial.print("="); //Thermistor pin and thermistor value separator symbol
Serial.print(ThermistorValue[dataCount]);
Serial.print(";"); //End of thermistor value
}
//Error codes
Serial.println(WarningStateTemparature); //Print the warningstate of the temparature. 0 = OK, 1 = LOW, 2 = HIGH
Serial.println(WarningStateVolage); //Print the warningstate of the voltage. 0 = OK, 1 = LOW, 2 = HIGH, 3 = Discharging
Serial.println(WarningStateSmoke); //Print the warningstate of the smoke sensor 0 = OK, 1 = SMOKE
Serial.print("$"); //End of message separator symbol
// prevent interrupt handler from finishing
_delay_ms(100); // not good !!!
while (!(PIND & (1 << PD2))); // not good !!!
_delay_ms(100); // not good !!!
}
and the funtcions.h
//LED Pins
const byte LED_Low = PD4;
const byte LED_High = PD5;
const byte LED_OK = PD6;
const byte LED_DIS = PD7;
//Multiplexer
const byte MUX_0 = PB2;
const byte MUX_2 = PB0;
const byte MUX_1 = PB1;
int Mux_Var_0 = 0; //value of select pin at the 4051 (s0)
int Mux_Var_1 = 0; //value of select pin at the 4051 (s1)
int Mux_Var_2 = 0; //value of select pin at the 4051 (s2)
byte MuxCount = 0;
//Thermistor
float ThermistorValue[8];
const byte ThermistorPin = PC2;
//add or subtract the value //0 1 2 3 4 5 6 7
float ThermistorCalibration[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
//Battery thermistors
byte ThermistorBatt[] = {0, 1, 2, 3};
//Battery temparture
byte TempBattHigh = 40;
byte TempBattHighCutoff = 45;
byte TempBattLow = 5;
byte TempBattLowCutoff = 0;
const byte Buzzer = PB6;
byte Alarm = 0;
//Optocoupler
const byte OPTO_IN = PB7;
bool OPTO_IN_State = 0;
//Warningstates
byte WarningStateTemparature = 0;
byte WarningStateVolage = 0;
byte WarningStateSmoke = 0;
//Thermistor calculations:
float readingToTemp(float reading) {
//Divide by 1023 for the right units
float waarde = 1023 / reading - 1;
//Divide by resistor value
waarde = 10000 / waarde;
waarde = waarde / 10000;
waarde = log(waarde);
waarde /= 3950;
waarde += 1.0 / (21 + 273.15);
waarde = 1.0 / waarde;
waarde = waarde - 273.15;
return waarde;
}
void alarm() {
for (byte BuzzerCount = 1; BuzzerCount <= 4; BuzzerCount++) {
for (Alarm = 0; Alarm <= 8; Alarm++) {
PORTB |= (1 << Buzzer);
delay(10);
PORTB &= ~(1 << Buzzer);
delay(50);
}
delay(100);
}
}
void Thermistor() {
//start multiplexer forloop
for (MuxCount = 0; MuxCount <= 7; MuxCount++) {
// select the bit
Mux_Var_0 = bitRead(MuxCount, 0);
Mux_Var_1 = bitRead(MuxCount, 1);
Mux_Var_2 = bitRead(MuxCount, 2);
digitalWrite(MUX_0, Mux_Var_0);
digitalWrite(MUX_1, Mux_Var_1);
digitalWrite(MUX_2, Mux_Var_2);
//Reset Thermistor value
ThermistorValue[MuxCount] = 0;
//start thermistor forloop
for (byte ThermistorCount = 0; ThermistorCount <= 7; ThermistorCount++) {
//Read the analog pin 8 times and add them up
ThermistorValue[MuxCount] = ThermistorValue[MuxCount] + analogRead(ThermistorPin);
delay(200);
}
//Divide the themistor value by 8 to get the average
ThermistorValue[MuxCount] = ThermistorValue[MuxCount] / 8;
ThermistorValue[MuxCount] = readingToTemp(ThermistorValue[MuxCount]);
ThermistorValue[MuxCount] = ThermistorValue[MuxCount] + ThermistorCalibration[MuxCount];
}
}
void Battery () {
}
void SmokeDetector() {
}
void Check () {
//Temperature check
for (byte Thermistorcheck = 0; Thermistorcheck <= 7; Thermistorcheck++) {
//Check if temperature is above temp threshold.
if (ThermistorValue[Thermistorcheck] >= TempBattHigh) {
alarm();
WarningStateTemparature = 2;
PORTD|=(1<<LED_High);
}
//Check if temperature is below temp threshold
if (ThermistorValue[Thermistorcheck] <= TempBattLow) {
alarm();
WarningStateTemparature = 1;
PORTD|=(1<<LED_Low);
}
if (ThermistorValue[Thermistorcheck] >= TempBattLow && ThermistorValue[Thermistorcheck] <= TempBattHigh) {
WarningStateTemparature = 0;
PORTD|=(1<<LED_OK);
}
}
//Voltage check
//Smoke detector check
}
And here is my striped down code:
//LED Pins
const byte LED_Low = PD4;
const byte LED_High = PD5;
const byte LED_OK = PD6;
const byte LED_DIS = PD7;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
DDRD |= (1 << LED_Low);
DDRD |= (1 << LED_High);
DDRD |= (1 << LED_OK);
DDRD |= (1 << LED_DIS);
}
// the loop function runs over and over again forever
void loop() {
PORTD|=(1<<LED_Low);
delay(1000);
PORTD|=(1<<LED_High);
delay(1000);
PORTD|=(1<<LED_OK);
delay(1000);
PORTD|=(1<<LED_DIS);
delay(1000); // wait for a second
PORTD&=~(1<<LED_Low);
delay(1000);
PORTD&=~(1<<LED_High);
delay(1000);
PORTD&=~(1<<LED_OK);
delay(1000);
PORTD&=~(1<<LED_DIS);
delay(1000); // wait for a second
}
Its just the same but without the rest of my code which it shouldn't interfere with it.
In the main code i even comment out the functions and still it wont work.
If you want i can post pictures of the led's but the difference is night and day.
I hope some can help just as good as with the previous problem.