Example Needed Using Byte To DigitalWrite Pins - Arduino Forum
Maybe your like
Working with this code and I see it uses [Outlets] byte and try to figure out how to digialWrite pins . Also would like to figure out how to use the override menu but don,t understand the code instructions givin with code in override menu can any help me with some examples using this code.Here is the first part.
#include <avr/pgmspace.h> // library for keeping variables in Flash RAM #include <ooPinChangeInt.h> #include <AdaEncoder.h> // adafruit.com eoncoder routine. #include <Wire.h> #include <LiquidCrystal_I2C.h> #define menuTimeout 10000 //delay before settings menus time out & return #define sumpLevelSwitchPin 2 #define EncoderPin1 A0 // Rotary Encoder Pins #define EncoderPin2 A1 // #define buttonPin A2 // pin for encoder button #define ATOlevel 20 #define FanPin 0 #define Heater1Pin 1 #define Heater2Pin 2 #define Light1Pin 3 #define Light2Pin 4 #define Light3Pin 5 #define ATOPin 6 #define SkimmerPin 7 #define Powerhead1Pin 8 #define Powerhead2Pin 9 #define Powerhead3Pin 10 #define ReturnPumpPin 11 #define CircPumpPin 12 #define Doser1Pin 13 #define Doser2Pin 14 #define AuxPin 15 unsigned long lastTime; unsigned long lastMills = 0; unsigned long timeout; byte Outlets[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; char buffer[18]; const char mainMenu1[] PROGMEM = "1.Feed "; const char mainMenu2[] PROGMEM = "2.Man Override "; const char mainMenu3[] PROGMEM = "3.Exit "; const char *const mainMenu[]PROGMEM = { mainMenu1, mainMenu2, mainMenu3 }; const char OverrideMenu0[] PROGMEM = "1. Fan "; const char OverrideMenu1[] PROGMEM = "2. Heater 1 "; const char OverrideMenu2[] PROGMEM = "3. Heater 2 "; const char OverrideMenu3[] PROGMEM = "4. Light1 "; const char OverrideMenu4[] PROGMEM = "5. Light2 "; const char OverrideMenu5[] PROGMEM = "6. Light3 "; const char OverrideMenu6[] PROGMEM = "7. ATO "; const char OverrideMenu7[] PROGMEM = "8. Skimmer "; const char OverrideMenu8[] PROGMEM = "9. Powerhead 1 "; const char OverrideMenu9[] PROGMEM = "10.Powerhead 2 "; const char OverrideMenu10[] PROGMEM = "11.Powerhead 3 "; const char OverrideMenu11[] PROGMEM = "12.Return Pump "; const char OverrideMenu12[] PROGMEM = "13.Circ Pump "; const char OverrideMenu13[] PROGMEM = "14.Dosing Pump1"; const char OverrideMenu14[] PROGMEM = "15.Dosing Pump2"; const char OverrideMenu15[] PROGMEM = "16.Aux Outlet "; const char OverrideMenu16[] PROGMEM = "17.Exit "; const char *const OverrideMenu[]PROGMEM = { OverrideMenu0, OverrideMenu1, OverrideMenu2, OverrideMenu3, OverrideMenu4, OverrideMenu5, OverrideMenu6, OverrideMenu7, OverrideMenu8, OverrideMenu9, OverrideMenu10, OverrideMenu11, OverrideMenu12, OverrideMenu13, OverrideMenu14, OverrideMenu15, OverrideMenu16 }; const char OverrideSelect0[] PROGMEM = "1. Auto "; const char OverrideSelect1[] PROGMEM = "2. Off "; const char OverrideSelect2[] PROGMEM = "3. On "; const char*const OverrideSelect[]PROGMEM = { OverrideSelect0, OverrideSelect1, OverrideSelect2 }; int8_t clicks; char id = 0; int total = 0; LiquidCrystal_I2C lcd(0x3F, 16, 2); AdaEncoder encoderA = AdaEncoder('a', EncoderPin1, EncoderPin2); AdaEncoder *knob = NULL; void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT); digitalWrite(buttonPin, LOW); lcd.begin(); } void loop() { lcd.clear(); lcd.print("push button"); lcd.setCursor(0, 1); lcd.print("to start..."); while (!digitalRead(buttonPin)) { } knob = AdaEncoder::genie(); // check for input from rotary encoder if (knob != NULL || digitalRead(buttonPin)) { while (digitalRead(buttonPin)); delay(20); lcd.clear(); lcd.print("going to menu"); delay(1000); menu(); } } void menu() { int item; timeout = millis() + menuTimeout; do { item = menuSelect(mainMenu, 3); switch (item) { case 0: feed(); break; case 1: overrideMenu(); // man override... break; case 2: return; break; } } while (item != 3 && timeout > millis()); } // **************************************************** void feed() { // Return pump and skimmer off i2cWrite(ReturnPumpPin % 8, 0, ReturnPumpPin / 8 + 1); i2cWrite(SkimmerPin % 8, 0, SkimmerPin / 8 + 1); lcd.clear(); lcd.print(F("Pump/skimmer off")); lcd.setCursor(0, 1); lcd.print(F("press to cont...")); while (!digitalRead(buttonPin)); delay(30); while (digitalRead(buttonPin)); // now wait for release delay(30); // debounce timeout = millis() - 1; // exit out of menus on return return; // pup and skimmer will be returned to their normal states in the main loop } // **************************************************** void overrideMenu() { int item; do { item = menuSelect(OverrideMenu, 17); if (item == 16) return; // 17 = exit; int mode = menuSelect(OverrideSelect, 3); Outlets[item] = Outlets[item] % 2 + 2 * mode; // 'item' indexes the pointer to the proper spot; mode will be // 0 for auto, 1 for always off, 2 for always on; LSB (=*outlet%2) // is the default value, so it stays unchangedd. 2nd bit (2^1) override off, // 3rd bit (2^2) override on, so multiply menuSelect by 2 for bit values // and add. lcd.clear(); lcd.print(strcpy_P(buffer, (char*)pgm_read_word(&(OverrideMenu[item]))) + 3); lcd.setCursor(0, 1); lcd.print(F("Set to ")); lcd.print(strcpy_P(buffer, (char*)pgm_read_word(&(OverrideSelect[mode]))) + 3); delay(2000); } while (item != 16); } // **************************************************** richl15 November 12, 2015, 12:33am 3And the second part.
// menuSelect - scroll through a menu, return # of selected item // int menuSelect(const char*const menustring[], int menuItems) { int currentItem = 0; //item that's currently selected/highlighted lcd.clear(); lcd.setCursor(0, 0); lcd.print(F(">")); lcd.println(strcpy_P(buffer, (char*)pgm_read_word(&(menustring[0])))); lcd.setCursor(0, 1); lcd.print(F(" ")); lcd.println(strcpy_P(buffer, (char*)pgm_read_word(&(menustring[(1) % menuItems])))); timeout = millis() + menuTimeout; do { AdaEncoder *knob = NULL; knob = AdaEncoder::genie(); if (knob != NULL) { clicks = knob->query(); currentItem += clicks; clicks = 0; timeout = millis() + menuTimeout; // reset timer if there's input currentItem = (currentItem + menuItems) % menuItems; // wrap around // '+ menuitems' makes equation work if curentitem <0 lcd.setCursor(0, 0); lcd.print(F(">")); lcd.println(strcpy_P(buffer, (char*)pgm_read_word(&(menustring[currentItem])))); lcd.setCursor(0, 1); if (currentItem < menuItems - 1) { // we're not at the last selection ('exit') lcd.print(F(" ")); lcd.println(strcpy_P(buffer, (char*)pgm_read_word(&(menustring[(currentItem + 1) % menuItems])))); } else { // print the next selection lcd.println(F(" ")); // otherwise print a blank line on line 2 } } if (digitalRead(buttonPin)) { // if button's been pushed while (digitalRead(buttonPin)) { delay(20); // wait for it to be released } delay(30); //delay for btton debounce timeout = millis() + menuTimeout; // reset timeout time return currentItem; } } while (timeout > millis()); // end do timeout = millis() + menuTimeout; return (menuItems - 1); } void pause(char* msg) { Serial.print(msg); Serial.println(F(", Proceed?")); while (!Serial.available()); while (Serial.available()) Serial.read(); return; } // ************************************ // routine to turn outlets on/off // Pins 0-7 are in outlet bank 1, 8-15 in bank 2 // pin%8 converts to 0-7, // pin/8+1 converts to i2c address 1 for 0-7, 2 for 8-15 // to allow for automatic assignment if pins are changed void outlets() { i2cWrite(FanPin % 8, Outlets[FanPin] % 2, FanPin / 8 + 1); i2cWrite(Heater1Pin % 8, Outlets[Heater1Pin] % 2, Heater1Pin / 8 + 1); i2cWrite(Heater2Pin % 8, Outlets[Heater2Pin] % 2, Heater2Pin / 8 + 1); i2cWrite(Light1Pin % 8, Outlets[Light1Pin] % 2, Light1Pin / 8 + 1); i2cWrite(Light2Pin % 8, Outlets[Light2Pin] % 2, Light2Pin / 8 + 1); i2cWrite(Light3Pin % 8, Outlets[Light3Pin] % 2, Light3Pin / 8 + 1); i2cWrite(ATOPin % 8, Outlets[ATOPin] % 2, ATOPin / 8 + 1); i2cWrite(SkimmerPin % 8, Outlets[SkimmerPin] % 2, SkimmerPin / 8 + 1); i2cWrite(Powerhead1Pin % 8, Outlets[Powerhead1Pin] % 2, Powerhead1Pin / 8 + 1); i2cWrite(Powerhead2Pin % 8, Outlets[Powerhead2Pin] % 2, Powerhead2Pin / 8 + 1); i2cWrite(Powerhead3Pin % 8, Outlets[Powerhead3Pin] % 2, Powerhead3Pin / 8 + 1); i2cWrite(ReturnPumpPin % 8, Outlets[ReturnPumpPin] % 2, ReturnPumpPin / 8 + 1); i2cWrite(CircPumpPin % 8, Outlets[CircPumpPin] % 2, CircPumpPin / 8 + 1); i2cWrite(Doser1Pin % 8, Outlets[Doser1Pin] % 2, Doser1Pin / 8 + 1); i2cWrite(Doser2Pin % 8, Outlets[Doser2Pin] % 2, Doser2Pin / 8 + 1); i2cWrite(AuxPin % 8, Outlets[AuxPin] % 2, AuxPin / 8 + 1); Serial.println(); } void i2cWrite(byte pin, byte value, byte addr) { Serial.print("Write "); Serial.print(value); Serial.print(" to pin #"); Serial.print(pin); Serial.print(" at address "); Serial.println(addr); } richl15 November 12, 2015, 12:53am 4Ok I thought outlets was digitalWriting pins some how I have written the code already using digital write and that works good . What I can't figure out is how to write the override code.If I write It in Void override I used switch case but when the menu goes back to the main loop it just overrides the override . How would I call the function in the main loop
richl15 November 12, 2015, 1:11am 6If a light timer,heater, pumps,fan,etc were ON I should be able to go to override menu and shut them OFF and say if the timer was to turn ON in an hour it should stay OFF . If I choose ON in override it would be ON all the time and if I choose AUTO it turns ON and OFF using the timer function wrote in the main loop.
richl15 November 12, 2015, 2:07am 7the list of character strings is for the void overrideMenu() Not sure if you look at the code in my first post but there are instructions commented out in void overrideMenu() that my be able to help me but I don't understand them maybe you do or someone else.
bperrybap November 12, 2015, 5:40am 8Delta_G: byte is just an 8 bit data type. This code is using that to store the state of the pins he is setting, 1 for on and 0 for off. You can use that in a digitalWrite since HIGH is the same thing as 1 and LOW is 0.
digitalWrite(somePin, 1); // writes a pin high
While HIGH and LOW happen to be 1 and 0 in the cores supplied by the arduino.cc IDE, they are not required to be those values. The digitalWrite() API is defined to use HIGH and LOW not 1 and 0 or 0 and any non-zero value.
To use 0 instead of LOW and 1 or a non zero value instead of HIGH when using digitalWrite() is abusing the API because it is taking advantage of specific implementation details. When writing code that uses an API, it is always best to use the API the way it is defined.
--- bill
richl15 November 12, 2015, 11:52pm 10Need help with last part of this code understanding how to use the override menu maybe with the instructions in the void override part that is commented
richl15 November 13, 2015, 12:14am 12I don,t understand how to use this part of the code. The commented out stuff and I assume it needs to be called in the main loop to work properly
void overrideMenu() { int item; do { item = menuSelect(OverrideMenu, 17); if (item == 16) return; // 17 = exit; int mode = menuSelect(OverrideSelect, 3); Outlets[item] = Outlets[item] % 2 + 2 * mode; // 'item' indexes the pointer to the proper spot; mode will be // 0 for auto, 1 for always off, 2 for always on; LSB (=*outlet%2) // is the default value, so it stays unchangedd. 2nd bit (2^1) override off, // 3rd bit (2^2) override on, so multiply menuSelect by 2 for bit values // and add. richl15 November 13, 2015, 12:43am 15I cant seem to make the ON,OFF AUTO part work. Think of it maybe like progamable thermastat if the program is set in it. You press RUN and that runs the program for times and temperatures. In other words "AUTO". If you press HOLD it just holds that temperature "ON" until you press RUN again. Can you explain the formula in the commented out stuff or write an example for it.
// 'item' indexes the pointer to the proper spot; mode will be // 0 for auto, 1 for always off, 2 for always on; LSB (=*outlet%2) // is the default value, so it stays unchangedd. 2nd bit (2^1) override off, // 3rd bit (2^2) override on, so multiply menuSelect by 2 for bit values // and add.
richl15 November 13, 2015, 1:33am 16The code that I posted is cut smaller to use as an example there's a lot more stuff like temp menu light timer menus etc I made the code smaller just to make it easier to post but let's say in void feed when you enter the menu the pumps etc shut off digital write low and when you press the button again pumps etc on digital write high that is how that part works but if I want to use the override and keep pumps off or on and the auto part would be the void feed
Related topics
| Topic | Replies | Views | Activity |
|---|---|---|---|
| Turn on and off an output for a specified amount of time? Programming | 54 | 2146 | November 22, 2023 |
| Snellere IO via poort manipulatie Nederlands | 48 | 119 | November 15, 2025 |
| Waarom werkt de lcd nog niet goed in wokwiki ? Nederlands | 187 | 668 | January 25, 2026 |
| GIGA R1 Wifi : Fast digital output handling Programming | 63 | 599 | February 19, 2026 |
| could use some help with progmem Programming | 14 | 2435 | May 5, 2021 |
Tag » Arduino Write Byte To Digital Pins
-
Writing An Output Byte Instead Of Single Pin - Arduino Forum
-
Parallel I/O - E.g Reading Or Writing A Whole Byte, Not One Pin At A Time
-
Byte Digital Output - Programming Questions - Arduino Forum
-
Converting Digital Pin Input To Byte - Arduino Forum
-
8-Bit IO Port Library For Arduino - Arduino Project Hub
-
Digital Pins To Byte Variable... - Arduino Forum
-
Assigning A Byte To Port Pins - Arduino Forum
-
Arduino And Port Manipulation : 9 Steps - Instructables
-
4. Serial Communications - Arduino Cookbook [Book] - O'Reilly
-
How To Manipulate Arduino Pins Simultaneously
-
Référence Du Langage Arduino En Français - DigitalWrite()
-
Getting Started With Arduino
-
22.1: DigitalWrite() - Engineering LibreTexts