Hi I am using an Arduino Mega, it is suppose to run my air ride on my car. All inputs and outputs is connected to the Arduino Mega with only the 8 channel relay module board getting power from an external source to power up the relay module, the screen and pressure transducers is getting power from the Arduino Mega. I am using a 4x4 keypad with A, B, C and D keys as presets and the rest of the keys controlling the relays individually. See below complete code.
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 23, 24, 25}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {26, 27, 28, 29}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
/////// REMOTE ///////
#include <RemoteControl.h>
#include "RemoteReceiver.h"
const int pinD0D = A9; // Connect to D0 on the receiver Button D
const int pinD1C = A8; // Connect to D1 on the receiver Button C
const int pinD2B = A7; // Connect to D2 on the receiver Button B
const int pinD3A = A6; // Connect to D3 on the receiver Button A
const int pinVT = A5; // Connect to VT on the receiver Learn
/////// SCREEN ///////
#include <Adafruit_GFX.h> // include Adafruit graphics library
#include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED library
#include <SPI.h>
#include <Wire.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define BLACK 0
#define WHITE 1
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI 51
#define OLED_CLK 52
#define OLED_DC 48
#define OLED_CS 53
#define OLED_RESET 44
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const unsigned long interval = 1000; // Time interval between pressure readings in milliseconds
unsigned long previousMillis = 0; // Variable to store the last time pressure was read
unsigned long previousDisplayMillis = 0; // Variable to store the last time the display was updated
unsigned long displayInterval = 1000; // Interval for updating the display in milliseconds
/////// PRESSURE ///////
const int pressureInput1 = A4; // Analog pin for pressure sensor A1
const int pressureInput2 = A1;
const int pressureInput3 = A2;
const int pressureInput4 = A3;
const int pressureInput5 = A0;
// Define your preferred PSI thresholds for each pressure sensor
int preferredPSI_A1 = 30; // Adjust this value according to your requirements
int preferredPSI_A2 = 30;
int preferredPSI_A3 = 50;
int preferredPSI_A4 = 50;
int preferredPSI_B1 = 40;
int preferredPSI_B2 = 40;
int preferredPSI_B3 = 50;
int preferredPSI_B4 = 50;
int preferredPSI_C1 = 60;
int preferredPSI_C2 = 60;
int preferredPSI_C3 = 70;
int preferredPSI_C4 = 70;
const int preferredPSI_D1 = 0;
const int preferredPSI_D2 = 0;
const int preferredPSI_D3 = 0;
const int preferredPSI_D4 = 0;
float pressureValue1 = 0; //variable to store the value coming from the pressure transducer
float pressureValue2 = 0;
float pressureValue3 = 0;
float pressureValue4 = 0;
float pressureValue5 = 0;
const int pressureZero = 102; //analog reading of pressure transducer at 0psi 102.4
const int pressureMax = 921; //analog reading of pressure transducer at 200psi 921.6
const int pressuretransducermaxPSI = 200; //psi value of transducer being used
// Function to check if pressure exceeds the preferred PSI
/*bool pressureExceedsThreshold(int pin, int preferredPSI_A1) {
pressureValue1 = analogRead(pin);
return pressureValue1 >= preferredPSI_A1;
}*/
/////// RELAYS /////////
byte relayPin1 = 9;
byte relayPin2 = 8;
byte relayPin3 = 7;
byte relayPin4 = 6;
byte relayPin5 = 5;
byte relayPin6 = 4;
byte relayPin7 = 3;
byte relayPin8 = 2;
void setup() {
Serial.begin(9600); // Initialize serial communication
// Rotate the display 90 degrees (clockwise)
display.setRotation(1);
// SSD1306 display setup
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
pinMode(pinD0D, INPUT);
pinMode(pinD1C, INPUT);
pinMode(pinD2B, INPUT);
pinMode(pinD3A, INPUT);
pinMode(pinVT, INPUT);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
pinMode(relayPin5, OUTPUT);
pinMode(relayPin6, OUTPUT);
pinMode(relayPin7, OUTPUT);
pinMode(relayPin8, OUTPUT);
// Initialize relays to OFF state
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, HIGH);
digitalWrite(relayPin5, HIGH);
digitalWrite(relayPin6, HIGH);
digitalWrite(relayPin7, HIGH);
digitalWrite(relayPin8, HIGH);
keypad.addEventListener(keypadEvent);
}
void loop() {
char key = keypad.getKey(); // Check for a keypress
unsigned long currentMillis = millis(); // get the current time
// Check if the specified interval has passed since the last pressure reading
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the current time for the next interval
// Read pressure values
pressureValue1 = analogRead(pressureInput1);
pressureValue1 = ((pressureValue1 - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
pressureValue2 = analogRead(pressureInput2);
pressureValue2 = ((pressureValue2 - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
pressureValue3 = analogRead(pressureInput3);
pressureValue3 = ((pressureValue3 - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
pressureValue4 = analogRead(pressureInput4);
pressureValue4 = ((pressureValue4 - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
pressureValue5 = analogRead(pressureInput5);
pressureValue5 = ((pressureValue5 - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
}
// Check if the display update interval has passed
if (currentMillis - previousDisplayMillis >= displayInterval) {
previousDisplayMillis = currentMillis; // Save the current time for the next display update
// Display pressure readings
display.clearDisplay(); // Clear the display buffer
// LF
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 5);
display.println("LF");
display.setCursor(4, 25);
display.print(" ");
display.println(pressureValue1, 0);
//Serial.print(pressureValue1);
//Serial.print(" ");
// RF
display.setTextSize(1);
display.setCursor(45, 5);
display.println("RF");
display.setCursor(39, 25);
display.print(" ");
display.println(pressureValue2, 0);
//Serial.print(pressureValue2);
//Serial.print(" ");
// LR
display.setTextSize(1);
display.setCursor(10, 50);
display.println("LR");
display.setCursor(4, 70);
display.print(" ");
display.println(pressureValue3, 0);
//Serial.print(pressureValue3);
//Serial.print(" ");
// RR
display.setTextSize(1);
display.setCursor(45, 50);
display.println("RR");
display.setCursor(39, 70);
display.print(" ");
display.println(pressureValue4, 0);
//Serial.println(pressureValue4);
//Serial.print(" ");
// TANK
display.setTextSize(1);
display.setCursor(22, 90);
display.println("TANK");
display.setCursor(22, 110);
display.print(" ");
display.println(pressureValue5, 0);
display.display(); // Display the content in the buffer
}
}
/////////////////////////////////////////
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
if (key == '1') {
digitalWrite(relayPin1,LOW);
}
break;
case RELEASED:
if (key == '1') {
digitalWrite(relayPin1,HIGH);
}
break;
}
/* FRONT UP */
switch (keypad.getState()){
case PRESSED:
if (key == '2') {
digitalWrite(relayPin1,LOW);
digitalWrite(relayPin3,LOW);
}
break;
case RELEASED:
if (key == '2') {
digitalWrite(relayPin1,HIGH);
digitalWrite(relayPin3,HIGH);
}
break;
}
switch (keypad.getState()){
case PRESSED:
if (key == '3') {
digitalWrite(relayPin3,LOW);
}
break;
case RELEASED:
if (key == '3') {
digitalWrite(relayPin3,HIGH);
}
break;
}
switch (keypad.getState()){
case PRESSED:
if (key == '4') {
digitalWrite(relayPin2,LOW);
}
break;
case RELEASED:
if (key == '4') {
digitalWrite(relayPin2,HIGH);
}
break;
}
/* FRONT DOWN */
switch (keypad.getState()){
case PRESSED:
if (key == '5') {
digitalWrite(relayPin2,LOW);
digitalWrite(relayPin4,LOW);
}
break;
case RELEASED:
if (key == '5') {
digitalWrite(relayPin2,HIGH);
digitalWrite(relayPin4,HIGH);
}
break;
}
switch (keypad.getState()){
case PRESSED:
if (key == '6') {
digitalWrite(relayPin4,LOW);
}
break;
case RELEASED:
if (key == '6') {
digitalWrite(relayPin4,HIGH);
}
break;
}
switch (keypad.getState()){
case PRESSED:
if (key == '7') {
digitalWrite(relayPin5,LOW);
}
break;
case RELEASED:
if (key == '7') {
digitalWrite(relayPin5,HIGH);
}
break;
}
/* REAR UP */
switch (keypad.getState()){
case PRESSED:
if (key == '8') {
digitalWrite(relayPin5,LOW);
digitalWrite(relayPin7,LOW);
}
break;
case RELEASED:
if (key == '8') {
digitalWrite(relayPin5,HIGH);
digitalWrite(relayPin7,HIGH);
}
break;
}
switch (keypad.getState()){
case PRESSED:
if (key == '9') {
digitalWrite(relayPin7,LOW);
}
break;
case RELEASED:
if (key == '9') {
digitalWrite(relayPin7,HIGH);
}
break;
}
switch (keypad.getState()){
case PRESSED:
if (key == '*') {
digitalWrite(relayPin6,LOW);
}
break;
case RELEASED:
if (key == '*') {
digitalWrite(relayPin6,HIGH);
}
break;
}
/* REAR DOWN */
switch (keypad.getState()){
case PRESSED:
if (key == '0') {
digitalWrite(relayPin6,LOW);
digitalWrite(relayPin8,LOW);
}
break;
case RELEASED:
if (key == '0') {
digitalWrite(relayPin6,HIGH);
digitalWrite(relayPin8,HIGH);
}
break;
}
switch (keypad.getState()){
case PRESSED:
if (key == '#') {
digitalWrite(relayPin8,LOW);
}
break;
case RELEASED:
if (key == '#') {
digitalWrite(relayPin8,HIGH);
}
break;
}
// Check if key 'A', 'B', 'C' & 'D' is pressed and the pressure is below the preferred PSI
if (key){
//Serial.print("Received key: ");
Serial.print(key);
switch (key) {
case 'A':
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin5, LOW);
digitalWrite(relayPin7, LOW);
break;
case 'B':
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin5, LOW);
digitalWrite(relayPin7, LOW);
break;
case 'C':
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin5, LOW);
digitalWrite(relayPin7, LOW);
break;
case 'D':
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin4, LOW);
digitalWrite(relayPin6, LOW);
digitalWrite(relayPin8, LOW);
break;
/*default:
break;*/
}
}
// AA //
if (pressureValue1 >= preferredPSI_A1){
digitalWrite(relayPin1, HIGH);
Serial.print(preferredPSI_A1);
Serial.print(", ");
}
if (pressureValue2 >= preferredPSI_A2){
digitalWrite(relayPin3, HIGH);
Serial.print(preferredPSI_A2);
Serial.print(", ");
}
if (pressureValue3 >= preferredPSI_A3){
digitalWrite(relayPin5, HIGH);
Serial.print(preferredPSI_A3);
Serial.print(", ");
}
if (pressureValue4 >= preferredPSI_A4){
digitalWrite(relayPin7, HIGH);
Serial.print(preferredPSI_A4);
Serial.print(", ");
}
// BB //
if (pressureValue1 >= preferredPSI_B1){
digitalWrite(relayPin1, HIGH);
Serial.print(preferredPSI_B1);
Serial.print(", ");
}
if (pressureValue2 >= preferredPSI_B2){
digitalWrite(relayPin3, HIGH);
Serial.print(preferredPSI_B2);
Serial.print(", ");
}
if (pressureValue3 >= preferredPSI_B3){
digitalWrite(relayPin5, HIGH);
Serial.print(preferredPSI_B3);
Serial.print(", ");
}
if (pressureValue4 >= preferredPSI_B4){
digitalWrite(relayPin7, HIGH);
Serial.print(preferredPSI_B4);
Serial.print(", ");
}
// CC //
if (pressureValue1 >= preferredPSI_C1){
digitalWrite(relayPin1, HIGH);
Serial.print(preferredPSI_C1);
Serial.print(", ");
}
if (pressureValue2 >= preferredPSI_C2){
digitalWrite(relayPin3, HIGH);
Serial.print(preferredPSI_C2);
Serial.print(", ");
}
if (pressureValue3 >= preferredPSI_C3){
digitalWrite(relayPin5, HIGH);
Serial.print(preferredPSI_C3);
Serial.print(", ");
}
if (pressureValue4 >= preferredPSI_C4){
digitalWrite(relayPin7, HIGH);
Serial.print(preferredPSI_C4);
Serial.print(", ");
}
// DD //
if (pressureValue1 <= preferredPSI_D1){
digitalWrite(relayPin2, HIGH);
Serial.print(preferredPSI_D1);
Serial.print(", ");
}
if (pressureValue2 <= preferredPSI_D2){
digitalWrite(relayPin4, HIGH);
Serial.print(preferredPSI_D2);
Serial.print(", ");
}
if (pressureValue3 <= preferredPSI_D3){
digitalWrite(relayPin6, HIGH);
Serial.print(preferredPSI_D3);
Serial.print(", ");
}
if (pressureValue4 <= preferredPSI_D4){
digitalWrite(relayPin8, HIGH);
Serial.println(preferredPSI_D4);
}
}