Nathan analog + digital Assignment

For this week’s assignment, I wanted to do a ghostbuster (kind of, not really) themed project. The idea is to use the potentiometer to find the position of the ghost, while the lights fade.

General instructions

The player is entering a house that is infested with ghosts. First, they have to click the red button on the right to activate the ghost-catching device. The ghost-catching device has five red LEDs, marking the general 5 directions in front of the player. When the lights in the house are on, all five device lights are on, as the surrounding is too bright to detect anything.

As the lights in the ghost-infested house are dimmed, the ghosts begin to reveal themselves. They are quick and barely leave marks, so the lights on the device are usually all lighting chaotically. The player then turns the knob to try and pinpoint the ghost locations.

The ghost changes its location every second. If the direction the knob is pointing to aligns with the ghost’s location, the correlating light on the device will shine, revealing that location to the player.

Troubles

I ran into a lot of problems for this week’s assignment. Some kind of dumb, for example, I forgot the ground the red button and was very confused about the chaotic lights that were flashing for quite some time.

I think the most useful challenge that I faced was trying to organize functions, specifically the logic of the functions and their conditions. There are many steps and phases in my project this week. When I began to code the project, I didn’t really use functions, and the loop function was a mess. So I redesigned my code and tried to compartmentalize each step. This helped a lot, but the order that the functions ran was also something that needed to be taken care of.

Wiring

For the wiring of this project, I had one digital button that inputs ON/OFF to the Arduino wired on the right side of the breadboard. There are five red LEDs to signify the device location lights, a potentiometer that signifies the controls for the ghost device, and a photoresistor that signifies the lights in the haunted hosue

Code

int button = 2;
int led1 = 4;
int led2 = 6;
int led3 = 8;
int led4 = 10;
int led5 = 12;
bool led1state = false;
bool led2state = false;
bool led3state = false;
bool led4state = false;
bool led5state = false;
bool prevB = false;
//timer for fake ghost position
long timer;
int timerlength = 50;
//timer for real ghost position
long timer1;
int timerlength1 = 1000;
int ghostPosition;
int truePosition;


void setup() {
  pinMode(button, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  
  Serial.begin(9600);
  timer = millis() + timerlength;
  timer1 = millis() + timerlength1;
}

void loop() {
  deviceon();
  //shifting the ghost's true position every second
  if (millis() > timer1){
    truePosition = random(1,6);
    timer1 = millis() + timerlength1;
  }
}

//function for turning the ghost catching device on
void deviceon() {
  int buttonstate = digitalRead(button);
  if (buttonstate == HIGH && prevB == LOW){
    deviceSwitch();
  }
  digitalWrite(led1, led1state);
  digitalWrite(led2, led2state);
  digitalWrite(led3, led3state);
  digitalWrite(led4, led4state);
  digitalWrite(led5, led5state);
  prevB = buttonstate;
  lightsoff();
}

//turning the ghost device lights on or off
void deviceSwitch() {
  led1state = !led1state;
  led2state = !led2state;
  led3state = !led3state;
  led4state = !led4state;
  led5state = !led5state;
}

//fake ghost position lights
void ghost() {
  if (millis() > timer){
    ghostPosition = random(1,6);
    
    if (ghostPosition == 1){
      digitalWrite(led1, true);
    }
    else if (ghostPosition != 1){
      digitalWrite(led1, false);
    }
    
    if (ghostPosition == 2){
      digitalWrite(led2, true);
    }
    else if (ghostPosition != 2){
      digitalWrite(led2, false);
    }
    
    if (ghostPosition == 3){
      digitalWrite(led3, true);
    }
    else if (ghostPosition != 3){
      digitalWrite(led3, false);
    }

    if (ghostPosition == 4){
      digitalWrite(led4, true);
    }
    else if (ghostPosition != 4){
      digitalWrite(led4, false);
    }
    
    if (ghostPosition == 5){
      digitalWrite(led5, true);
    }
    else if (ghostPosition != 5){
      digitalWrite(led5, false);
    }
    
    timer = millis() + timerlength;
  }
}

//what happens when the house lights are off
void lightsoff() {
  int light = analogRead(A0);
  if (light <= 250){

    led1state = false;
    led2state = false;
    led3state = false;
    led4state = false;
    led5state = false;
    ghost();
    catching();
  }
}

//function for the potentiometer, catching the true position
void catching() {
  int knobValue = analogRead(A1);
  Serial.println(knobValue);
  int knobPosition;

  if (270 < knobValue && knobValue <= 360){
    if (truePosition == 5){
      led5state = !led5state;
      led1state = false;
      led2state = false;
      led3state = false;
      led4state = false;
    }
  }
  if (360 < knobValue && knobValue <= 480){
    if (truePosition == 4){
      led4state = !led4state;
      led1state = false;
      led2state = false;
      led3state = false;
      led5state = false;
    }
  }
  if (480 < knobValue && knobValue <= 650){
    if (truePosition == 3){
      led3state = !led3state;
      led1state = false;
      led2state = false;
      led4state = false;
      led5state = false;
    }
  }
  if (650 < knobValue && knobValue <= 730){
    if (truePosition == 2){
      led2state = !led2state;
      led3state = false;
      led1state = false;
      led4state = false;
      led5state = false;
    }
  }
  if (730 < knobValue && knobValue <= 800){
    if (truePosition == 1){
      led1state = !led1state;
      led2state = false;
      led3state = false;
      led4state = false;
      led5state = false;
    }
  }

}

Demonstration

Leave a Reply