Go Down

Topic: Engineering project (Read 119 times) previous topic - next topic

littlevaq

I am currently in an engineering design and development class. In this class we got to choose a project that we were going to do and my group chose to do a lock that will automatically lock a door. We have decided to use an arduino nano for this project. The mechanism locks after 6 seconds of a limit switch being held down and it is supposed to unlock if you push another button. The mechanism locks the door perfectly fine after the 6 seconds but after it locks it will just randomly unlock instead of unlocking when you push the button. Below are a picture of the aruino nano on the breadboard and the code. Please help.

#include <Servo.h>
const int limitSwitch = 3;
const int ledPin = 13;
const int servoPin = 7;
const int unlockPin = 2;
int light = 0;
int setting = 0;
int yes = 0;
int pos = 90;
Servo servo;
void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  servo.attach(servoPin);
  pinMode(limitSwitch, INPUT);
  servo.write(pos);
}
void loop()
{
  while(setting == 0)
  {
    while(digitalRead(limitSwitch) == LOW)
    {
      light = 1;
      if(yes == 48)
      {
        pos = 160;
        servo.write(pos);
      }
      if(light == 1)
      {
        digitalWrite(ledPin, HIGH);
      }
      if(light == 0)
      {
        digitalWrite(ledPin, LOW);
      }
      delay (125);
      yes += 1;
      while(yes > 48)
      {
        if(digitalRead(unlockPin) == HIGH)
        {
          pos = 90;
          servo.write(pos);
          yes = 0;
        }
      }
    }
    if(digitalRead(limitSwitch) == HIGH)
    {
      light = 0;
      delay(20);
    }
    if(light == 1)
    {
      digitalWrite(ledPin, HIGH);
    }
    if(light == 0)
    {
      digitalWrite(ledPin, LOW);
    }
    digitalWrite(limitSwitch, HIGH);
  }
}

DrAzzy

#1
Apr 24, 2019, 05:50 pm Last Edit: Apr 24, 2019, 05:55 pm by DrAzzy
You posted in the wrong section. What part of "not for questions about your project" is confusing you?

Anyway, I'd bet money that your problem (Edit, realized you posted image of wiring, where no pulldowns are visible, so this is indeed the problem) is that the input pins are floating when the switch is off. I see you have them wired to connect the pin to Vcc when pressed, and it's connected to nothing when button not pressed. You need them to be LOW the rest of the time, but have not done anything to make that happen - when the button is not pressed, the wire is connected to nothing other than the input, with nothing acting on it to give it a defined voltage. Well, nothing other than ambient electrical noise: that floating pin will act as an antenna, picking up noise and taking a random value between 0 and 5v (between which it is confined by the protection diodes on the pin), which will change rapidly and randomly. You either need to add pulldown resistors (~10k is good bet - but anything within an order of magnitude of that will work) between each of those input pins and ground. Or rewire the switch so that pressing it connects the pin to ground, set the pin to use the internal pullup (INPUT_PULLUP - the AVR microcontrollers don't have internal pulldowns; some other non-AVR chips have both, but internal pullups are damned near universal) and invert the tests in your code (since it will now be LOW when pressed).
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Go Up