Atmega32 outputs go all ON for a few milliseconds

Thread Starter

idir93

Joined Jan 10, 2018
27
Hello fellas,
I am programming an atmega32 using Arduino as ISP.
The outputs go all HIGH for a few ms which causes system misbehavior for that short moment.
The portion of the code is
Code:
while (goup) {                         //// lift is going up
      Serial.print("current_floor is");
  Serial.println(current_floor, DEC);
  Serial.print("Destination floor is");
  Serial.println(destination_floor, DEC);
  Serial.print("E-floor is");
  Serial.println(e_floor, DEC);
  //delay(500);
         difference = destination_floor - current_floor;
         e_difference = e_destination - e_floor;
         lift_display.showNumberDec(current_floor);
         state_change_DSS(DSS);
         state_change_USS(USS);
         Serial.print("DSS= ");
         Serial.print(buttonPushCounter_DSS, DEC);
         Serial.print("   USS= ");
         Serial.println(buttonPushCounter_USS, DEC);
         if( digitalRead(incremented) == HIGH)
         Serial.println("  incremented is high ");
         else if ( digitalRead(incremented) == LOW)
         Serial.println("  incremented is low ");
         if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && e_difference > 1) ) {
              if(!incremented){
              e_floor++;
              incremented = HIGH;}
         lift_display.showNumberDec(e_floor);
         }
         else if((buttonPushCounter_DSS  % 4 == 0 && buttonPushCounter_USS % 4 == 0 && difference > 1) ) {
              if(!incremented){
              current_floor=2;
              incremented = HIGH;}
         lift_display.showNumberDec(current_floor);
         }
         else if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && difference == 1) ) {
              if(!incremented){
              current_floor++;
              incremented = HIGH;}
         lift_display.showNumberDec(current_floor);
         lift.setAll(UP_LOW);
         }
         else if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && difference == 0) || (buttonPushCounter_DSS  % 3 == 0 && buttonPushCounter_USS % 2 == 0 && difference == 0) || (buttonPushCounter_DSS  % 5 == 0 && buttonPushCounter_USS % 4 == 0 && difference == 0)){
         incremented = LOW;
         lift.setAll(UP_LOW);}
         else if((buttonPushCounter_DSS  % 3 == 0 && buttonPushCounter_USS % 3 == 0 && difference == 0) || (buttonPushCounter_DSS  % 5 == 0 && buttonPushCounter_USS % 5 == 0 && difference == 0)) {
          lift.setAll(off);
          lift_display.showNumberDec(current_floor);
          incremented = LOW;
          goup = LOW;
         }
         else {
         incremented = LOW;
         lift.setAll(UP_HIGH);
         }
The attached gif in slow motion makes it clear.
 

BobaMosfet

Joined Jul 1, 2009
2,211
Don't see a gif to look at. It would help if you would explain the logic in your code, as it sounds like you're running an elevator. Is the code the issue, or is it that you're Arduino isn't behaving as an ISP properly? Or is it because the ATMEGA32 is simply responding to something you're causing it to go high on all pins...?

We all need more details. Schematic would be useful,too.
 

atferrari

Joined Jan 6, 2004
5,016
Two basic questions:

Is there any section of your code that brings them high even just once? You could be entering / calling that part even if with no intention of that happening.

Is it possible to simulate your code? Divide and conquer plus simulation eliminated ghost chasing that in the past was a common thing to me.
 

Thread Starter

idir93

Joined Jan 10, 2018
27
Thank you all for your responses.
1- The Arduino is used just as a programmer for the the Atmega32.
2- I am not causing programmatically the outputs chip to go HIGH, no section in my code that indicates so. The entire code is below.
3- I don't have the circuit with me, really sorry!
4- You guys will find below a YouTube video instead of the gif
Code:
#include <ShiftRegister74HC595.h>
#include <Arduino.h>
#include <TM1637Display.h>
// TM1637
#define CLK 25
#define DIO 26
TM1637Display lift_display(CLK, DIO);
// Buttons
#define BT_UP 27
#define BT_DW 26
#define BT_INS_UP 30
#define BT_INS_DW 31
// Calls
#define CALL_F0 21
#define CALL_F1 22
#define CALL_F2 23
#define CALL_F3 24
// Limits Switches
#define DSS 2
#define USS 1
#define DLS 0
#define ULS 29
// Sensors
#define PHOTOCELL2 12
#define EXT_ALARM1 7
#define EXT_ALARM2 6
#define THERMO 5
#define DOOR_OPEN_BT 4
#define PHOTOCELL1 3
//Security
#define SC2 16
#define SC1 17
#define SC3 18
#define SC4 19
#define SC5 20
// Shift register
#define OUT_DATA 15
#define OUT_STB 14
#define OUT_CLK 13
ShiftRegister74HC595 lift(1, OUT_DATA, OUT_CLK, OUT_STB);
unsigned int current_floor=0, destination_floor;
uint8_t unknown_floor[] = {0b01000000,0b01000000, 0b01000000, 0b01000000};
bool incremented = LOW;
bool goup = LOW;
bool godown = LOW;
bool unknown = LOW;
int  e_floor = 0;
int e_difference = 0;
int e_destination = 0;
// State change function
int buttonPushCounter_DSS = 1;   // counter for the number of button presses
int buttonState_DSS = 0;         // current state of the button
int lastButtonState_DSS = 0;     // previous state of the button
///////////////////
int buttonPushCounter_USS = 1;   // counter for the number of button presses
int buttonState_USS = 0;       // current state of the button
int lastButtonState_USS = 0;     // previous state of the button
///////////////////
int buttonPushCounter_DLS = 1;   // counter for the number of button presses
int buttonState_DLS = 0;       // current state of the button
int lastButtonState_DLS = 0;     // previous state of the button
///////////////////
int buttonPushCounter_ULS = 1;   // counter for the number of button presses
int buttonState_ULS = 0;       // current state of the button
int lastButtonState_ULS = 0;     // previous state of the button
uint8_t off[] = { B00001000 };              ///// not running
uint8_t DW_LOW[] = { B01011000 };            //// Low speed DOWN
uint8_t UP_LOW[] = { B01101000 };            //// Low speed UP
uint8_t UP_HIGH[] = { B10101000 };            //// High speed UP
uint8_t DW_HIGH[] = { B10011000 };            //// High speed DOWN
void setup(){
  pinMode(BT_UP, INPUT); ////Inputs
  pinMode(BT_DW, INPUT);
  pinMode(BT_INS_UP, INPUT);
  pinMode(BT_INS_DW, INPUT);
  pinMode(CALL_F0, INPUT);
  pinMode(CALL_F1, INPUT);
  pinMode(CALL_F2, INPUT);
  pinMode(CALL_F3, INPUT);
  pinMode(USS, INPUT);
  pinMode(DSS, INPUT);
  pinMode(DLS, INPUT);
  pinMode(ULS, INPUT);
  pinMode(PHOTOCELL2 , INPUT);
  pinMode(EXT_ALARM1, INPUT);
  pinMode(EXT_ALARM2 , INPUT);
  pinMode(THERMO , INPUT);
  pinMode(DOOR_OPEN_BT , INPUT);
  pinMode(PHOTOCELL1, INPUT);
  pinMode(SC2, INPUT);
  pinMode(SC1, INPUT);
  pinMode(SC3, INPUT);
  pinMode(SC4, INPUT);
  pinMode(SC5, INPUT);
  pinMode(DIO, OUTPUT);   //// Outputs
  pinMode(CLK, OUTPUT);
  pinMode(OUT_DATA, OUTPUT);
  pinMode(OUT_STB, OUTPUT);
  pinMode(OUT_CLK, OUTPUT);
  Serial.begin(9600);
  lift_display.setBrightness(7);
  lift.setAll(off);
  if( digitalRead(USS)== LOW && digitalRead(DSS)== LOW && digitalRead(ULS)== HIGH && digitalRead(DLS)== LOW )
    {lift_display.showNumberDec(0);
    current_floor=0;}
  else if ( digitalRead(USS)== LOW && digitalRead(DSS)== LOW && digitalRead(ULS)== LOW && digitalRead(DLS)== HIGH )
   { lift_display.showNumberDec(2);
    current_floor=2;}
  else
  {
    lift_display.setSegments(unknown_floor);
    unknown=HIGH;
   }
   if(unknown == HIGH) {
  while( digitalRead(DLS)== HIGH)
     lift.setAll(DW_HIGH);
     }
    while( digitalRead(DSS)== HIGH)
    lift.setAll(DW_LOW);
    lift_display.showNumberDec(0);
    current_floor=0;
   
   
      }

void loop()
{
  //lift_display.showNumberDec(current_floor);}
  buttonPushCounter_DSS = 1;
  buttonPushCounter_USS = 1;
  lift.setAll(off);
  boolean call = digitalRead(CALL_F0) ||  digitalRead(CALL_F1) || digitalRead(CALL_F2) || digitalRead(CALL_F3);
  if ( digitalRead(CALL_F0)== HIGH){
  destination_floor=0;
  e_destination=0;}
  else if(digitalRead(CALL_F1) == HIGH){
  destination_floor=1;
  e_destination=1;}
  else if(digitalRead(CALL_F2) == HIGH ){
  destination_floor=2;
  e_destination=2;}
  int difference = destination_floor - current_floor;
  if ( difference > 0){
  goup= HIGH;
  godown=LOW;}
  else if ( difference < 0){
  goup= LOW;
  godown=HIGH;}
  Serial.print("current_floor is");
  Serial.println(current_floor, DEC);
  Serial.print("Destination floor is");
  Serial.println(destination_floor, DEC);
  //delay(500);
  switch (call) {
  case HIGH:
  lift_display.showNumberDec(current_floor);
     while (goup) {                         //// lift is going up
      Serial.print("current_floor is");
  Serial.println(current_floor, DEC);
  Serial.print("Destination floor is");
  Serial.println(destination_floor, DEC);
  Serial.print("E-floor is");
  Serial.println(e_floor, DEC);
  //delay(500);
         difference = destination_floor - current_floor;
         e_difference = e_destination - e_floor;
         lift_display.showNumberDec(current_floor);
         state_change_DSS(DSS);
         state_change_USS(USS);
         Serial.print("DSS= ");
         Serial.print(buttonPushCounter_DSS, DEC);
         Serial.print("   USS= ");
         Serial.println(buttonPushCounter_USS, DEC);
         if( digitalRead(incremented) == HIGH)
         Serial.println("  incremented is high ");
         else if ( digitalRead(incremented) == LOW)
         Serial.println("  incremented is low ");
         if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && e_difference > 1) ) {
              if(!incremented){
              e_floor++;
              incremented = HIGH;}
         lift_display.showNumberDec(e_floor);
         }
         else if((buttonPushCounter_DSS  % 4 == 0 && buttonPushCounter_USS % 4 == 0 && difference > 1) ) {
              if(!incremented){
              current_floor=2;
              incremented = HIGH;}
         lift_display.showNumberDec(current_floor);
         }
         else if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && difference == 1) ) {
              if(!incremented){
              current_floor++;
              incremented = HIGH;}
         lift_display.showNumberDec(current_floor);
         lift.setAll(UP_LOW);
         }
         else if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && difference == 0) || (buttonPushCounter_DSS  % 3 == 0 && buttonPushCounter_USS % 2 == 0 && difference == 0) || (buttonPushCounter_DSS  % 5 == 0 && buttonPushCounter_USS % 4 == 0 && difference == 0)){
         incremented = LOW;
         lift.setAll(UP_LOW);}
         else if((buttonPushCounter_DSS  % 3 == 0 && buttonPushCounter_USS % 3 == 0 && difference == 0) || (buttonPushCounter_DSS  % 5 == 0 && buttonPushCounter_USS % 5 == 0 && difference == 0)) {
          lift.setAll(off);
          lift_display.showNumberDec(current_floor);
          incremented = LOW;
          goup = LOW;
         }
         else {
         incremented = LOW;
         lift.setAll(UP_HIGH);
         }}
         while (godown) {                         //// lift is going down
   Serial.print("current_floor is");
  Serial.println(current_floor, DEC);
  Serial.print("Destination floor is");
  Serial.println(destination_floor, DEC);
  Serial.print("E-floor is");
  Serial.println(e_floor, DEC);
  //delay(500);
         difference = destination_floor - current_floor;
         e_difference = e_destination - e_floor;
         lift_display.showNumberDec(current_floor);
         state_change_DSS(DSS);
         state_change_USS(USS);
         Serial.print("DSS= ");
         Serial.print(buttonPushCounter_DSS, DEC);
         Serial.print("   USS= ");
         Serial.println(buttonPushCounter_USS, DEC);
         if( digitalRead(incremented) == HIGH)
         Serial.println("  incremented is high ");
         else if ( digitalRead(incremented) == LOW)
         Serial.println("  incremented is low ");
        
         if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && e_difference < -1) ) {
              if(!incremented){
              e_floor--;
              incremented = HIGH;}
         lift_display.showNumberDec(e_floor);
         }
         else if((buttonPushCounter_DSS  % 4 == 0 && buttonPushCounter_USS % 4 == 0 && difference < -1) ) {
              if(!incremented){
              current_floor=0;
              incremented = HIGH;}
         lift_display.showNumberDec(current_floor);
         }
         else if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && difference == -1) ) {
          lift.setAll(DW_LOW);
             if(!incremented){
              current_floor--;
              incremented = HIGH;}
         lift_display.showNumberDec(current_floor);
         }
         else if((buttonPushCounter_DSS  % 2 == 0 && buttonPushCounter_USS % 2 == 0 && difference == 0) || (buttonPushCounter_USS  % 3 == 0 && buttonPushCounter_DSS % 2 == 0 && difference == 0) || (buttonPushCounter_USS  % 5 == 0 && buttonPushCounter_DSS % 4 == 0 && difference == 0)){
         lift.setAll(DW_LOW);
         incremented = LOW;}
         else if((buttonPushCounter_DSS  % 3 == 0 && buttonPushCounter_USS % 3 == 0 && difference == 0) || (buttonPushCounter_DSS  % 5 == 0 && buttonPushCounter_USS % 5 == 0 && difference == 0)) {
          lift.setAll(off);
          lift_display.showNumberDec(current_floor);
          incremented = LOW;
          godown = LOW;
         }
         else {
         incremented = LOW;
         lift.setAll(DW_HIGH);
         }}
         break;
    case LOW:
       lift.setAll(off);
    break;
    }}
  
