Main > Raspberry Pi & Dev Board

Arduino code

<< < (5/6) > >>

lilshawn:

--- Quote from: PL1 on November 04, 2022, 05:19:01 am ---Based on the code snippet you posted, what I can't wrap my head around is how/why "pinMode(PWRBTN, OUTPUT);" takes the pin to logic LOW (button pressed) without a digitalWrite.    :dunno


--- End quote ---

as far as i can tell, when the pin is set as an output pinMode(##,OUTPUT); there is apparently an internal resistor that gets applied to the pin that forces the pin to a LOW state on initialization so that when you issue a digitalWrite there is a decisive logic level switch to tie it high or low. apparently there is a way to disable it, but i've never had it work.

so the main problem is... this resistor prevents us from using it to drive an input that already has some kind of biasing (such as the computer power switch or an arcade button) as these inputs are tied HIGH and checked by the device to see if they are pulled LOW but are left floating. therefore the only way i have been able to "switch" an input like this, is to change the pinMode from an input (which has no internal bias resistor) to an output (which does) effectively simulating a button press to ground.

i will post the code next week, as it's currently on my work computer, and i'm not going back to the office till tuesday. i'm sure it can be optimised better than the dirty dirty demon code i did... but in the end, it does work. and i've been testing the computer most of the last week here.

PL1:
I'll try to do some more testing this weekend on what's going on during initialization and input/output switching.
- Maybe a big initialization delay to ensure there's time to open the serial monitor window, add more serial print messages to show what's going on, and slow the process down enough to stabilize the multimeter/read it/analyze it at each step.

Once we know what's actually happening we can clean up, speed up, and accurately document the code.   :cheers:


Scott

lilshawn:
i did notice that the serial window did not display any info despite there being info that was supposed to be passed onto the terminal window... i thought it was just my computer setup, as i just threw on the software willy-nilly slammed some code in there and promptly got onto the next thing.

wasn't a big deal, i was able to follow along in the code and figure out where to make changes and whatnot...and the //'s really helped  :laugh:

you should see my code i wrote for a device i made that is used to auto program 60-in-1 board settings in all the games using the service mode and button presses.

it's a LITERAL sea of

pinMode(up, OUTPUT);
delay 200;
pinMode(up, INPUT)
delay 200;
pinMode(right, OUTPUT);
delay 200;
pinMode(right, INPUT)
delay 200;

it's dirty as all EFF and im ashamed of anybody that would need to change/fix/replicate it, but...it works..and at the end of the day... that's what matters.

PL1:

--- Quote from: lilshawn on November 04, 2022, 11:51:01 pm ---i did notice that the serial window did not display any info despite there being info that was supposed to be passed onto the terminal window... i thought it was just my computer setup, as i just threw on the software willy-nilly slammed some code in there and promptly got onto the next thing.

--- End quote ---
1. You need to select a port other than COM1.

Sometimes I have to unplug/replug this board a few times to get COM5 to show up under the "Tools - Port" menu.
- Once it shows up, click on COM5 and you should get the selection checkmark.   ;D

2. Close the Serial Monitor window if you are uploading, resetting, or unplugging/replugging the board.  These are things that break the serial connection between the board and window.
- Open the Serial Monitor after you get the confirmation that the upload was successful or after the board boots up.  This ensures that the board is ready for the Serial Monitor to establish the serial connection.
- If you're not seeing the serial message stream, close and re-open the Serial Monitor to re-establish the connection.


--- Quote from: lilshawn on November 04, 2022, 11:51:01 pm ---wasn't a big deal, i was able to follow along in the code and figure out where to make changes and whatnot...and the //'s really helped  :laugh:

--- End quote ---
Glad the comments were useful.   :cheers:


Scott

lilshawn:

--- Code: ---// Computer power check and switch press for always-on.
    // Connect the LED power pin to pin A0
    // Connect the PC power button to pin 2


// Define variables
int id = 10000;            // Initial delay period -- how long to wait before starting the main program loop (in miliseconds)
int cd = 3000;            // Check delay period -- how long to wait between power checks (in miliseconds)
int pd = 500;             // Pulse duration period -- how long to press the button (in miliseconds)

int PWRPIN = 0;           // Power pin on pin A0
int pwrstate = 1024;      // Initialize power state condition
int pwrthreshold = 819;   // Power on threshold (range is 0-1024) -- greater than this value = power LED on

int PWRBTN = 2;           // Power button on pin 2
boolean pwrswitch = HIGH; // Initialize power switch condition -- HIGH = not pressed, LOW = pressed


// Define setup
void setup() {

pinMode(PWRBTN, OUTPUT); // Set the power button pin as an output

digitalWrite(PWRBTN, HIGH); // Initialize the power button pin as not pressed

delay(id); // Initial delay before starting main power loop

}


// Main program loop
void loop() {

delay(cd); // Delay between power checks

pwrstate = analogRead(PWRPIN); // Read the analog input pin

if(pwrstate > pwrthreshold) { // Do the following if the power LED voltage is >(power threshold/1024)*5v (on)

Serial.println("Power ON "); // Print power ON and carriage return
Serial.println(pwrstate); // Print pwrstate and carriage return

} else { // Do the following if the power LED is LOW (off)

Serial.println("Power OFF "); // Print power OFF and carriage return
Serial.println(pwrstate); // Print pwrstate and carriage return

// digitalWrite(PWRBTN, LOW); // Press the power button
pinMode(PWRBTN, OUTPUT);

Serial.print("Press power button "); // Print press power button
pwrswitch = digitalRead(PWRBTN);    // Read the power button pin state
Serial.println(pwrswitch); // Print power switch state and carriage return

delay(pd); // Hold the power button for "pd" miliseconds

//digitalWrite(PWRBTN, HIGH); // Release the power button
pinMode(PWRBTN, INPUT);

Serial.print("Release power button "); // Print release power button
pwrswitch = digitalRead(PWRBTN);    // Read the power button pin state
Serial.println(pwrswitch); // Print power switch state and carriage return

}

} // End main program loop
--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version