LED with Switch and Memory Bulp



 https://www.tinkercad.com

Script

 

int buttonState = 0;
int buttonNurse = 1;
int buttonNurseOld = 1;
int PortBMem;
int PortCMem;
int PortDMem;

void setup()
{
  Serial.begin (9600);
  DDRD = B00000000;  // sets Arduino pins 0 to 7 as input, pin 0 as input
  DDRD = DDRD | B11110000;  // this is safer as it sets pins 4 to 7 as inputs
                        // without changing the value of pins 0 to 3, which are RX & TX
  DDRB = B00000000;  // sets Arduino pins 8 to 13 as input
  DDRB = DDRB | B00111111;  // this is safer as it sets pins 8 to 13 as inputs

  DDRC = B00001111;  // sets Arduino pins A0 to A4 as output
  DDRC = DDRC | B00001111;  // this is safer as it sets pins 8 to 13 as inputs

  PortBMem = 0;
  PortCMem = 0;
  PortDMem = 0;
 
  PORTC = 0;
/*
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
*/  
}

void(* resetFunc) (void) = 0; //declare reset function @ address 0

void loop()
{

    PortBMem = PINB;
 
      if (PortBMem > B00000000){
      PortCMem = PortCMem | B1;
      PORTC = PortCMem;
      delay(100);
      }
 
    PortDMem = PIND;

    if ((PortDMem & B11100000)>31){
      PortCMem = PortCMem | ((PortDMem >> 4)& B00001110);
      PORTC = PortCMem;
      delay(100);
    }
 
    buttonNurse = digitalRead(4);
 
    if (buttonNurseOld != buttonNurse){
      if (buttonNurse == LOW){
          PortCMem = 0;
          PortDMem = 0;
          PORTC = 0;
          delay(1000);
          //resetFunc();  //call reset
      }
      buttonNurseOld = buttonNurse;
  }
 
// buttonNurse = digitalRead(4);

  delay(10); // Delay a little bit to improve simulation performance
}