/////////////////////////////// State Change functions  DSS
int state_change_DSS(int input_pin)
{
buttonState_DSS = digitalRead(input_pin);
    if (buttonState_DSS != lastButtonState_DSS) {
       if (buttonState_DSS == LOW) {
      buttonPushCounter_DSS++;}
      delay(50);
  }
lastButtonState_DSS = buttonState_DSS;
return buttonPushCounter_DSS;
}
//////////////////////////  USS
int state_change_USS(int input_pin)
{
buttonState_USS = digitalRead(input_pin);
    if (buttonState_USS != lastButtonState_USS) {
       if (buttonState_USS == LOW) {
      buttonPushCounter_USS++;}
      delay(50);
  }
lastButtonState_USS = buttonState_USS;
return buttonPushCounter_USS;
}
///////////////////////////  DLS
int state_change_DLS(int input_pin)
{
buttonState_DLS = digitalRead(input_pin);
    if (buttonState_DLS != lastButtonState_DLS) {
       if (buttonState_DLS == LOW) {
      buttonPushCounter_DLS++;}
      delay(50);
  }
lastButtonState_DLS = buttonState_DLS;
return buttonPushCounter_DLS;
}
////////////////////////   ULS
int state_change_ULS(int input_pin)
{
buttonState_ULS = digitalRead(input_pin);
    if (buttonState_ULS != lastButtonState_ULS) {
       if (buttonState_ULS == LOW) {
      buttonPushCounter_ULS++;}
      delay(50);
  }
lastButtonState_ULS = buttonState_ULS;
return buttonPushCounter_ULS;
}
 

Thread Starter

idir93

Joined Jan 10, 2018
27
they go all high at certain moment when running depending on the sequence of the elevator. Actually what happens in a change of speed from high to low, HS should go LOW and LS goes high, but what I experience is that all relays go HIGH for a very short moment.
 

BobaMosfet

Joined Jul 1, 2009
2,211
You're using relays... that's what that sound is, right? If so, it's possible that you are drawing too much current with a relay, and it is causing the MCU to reboot. Please post your entire schematic-- I'm not sure you understand how to control current enough to make sure you're not drawing to much.
 

Thread Starter

idir93

Joined Jan 10, 2018
27
Yes, I am using relays. And no, the MCU is not rebooting, because if it were rebooting, it wouldn't resume the code from where it has left.
Here is the schematic.
 

Attachments

BobaMosfet

Joined Jul 1, 2009
2,211
I don't see any resistors for your LEDs from U2, by the way. Your schematic shows no relays. Just momentary N/O pushbuttons, some displays, and LEDs.
 
Last edited:
Top