Pages: [1]   Go Down
Author Topic: Seatbelt detection  (Read 1217 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey people. I have been working on a system for my science fair that can detect whether a seat  belt is fastened. In my prototype the "seat belt" is actually a switch. Fastened = closed, Unfastened = open. The system is made up of two components, a seat belt status checker and a "car". The seat belt status checker lights up a green LED when the switch is closed, and a red one if the switch is open. It is controlled by a 7404 IC. The "car" is a simple LED circuit that should light up only if the seat belt is fastened. Overall the system works like this:

"Seat belt" fastened ---> Button on "car" pressed ---> green LED on "car" turns on ---> "Seat belt" unfastened ---> green LED on "car" blinks for 10sec/7seg LED countdowns 10sec ---> green LED on "car" turns off ---> Back to Step 1

I have programmed the 7 seg LED already but cannot seem to link the "seat belt status checker" to the "car". Any ideas on how to program it so? My current progress on the program:


#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define Z 7
#define G 8
#define ledPin 10
const int sbeltState = 0;
const int sbeltPin = 9;


#define ON HIGH
#define OFF LOW

int ms = 1000;

void setup(){
 
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(Z, OUTPUT);
  pinMode(G, OUTPUT);
 
 
}

void loop(){
  sbeltState == digitalRead(sbeltPin);
  while (sbeltState == HIGH){
    countdown();
  }
 
}

void countdown(){
    nine();
    delay(ms);
    eight();
    delay(ms);
    seven();
    delay(ms);
    six();
    delay(ms);
    five();
    delay(ms);
    four();
    delay(ms);
    three();
    delay(ms);
    two();
    delay(ms);
    one();
    delay(ms);
    zero();
    delay(ms);
}

void nine(){
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(Z, ON);
  digitalWrite(G, ON);
}
void eight(){
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(Z, ON);
  digitalWrite(G, ON);
}
  void seven(){
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(Z, OFF);
  digitalWrite(G, OFF);
}
  void six(){
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(Z, ON);
  digitalWrite(G, ON);
}
  void five(){
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(Z, ON);
  digitalWrite(G, ON);
}
  void four(){
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(Z, ON);
  digitalWrite(G, ON);
}
  void three(){
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(Z, OFF);
  digitalWrite(G, ON);
}
  void two(){
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(Z, OFF);
  digitalWrite(G, ON);
}
  void one(){
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(Z, OFF);
  digitalWrite(G, OFF);
}
  void zero(){
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(Z, ON);
  digitalWrite(G, OFF);
}

My Setup


Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Please use the # button to give code appropriate tags. You can modify your post, select the code and press the # button.

with respect to your question I think you need one line in loop()

Code:
void loop()
{
  sbeltState == digitalRead(sbeltPin);
  digitalWrite(ledPin, sbeltState);

  while (sbeltState == HIGH)
  {
    countdown();
  }
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop(){
  sbeltState == digitalRead(sbeltPin);   // #1 and #2
  while (sbeltState == HIGH){                 // #3
    countdown();
  }
 
}

#1 - sbeltState is declared as a constant.
#2 - You are using a comparison operator when you want an assignment.
#3 - sbeltState will never be HIGH it's a constant with a value of 0.

You start using some vague defines for pin assignment then break that convention with sbeltPin.

You may need to read up on variable types a bit more. You might find arrays an exciting topic.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

While you are fixing those issues, you might want to consider what you are doing while the state is HIGH. You start countdown() over an over.

You have nothing in the code for dealing with when countdown() ends or when the state changes to LOW.

Finally, this is a really good opportunity to learn about detecting transitions. You are not as concerned about the state of the switch as you are about when the switch changes state - when it goes from released to pressed (the seatbelt is buckled) or when it goes from pressed to released (the seatbelt is unbuckled).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry mates I'm kinda new to Arduino and this place. I figured it out. Should have used a AND gate. Got it working. Thanks! I'm new to Arduino. Still getting used to it. Was doing a lot of 8051 programming before.
Logged

Pages: [1]   Go Up
Jump to: