Pages: [1]   Go Down
Author Topic: Reading the value of an output pin?  (Read 356 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Team WIKISPEED
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible for the arduino to digitalRead the value (high or low) of it's own pin that is supplying 5v to the base of a relay driver circuit without using another (input) pin connected to that output pin?
I am using a Duemilanove (I have an UNO as well)

here is an example:

Code:
  if (lastSw == LOW && currentSw == HIGH && outPin == LOW);
  {
    digitalWrite(outPin, HIGH);
  }
 
lastSw and currentSw are inputs but outPin controls the relay circuit. can i read outPin like this or is it illogical?
as you can see, i want to turn outPin on if it is not already on and the other two inputs are where they need to be.

any help is greatly appreciated!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would think the correct way to do that is in software. Declare a variable before you setup function (global variable) like boolean running = false;

Then any time you perform a:

digitalWrite(outPin, HIGH);  // turn motor on
follow it immediately with a
running = true;  // capture that motor is on

and any time you perform a:

digitalWrite(outPin, LOW); // turn motor off
follow it immediately with a
running = false; // capture that motor is off

Now your sketch has a variable called running that you can test any time or where in your sketch to see the present state of the output pin that is controlling your motor.

Lefty
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If lastSw is low and current Sw is high, would outPin stay high even if it was high already?

Because if so, there's no point testing it- just send outPin high based on the other two pins, regardless of outPin's existing state.

Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

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

Code:
if (lastSw == LOW && currentSw == HIGH && outPin == LOW);
If the condition evaluates to true, do nothing (;). Otherwise, do nothing.

Why bother with the evaluation?
« Last Edit: January 14, 2013, 01:22:23 pm by PaulS » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is an example:

Code:
 if (lastSw == LOW && currentSw == HIGH && outPin == LOW);
  {
    digitalWrite(outPin, HIGH);
  }
  

To make any sense, that would need to be digitalRead(outPin) == LOW.

On my UNO, with IDE 1.0.1, a digitalRead() to an OUTPUT pin returns the last value that was output. I would expect this behaviour to be consistent with other cores and versions. So, what you're trying to do should work, as long as you do actually test the state of the pin rather than the pin number and lose that bogus semicolon.

Logged

I only provide help via the forum - please do not contact me 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

Here's the basic logic that I use to handle relays.

Code:
byte relayPin;                             // point to the relay's pin
int relayState = LOW;                  // keeps the relay state as HIGH or LOW

void setup(){
     pinMode(relayPin, OUTPUT);            // set the pin to output
     digitalWrite(relayPin, relayState);    // write the current state to it
}

// now we'll check the relay to see if it should be on or off
void loop(){
     int flag = LOW;                   // start with a flag set to low
     if (The relay should be on for whatever reason)   // obviously not real code
     {
          flag = HIGH;
     }
     // now compare the flag to the relayState
     if (flag != relayState)
     {
          relayState = flag;                                  // assign flag's value to relayState
          digitalWrite(relayPin, relayState);         // write this new state to the pin
     }
}

It's pretty sane and easily allows for more than one reason to keep the relay on. The relay only gets changed if needed.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Isn't the state of all pins available in the MCU registers?

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Seattle, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Team WIKISPEED
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First off, wow! i dont think i've ever used a forum with so many useful answers after a single question! thanks guys!

so i guess to put this in perspective, ill fill you in a bit more. this is my first *almost* completely original sketch. it's function is to emulate a smart key system like toyota uses. so if the rf key is detected and the brake is held while the button is pushed the car starts right up. if the brake is not held and the button is pushed it goes to accessory, push again, ignition. once at ignition you can hold the brake and push again to start the vehicle or you can just push again and the car turns off. right now im not worried about the rf key since this vehicle is still in development and i can code that in a bit further down the line.

the safety feature is making sure the NSS (neutral safety switch from the transmission that shows ground in park or neutral) is grounded so the vehicle cannot start in gear.

the lastSw and currentSw are variables from a debounce code i borrowed from another sketch. honestly, i dont know if i'm even using it correctly.

my assumption is that by making sure the last switch state and current switch state does not match in each possible situation it will prevent crazy things from happening if the user holds the button down for too long since it is always checking to make sure the last button state was not high (pressed) before running another line of code.

here is my sketch if you care to look it over. if you see anywhere i can make it more efficient please let me know! i appreciate your time and helpfulness!

Code:
/*
  Push button Ignition Switch/Starter (Like Toyota SmartKey)
 
TEAM WIKISPEED
Created by Nick Franklin - Franklin Automotive Electronics
206.551.5705
January 14, 2013
*/


//set pin numbers:

const int brakePin = 7;   // brake petal +input
const int engRun = 6;     // from alternator running +input
const int swPin = 5;      // ignition push button closed = HIGH
const int stPin = 4;      // +output to starter relay
const int igPin = 3;      // +output to ignition relay
const int led = 13;       // i synchronize this led with other outputs for diagnostic purposes
const int nssPin = 8;     // -input from transmission in park or neutral so you can not start in gear
boolean lastSw = LOW;
boolean currentSw = LOW;

void setup()
{
  pinMode(stPin, OUTPUT);
  pinMode(igPin, OUTPUT);
  pinMode(led, OUTPUT);   
  pinMode(swPin, INPUT);
  pinMode(engRun, INPUT);
  pinMode(brakePin, INPUT);
  pinMode(nssPin, INPUT);


}

// Debounce code to prevent button misread
boolean debounce(boolean last)
{
  boolean current = digitalRead(swPin);
  if (last != current);
  {
    delay(5);
    current = digitalRead(swPin);
  }
  return current;




void loop()
{
  currentSw = debounce(lastSw);
 
  //if the ignition is off but the brake is held and the ignition button is pushed
  //program will turn the ignition on, wait 3sec for fuel pump to prime, then activate the starter relay.
  if (lastSw == LOW && currentSw == HIGH && igPin == LOW && nssPin == LOW && brakePin == HIGH);
  {
    digitalWrite(igPin, HIGH);
    delay(3000);
    digitalWrite(stPin, HIGH);
    //once the engine running signal is recieved from the alternator the starter relay is deactivated.
    if (engRun == HIGH);
    digitalWrite(stPin, LOW);
  }
  // if the ignition is off and the button is pressed once ignition will turn on
  if (lastSw == LOW && currentSw == HIGH && igPin == LOW);
  {
    digitalWrite(igPin, HIGH);
  }
 
  // if ignition is on, brake is pressed, engine is not running, and the button is pushed
  // then activate the starter relay.
    if (lastSw == LOW && currentSw == HIGH && igPin == HIGH && nssPin == LOW && brakePin == HIGH && engRun == LOW);
  {
    digitalWrite(stPin, HIGH);
    //once the engine running signal is recieved from the alternator the starter relay is deactivated.
    if (engRun, HIGH);
    digitalWrite(stPin, LOW);
  }
  // if the engine is running and the button is pushed the ignition relay is deactivated and the vehicle is turned off
    if (lastSw == LOW && currentSw == HIGH && igPin == HIGH && engRun == HIGH);
  {
    digitalWrite(igPin, LOW);
  }
}
« Last Edit: January 14, 2013, 07:03:32 pm by NFrank89 » Logged

Pages: [1]   Go Up
Jump to: