Need help with this code!
It prints
11_1
12_1
In the serial monitor
for (int port = 2; port <= 12; port++)
{
if (digitalRead(port) != DigitalInOld[port]) {
DigitalInOld[port] = digitalRead(port); // save the changed value
Serial.print(port);
Serial.print("_");
Serial.print(digitalRead(port));
Serial.print("\n");
delay(5);
}
}
This code is not working either.
It writes:
inc5000
spd1100
fed0110
If i turn the potentiometers it still sends every loop
if (switchPosition != switchPositionOld) {
switchPositionOld = switchPosition; // save the changed value
Serial.print(mode + mod + linebreak);
//Serial.println(Switch1 + switchPosition + Switch2 + switchPositionOld);
}
if (switchPosition1 != switchPosition1Old) {
switchPosition1Old = switchPosition1; // save the changed value
Serial.print(spd + jog_speed + linebreak);
Serial.print(fed + feed + linebreak);
}
Full code!
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <SoftwareSerial.h>
Adafruit_MCP23017 mcp1;
Adafruit_MCP23017 mcp2;
String jog_speed;
String feed;
String mode;
String mod = "";
String linebreak = "\n";
String spd = "spd";
String fed = "fed";
int switchPosition ;
int switchPosition1;
int switchPositionOld;
int switchPosition1Old;
int DigitalInOld[11];
int mcp1_DigitalInOld[16];
int mcp2_DigitalInOld[16];
// predef
void sendCommand(char);
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
for (int port = 2; port <= 12; port++) {
pinMode(port, INPUT_PULLUP);
DigitalInOld[port] = 1;
}
//Wire.setClock(400000L)
mcp1.begin(0); // Default device address 0 = 0x20 1 = 0x21
mcp2.begin(1); // Default device address 0 = 0x20 1 = 0x21
for (int p = 0; p < 16; p++)
{
mcp1.pinMode(p, INPUT);
mcp1.pullUp(p, HIGH); // Pulled high to ~100k
mcp2.pinMode(p, INPUT);
mcp2.pullUp(p, HIGH); // Pulled high to ~100k
mcp1_DigitalInOld[p] = 1;
mcp2_DigitalInOld[p] = 1;
}
}
String mess1 = "1_";
String mess2 = "2_";
String mess3 = "Port ";
String mess4 = "Av/på ";
// the loop routine runs over and over again forever:
void loop() {
checkPorts();
potentiometerSettings(analogRead(A0), analogRead(A1));
if (Serial.available()) {
char input = Serial.read();
//sendCommand(input);
}
}
void checkPorts()
{
for (int port = 2; port <= 12; port++)
{
if (digitalRead(port) != DigitalInOld[port]) {
DigitalInOld[port] = digitalRead(port); // save the changed value
Serial.print(port);
Serial.print("_");
Serial.print(digitalRead(port));
Serial.print("\n");
delay(5);
}
}
for (int p = 0; p < 16; p++)
{
if (mcp1.digitalRead(p) != mcp1_DigitalInOld[p]) {
mcp1_DigitalInOld[p] = mcp1.digitalRead(p); // save the changed value
Serial.print(mess1 + p);
Serial.print("_");
Serial.print(mcp1.digitalRead(p));
Serial.print("\n");
delay(5);
}
if (mcp2.digitalRead(p) != mcp2_DigitalInOld[p]) {
mcp2_DigitalInOld[p] = mcp2.digitalRead(p); // save the changed value
Serial.print(mess2 + p);
Serial.print("_");
Serial.print(mcp2.digitalRead(p));
Serial.print("\n");
delay(5);
}
}
}
void potentiometerSettings(int sensorValue, int sensorValue1)
{
//---------------------Potentiometer1---------------------
if (sensorValue >= 0 && sensorValue <= 255) {
switchPosition = 10; //10.00
mode = "inc";
mod = "5000";
}
else if (sensorValue > 255 && sensorValue <= 350) {
switchPosition = 9;//1.00
mode = "inc";
mod = "1000";
}
else if (sensorValue > 350 && sensorValue <= 439) {
switchPosition = 8; //100
mode = "inc";
mod = "0500";
}
else if (sensorValue >= 439 && sensorValue <= 526) {
switchPosition = 7; //10
mode = "inc";
mod = "0100";
}
else if (sensorValue > 526 && sensorValue <= 614) {
switchPosition = 6; // 1
mode = "inc";
mod = "0050";
}
else if (sensorValue > 614 && sensorValue <= 704) {
switchPosition = 5; // 1
mode = "inc";
mod = "0010";
}
else if (sensorValue > 704 && sensorValue <= 800) {
switchPosition = 4; //Jog på
mode = "inc";
mod = "0005";
}
else if (sensorValue > 800 && sensorValue <= 896) {
switchPosition = 3; //MDI
mode = "mdi";
mod = "9000";
}
else if (sensorValue >= 896 && sensorValue <= 983) {
switchPosition = 2; //manual
mode = "man";
mod = "9000";
}
else if (sensorValue > 983 && sensorValue <= 1050) {
switchPosition = 1; //Auto
mode = "aut";
mod = "9000";
}
//---------------------Potentiometer2---------------------(0-100%
if (sensorValue1 >= 0 && sensorValue1 <= 271) {
switchPosition1 = 15; //110%
jog_speed = "1100";
feed = "0110";
}
else if (sensorValue1 > 271 && sensorValue1 <= 327) {
switchPosition1 = 14; //100%
jog_speed = "1000";
feed = "0100";
}
else if (sensorValue1 > 327 && sensorValue1 <= 383) {
switchPosition1 = 13;
jog_speed = "0900";
feed = "0095";
}
else if (sensorValue1 >= 383 && sensorValue1 <= 436) {
switchPosition1 = 12;
jog_speed = "0800";
feed = "0090";
}
else if (sensorValue1 > 436 && sensorValue1 <= 490) {
switchPosition1 = 11;
jog_speed = "0700";
feed = "0085";
}
else if (sensorValue1 > 490 && sensorValue1 <= 544) {
switchPosition1 = 10;
jog_speed = "0600";
feed = "0080";
}
else if (sensorValue1 > 544 && sensorValue1 <= 595) {
switchPosition1 = 9;
jog_speed = "0500";
feed = "0075";
}
else if (sensorValue1 > 595 && sensorValue1 <= 647) {
switchPosition1 = 8;
jog_speed = "0400";
feed = "0070";
}
else if (sensorValue1 >= 647 && sensorValue1 <= 700) {
switchPosition1 = 7;
jog_speed = "0300";
feed = "0060";
}
else if (sensorValue1 > 700 && sensorValue1 <= 756) {
switchPosition1 = 6;
jog_speed = "0200";
feed = "0050";
}
else if (sensorValue1 > 756 && sensorValue1 <= 812) {
switchPosition1 = 5;
jog_speed = "0100";
feed = "0040";
}
else if (sensorValue1 > 812 && sensorValue1 <= 866) {
switchPosition1 = 4;
jog_speed = "0050";
feed = "0030";
}
else if (sensorValue1 > 866 && sensorValue1 <= 920) {
switchPosition1 = 3;
jog_speed = "0025";
feed = "0020";
}
else if (sensorValue1 >= 920 && sensorValue1 <= 987) {
switchPosition1 = 2;
jog_speed = "0012";
feed = "0010";
}
else if (sensorValue1 > 987 && sensorValue1 <= 1050) {
switchPosition1 = 1;//2%
jog_speed = "0006";
feed = "0010";
}
// print out the value you read:
String Switch1 = "Switch1: ";
String Switch2 = " Switch1Old: ";
//Serial.println(Switch1 + switchPosition);
//Serial.println(Switch2 + switchPosition1);
if (switchPosition != switchPositionOld) {
switchPositionOld = switchPosition; // save the changed value
Serial.print(mode + mod + linebreak);
//Serial.println(Switch1 + switchPosition + Switch2 + switchPositionOld);
}
if (switchPosition1 != switchPosition1Old) {
switchPosition1Old = switchPosition1; // save the changed value
Serial.print(spd + jog_speed + linebreak);
Serial.print(fed + feed + linebreak);
}
//Serial.println(sensorValue1);
delay(5); // delay in between reads for stability
}