Arduino IDE Blinking led when using multiple sensors/if else statements - arduino

I'm trying to get my LED to blink if multiple conditions are met.
The following code works fine without the teller1 part inside the if else statement, but when i add the elapsedmillis counter to the if else statement the led doesn't blink, even when all of the conditions are met.
Any help?
Here's my code:
#define trigPin D7
#define echoPin D8
#include <elapsedMillis.h>
elapsedMillis teller1;
elapsedMillis teller2;
const int RED = D5; //het ‘rode’ pootje zit in D5
const int GREEN = D4; //etc.
const int BLUE = D3;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(D6, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop() {
if (teller1 > 500) {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
if ((sensorValue > 20) && (distance > 20) && (teller1 < 250)) {
analogWrite(RED, 255);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
} else if ((sensorValue > 20) && (distance > 20) && (teller1 > 250)) {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}
else if (sensorValue <20) {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}
teller1 = 0;
}
delay(50);
}
EDIT: I think i've fixed it, but i'm not sure this would be considered well written or the proper way of doing this:
#define trigPin D7 //trigger pin voor de echo sensor definieren op D7
#define echoPin D8 //echo pin voor de echo sensor definieren op D8
#include <elapsedMillis.h> //elapsedMillis library aanroepen
elapsedMillis teller1; //teller1 aanmaken
elapsedMillis teller2; //teller 2 aanmaken
const int RED = D5; //het ‘rode’ pootje zit in D5
const int GREEN = D4; //etc.
const int BLUE = D3;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); //confirugeer D7 als output
pinMode(echoPin, INPUT); //confirugeer D8 als input
pinMode(D6, OUTPUT); //confirugeer D6 als output
pinMode(RED, OUTPUT); //confirugeer D5 als output
pinMode(GREEN, OUTPUT); //confirugeer D4 als output
pinMode(BLUE, OUTPUT); //confirugeer D3 als output
}
void loop() {
if (teller1 > 500) { //als de timer boven 500ms komt
int sensorValue = analogRead(A0); //sla de sensorwaarde op
Serial.println(sensorValue); //druk de sensorwaarde af in de serial monitor
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1; //formule om afstand te berekenen
Serial.print(distance);
Serial.println(" cm");
if ((sensorValue > 20) && (distance < 20) && (teller1 < 750)) {
analogWrite(RED, 255);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
digitalWrite(D6, HIGH);
}
else if (sensorValue <20) {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}
teller1 = 0;
} else if (teller1 < 250) {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
digitalWrite(D6, LOW);
}
delay(50);
Serial.print("teller");
Serial.println(teller1);
}

Your void loop() starts with if (teller1 > 500) {.
Then, when checking the sensor values it says
if ((sensorValue > 20) && (distance > 20) && (teller1 < 250)) {
analogWrite(RED, 255);
teller1 can't be >500 and <250 at the same time.
Edit after comment:
void loop(){
if (teller1 > 500) {
//read sensor values
teller1 = 0;
}
if (sensorValue > 20 && distance > 20) {
//power led
teller2 = 0;
}
if (sensorValue <20 || teller2 > 250){
//unpower led
}
}

Related

Motors With Arduino's

I have been working on getting some motors to work through H-bridges and managed to with this code.
// initialise motors
int enA = 3; // Motor 1
int in1 = 4;
int in2 = 2;
int enB = 5; // Motor 2
int in3 = 8;
int in4 = 7;
int enC = 11; // Motor 3
int in5 = 12;
int in6 = 13;
int enD = 6; // Motor 4
int in7 = 9;
int in8 = 10;
void setup()
{
pinMode(enA, OUTPUT); // set the outputs for motors
pinMode(enB, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(enD, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
pinMode(in7, OUTPUT);
pinMode(in8, OUTPUT);
}
void motorLoop(){
// setting the direction to turn and speed
digitalWrite(in1, HIGH); // Motor 1
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); // Motor 2
digitalWrite(in4, LOW);
digitalWrite(in5, HIGH); // Motor 3
digitalWrite(in6, LOW);
digitalWrite(in7, HIGH); // Motor 4
digitalWrite(in8, LOW);
// Set the speed for the Motors
analogWrite(enA, 1);
analogWrite(enB, 20);
analogWrite(enC, 100);
analogWrite(enD, 200);
};
void loop()
{
motorLoop();
delay(500);
}
However I am trying to turn the data into an array and have hit some issues.
I have never tried creating arrays with Digital inputs but have with Analog ones.
Here is a link to my project on (TinkerCAD) https://www.tinkercad.com/things/fFQKRTjhDrb-smashing-allis-kieran/editel?tenant=circuits?sharecode=6rKnUZsFtcOAetd_TufIuN8TfUgi8EupA1TMjlxiacM=
As you can see by this code I have tried to setup the enable inputs which show no errors but I am struggling setting up the OUTPUT and Speed that the motors rotate at.
// initialise motors
// Motor 1
int in1 = 4;
int in2 = 2;
// Motor 2
int in3 = 8;
int in4 = 7;
// Motor 3
int in5 = 12;
int in6 = 13;
// Motor 4
int in7 = 9;
int in8 = 10;
// Array of PWM's
int i = 0;
byte pwms[i] = {3,5,6,11};
byte numberPwms = 4;
void setup()
{
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
pinMode(in7, OUTPUT);
pinMode(in8, OUTPUT);
for(byte i = 0; i <= numberPwms; i++){
pinMode(pwms[i], OUTPUT);
};
}
void motorLoop(){
// setting the direction to turn and speed
digitalWrite(in1, HIGH); // Motor 1
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); // Motor 2
digitalWrite(in4, LOW);
digitalWrite(in5, HIGH); // Motor 3
digitalWrite(in6, LOW);
digitalWrite(in7, HIGH); // Motor 4
digitalWrite(in8, LOW);
// Set the speed for the Motors
for(byte i = 0; i < numberPwms; i++){
analogWrite(pwms[i], 200);
};
};
void loop()
{
motorLoop();
delay(500);
}
Any help with this would be greatly appreciated :D
Your for loop counts 5 times. Try changing it to this:
for (byte i = 0; i < numberPwms; i++) {
pinMode(pwms[i], OUTPUT);
};
Also your array pwms[] is initialized with 0 elements, but should work anyway. But you can change it.
uint8_t pwms[] = {3, 5, 6, 11};

Arduino lcd screen showing broken characters at random times

So I've been working on an arduino project for an escape room puzzle. It's essentially a bomb with a timer on an lcd screen. After all of the elements of the bomb are solved the timer stops and the lcd screen displays that the bomb has been defused and gives the group a clue to the next puzzle. 9 times out of 10 it works perfectly. But every once in a while when it is supposed to display that the bomb is defused the lcd screen just shows random broken characters. I haven't had any luck diagnosing the problem. Hoping somebody here might have an idea.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#define pound 14
Tone tone1;
int Scount = 0;
int Mcount = 0;
int Hcount = 1;
int DefuseTimer = 0;
long secMillis = 0;
long interval = 1000;
char password[6] = "594432";
int currentLength = 0;
int i = 0;
char entered[6];
int ledPin = 23;
int ledPin2 = 25;
int ledPin3 = 27;
int ledPin4 = 29;
int ledPin5 = 31;
int ledPin6 = 34;
const int plugin1 = 44;
const int plugin2 = 46;
const int plugin3 = 48;
const int plugin4 = 50;
const int plugin5 = 52;
int plugin1State = 0;
int plugin2State = 0;
int plugin3State = 0;
int plugin4State = 0;
int plugin5State = 0;
const int switch1 = 37;
int switch1State = 0;
const int key1 = 40;
int key1State = 0;
int puzzle1 = 0;
int puzzle2 = 0;
int puzzle3 = 0;
int solved = 0;
LiquidCrystal lcd(7, 8, 10, 11, 12, 13);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, A6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(plugin1, INPUT);
pinMode(plugin2, INPUT);
pinMode(plugin3, INPUT);
pinMode(plugin4, INPUT);
pinMode(plugin5, INPUT);
digitalWrite(plugin1, HIGH);
digitalWrite(plugin2, HIGH);
digitalWrite(plugin3, HIGH);
digitalWrite(plugin4, HIGH);
digitalWrite(plugin5, HIGH);
pinMode(switch1, INPUT);
digitalWrite(switch1, HIGH);
pinMode(key1, INPUT_PULLUP);
digitalWrite(key1, HIGH);
tone1.begin(9);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
tone1.play(NOTE_E6, 200);
delay(3000);
lcd.clear();
currentLength = 0;
}
void loop()
{
timer();
plugin1State = digitalRead(plugin1);
plugin2State = digitalRead(plugin2);
plugin3State = digitalRead(plugin3);
plugin4State = digitalRead(plugin4);
plugin5State = digitalRead(plugin5);
if (plugin1State == LOW && plugin2State == LOW && plugin3State == LOW && plugin4State == LOW && plugin5State == LOW)
{
puzzle1 = 1;
}
if (puzzle1 == 1)
{
digitalWrite(ledPin4, HIGH);
switch1State = digitalRead(switch1);
if (switch1State == LOW)
{
puzzle2 = 1;
}
}
if (puzzle2 == 1)
{
digitalWrite(ledPin5, HIGH);
key1State = digitalRead(key1);
if (key1State == LOW)
{
puzzle3 = 1;
}
if (key1State == HIGH)
{
digitalWrite(ledPin6, LOW);
}
}
if (puzzle3 == 1)
{
digitalWrite(ledPin6, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
while (currentLength < 6)
{
timer();
char key2 = keypad.getKey();
if (key2 == "#")
{
currentLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
}
else if (key2 != NO_KEY)
{
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
lcd.print(key2);
entered[currentLength] = key2;
currentLength++;
tone1.play(NOTE_C6, 200);
delay(100);
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}
if (currentLength == 6)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4] && entered[5] == password[5])
{
solved = 1;
while (solved == 1)
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("BOMB 1 DEFUSED");
currentLength = 0;
digitalWrite(ledPin3, HIGH);
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("RELEASE");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("TOXIC GAS");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("CLUE: %&#$#");
delay(6000);
}
}
else
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Password!");
delay(1500);
currentLength = 0;
}
}
}
}
void timer()
{
if (Hcount <= 0)
{
if ( Mcount < 0 )
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("The Bomb Has ");
lcd.setCursor (0, 1);
lcd.print("Exploded!");
while (Mcount < 0)
{
digitalWrite(ledPin, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
}
}
}
lcd.setCursor (0, 1); // sets cursor to 2nd line
lcd.print ("Timer:");
if (Hcount >= 10)
{
lcd.setCursor (7, 1);
lcd.print (Hcount);
}
if (Hcount < 10)
{
lcd.setCursor (7, 1);
lcd.write ("0");
lcd.setCursor (8, 1);
lcd.print (Hcount);
}
lcd.print (":");
if (Mcount >= 10)
{
lcd.setCursor (10, 1);
lcd.print (Mcount);
}
if (Mcount < 10)
{
lcd.setCursor (10, 1);
lcd.write ("0");
lcd.setCursor (11, 1);
lcd.print (Mcount);
}
lcd.print (":");
if (Scount >= 10)
{
lcd.setCursor (13, 1);
lcd.print (Scount);
}
if (Scount < 10)
{
lcd.setCursor (13, 1);
lcd.write ("0");
lcd.setCursor (14, 1);
lcd.print (Scount);
}
if (Hcount < 0)
{
Hcount = 0;
}
if (Mcount < 0)
{
Hcount --;
Mcount = 59;
}
if (Scount < 1) // if 60 do this operation
{
Mcount --; // add 1 to Mcount
Scount = 59; // reset Scount
}
if (Scount > 0) // do this oper. 59 times
{
unsigned long currentMillis = millis();
if (currentMillis - secMillis > interval)
{
tone1.play(NOTE_G5, 200);
secMillis = currentMillis;
Scount --; // add 1 to Scount
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(10); // waits for a second
digitalWrite(ledPin2, LOW); // sets the LED off
delay(10); // waits for a second
//lcd.clear();
}
}
}
That would most likely be a an electrical issues either with your circuit of your LCD. The fact that a simple code like yours (not trying to insult you in any way) works 9/10 of times means that there is probably nothing wrong with the code (I've seen none).
For quick check, try reinstalling the IDE. That might update the Arduino libraries that get downloaded with the IDE. I doubt that it would solve the issue but that is an quick free easy way to try
I would personnaly suggest to disconnect everything, and reconnecting them. And if it doesn't work, then return that LCD and get yourself a new one.
I had the same issue when I used a non shielded 1m ribbon cable for the 16x2 LCD. The main board with the MCU had to be placed further away from the display. It showed random character for any electromagnetic interference. For example, turning on a fluorescent tube, starting an electric screwdriver, relay switches etc. Shortening the ribon cable solved the problem.
Anyway, the LCD is sensitive to electromagnetic interference. Keep it away from these sources.

Receiving SMS issue with SIM900 on arduino

We're having an issue about receiving an SMS with SIM900. According to our program, SMS that we sent is must be in a certain format to be accepted. For example sending #m1 to Arduino must switch a green LED on and any other texts must be ignored by the program. But now our program accepting all the texts regardless of what is the content of the text and switches on or off the red LED which one is irrelevant.
Can you guys have a look at the codes and find out where is the problem.
#include <SoftwareSerial.h>
SoftwareSerial SIM900(2, 3);
char inchar;
int motor = 4;
int heater = 12;
int sensor = 11;
int counter1;
int heat_degreePin = A0;
int counter;
int buttonState = 0;
String message = "";
void setup()
{
Serial.begin(19200);
pinMode(motor, OUTPUT);
pinMode(sensor, INPUT);
pinMode(heater, OUTPUT);
analogReference(INTERNAL);
digitalWrite(heater, LOW);
digitalWrite(motor, LOW);
int counter1 = 0;
int counter = 0;
SIM900power();
SIM900.begin(19200);
delay(20000);
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
void loop()
{
int voltage_value = analogRead(heat_degreePin);
float heat_degree = heat_degree = voltage_value / 9.31;
voltage_value = analogRead(heat_degreePin);
heat_degree = voltage_value / 9.31;
buttonState = digitalRead(sensor);
if (heat_degree < 36 && buttonState == LOW && counter != 1) {
digitalWrite(heater, HIGH);
delay(50);
digitalWrite(motor, LOW);
delay(50);
message = "D1: heat low: heater on ,moist ok: motor off";
delay(50);
SendTextMessage();
counter = 1;
}
else if (heat_degree >= 36 && buttonState == LOW && counter != 2) {
digitalWrite(heater, LOW);
delay(50);
digitalWrite(motor, LOW);
delay(50);
message = "D2: heat ok: heater off, moist ok: motor off";
delay(50);
SendTextMessage();
counter = 2;
}
else if (heat_degree < 36 && buttonState == HIGH && counter != 3) {
digitalWrite(heater, HIGH);
delay(50);
digitalWrite(motor, HIGH);
delay(50);
message = "D3: heat low: heater on, moist low: motor on";
delay(50);
SendTextMessage();
counter = 3;
}
else if (heat_degree >= 36 && buttonState == HIGH && counter != 4) {
digitalWrite(heater, LOW);
delay(50);
digitalWrite(motor, HIGH);
delay(50);
message = "D4: heat ok: heater off, moist low: motor on";
delay(50);
SendTextMessage();
counter = 4;
}
else {
}
if (SIM900.available() > 0) {
inchar = SIM900.read();
if (inchar == '#') {
delay(10);
inchar = SIM900.read();
if (inchar == 'm') {
delay(10);
inchar = SIM900.read();
if (inchar == '0') {
digitalWrite(motor, LOW);
delay(10);
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"+90505xxxxxxx\"\r");
delay(100);
SIM900.println("motor off");
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
SIM900power();
}
else if (inchar == '1') {
digitalWrite(motor, HIGH);
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"+90505xxxxxxx\"\r");
delay(100);
SIM900.println("motor on");
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
SIM900power();
}
SIM900.println("AT+CMGD=1,4");
}
}
}
}
void SIM900power()
{
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(1000);
}
void SendTextMessage()
{
SIM900.print("AT+CMGF=1\r");
delay(500);
SIM900.print("AT+CMGS=\"+90505xxxxxxx\"\r");
delay(500);
SIM900.println(message);
SIM900.print("\r");
delay(500);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
SIM900power();
delay(3000);
}

I m transmitting flex sensor values through serial port of arduino using Serial.print() function but I'm not able to read it at the receiving end

Actually in my code I'm transmitting accelerometer values as well as 4 flex sensor values. I have converted accelerometer value to 4 state as F(forward), B(backward),R(right),L(left). I am able to receive this 4 states and also I'm able to write code for these states. With these states I'm also sending flex sensor values to control servo motor remotely. But I'm not able to read those flex values as it is a varying integer. Please help me to read the flex values. I have tried Serial.read(), Serial.parseInt().
My transmitter code is:
int xpin = A0;
int x;
int ypin = A1;
int y;
int vcc=13;
const int flexpin1 = 2;
const int flexpin2 = 3;
const int flexpin3 = 4;
const int flexpin4 = 5;
int flex1[20];
int flex2[20];
int flex3[20];
int flex4[20];
int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;
char state1 = 'S';
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(flexpin1, INPUT);
pinMode(flexpin2, INPUT);
pinMode(flexpin3, INPUT);
pinMode(flexpin4, INPUT);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(vcc, OUTPUT);
digitalWrite(vcc, HIGH);
}
void loop() {
x = analogRead(xpin);
Serial.print("xpin=");
Serial.print(x);
y = analogRead(ypin);
Serial.print("\t ypin=");
Serial.print(y);
if ( y > 415)
{
state1 = 'F';
}
else if (y < 315)
{
state1 = 'B';
}
else if (x > 410)
{
state1 = 'R';
}
else if (x < 310)
{
state1 = 'L';
}
else {
state1 = 'S';
}
// flex readings stabelized//
for(int x=0; x<25; x++)
{
flex1[x]=analogRead(flexpin1);
flex2[x]=analogRead(flexpin2);
flex3[x]=analogRead(flexpin3);
flex4[x]=analogRead(flexpin4);
flexsum1=flexsum1+flex1[x];
flexsum2=flexsum2+flex2[x];
flexsum3=flexsum3+flex3[x];
flexsum4=flexsum4+flex4[x];
delayMicroseconds(20);
}
flexsum1=flexsum1/25;
flexsum2=flexsum2/25;
flexsum3=flexsum3/25;
flexsum4=flexsum4/25;
Serial.print("\t\t\tflexsum1= ");
Serial.print(flexsum1);
Serial.print("\tflexsum2= ");
Serial.print(flexsum2);
Serial.print("\tflexsum3= ");
Serial.print(flexsum3);
Serial.print("\tflexsum4= ");
Serial.print(flexsum4);
Serial.print("\t state1=");
Serial.println(state1);
delay(200);
}
My receiving code:
#include<Servo.h>
int IN1 = 13;
int IN2 = 12;
int IN3 = 7;
int IN4 = 6;
char state1 = 'S';
Servo servo1, servo2, servo3, servo4;
int flex1, flex2, flex3, flex4;
int angle1, angle2, angle3, angle4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
servo1.attach(9);
servo2.attach(8);
servo3.attach(10);
servo4.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()) {
delay(10);
state1 = Serial.read();
Serial.print(state1);
switch (state1)
{
case 'F' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is moving forward ");
delay(100);
break;
case 'B' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is moving backward ");
delay(100);
break;
case 'L' : digitalWrite(IN1 , HIGH);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , HIGH);
Serial.print("\tcar is turning left ");
delay(100);
break;
case 'R' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , HIGH);
digitalWrite(IN3 , HIGH);
digitalWrite(IN4 , LOW);
Serial.print("\tcar is turning right");
delay(100);
break;
case 'S' : digitalWrite(IN1 , LOW);
digitalWrite(IN2 , LOW);
digitalWrite(IN3 , LOW);
digitalWrite(IN4 , LOW);
delay(100);
break;
}
if (flex1 >= 845)
{
flex1 = Serial.read();
angle1 = map(flex1, 848, 1000, 0, 180);
angle1 = constrain(angle1, 0, 90);
servo1.write(angle1);
Serial.print("\tangle1=");
Serial.print(angle1);
delay(200);
}
if (flex2 >= 820)
{
flex2 = Serial.read();
angle2 = map(flex2, 825, 1000, 0, 180);
angle2 = constrain(angle2, 0, 90);
servo2.write(angle2);
Serial.print("\tangle2=");
Serial.print(angle2);
delay(200);
}
if (flex3 >= 770)
{
flex3 = Serial.read();
angle3 = map(flex3, 770, 930, 0, 180);
angle3 = constrain(angle3, 90, 180);
servo3.write(angle3);
Serial.print("\tangle3=");
Serial.print(angle3);
delay(200);
}
if (flex4 >= 870)
{
flex4 = Serial.read();
angle4 = map(flex4, 875, 1020, 0, 180);
angle4 = constrain(angle4, 90, 180);
servo4.write(angle4);
Serial.print("\tangle4=");
Serial.print(angle4);
delay(200);
}
}
}

issue with loop after invoking void function arduino

i've made simple program that is ligtning the RGB diod, and receiving info via i2c from keypad.
i have weird issue where i2c is not working right after invoking void function. when i switch functions with regular loop then same situation happens.
first i thought that it was connected with analog4 and analog5 port, but after changing functions it became obvious that this is not related.
after commenting Potentiometer(i) in loop method keypad is working as a charm
beneath i'm putting code
#include <Wire.h>
#include "Adafruit_MPR121.h"
Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
int RGB1[] = { 22, 23, 24 };
int RGB2[] = { 25, 26, 27 };
int RGB3[] = { 28, 29, 30 };
int RGB4[] = { 31, 32, 33 };
void setup()
{
for (int i = 0; i < 3; i++){
pinMode(RGB1[i], OUTPUT);
digitalWrite(RGB1[i], HIGH);
pinMode(RGB2[i], OUTPUT);
digitalWrite(RGB2[i], HIGH);
pinMode(RGB3[i], OUTPUT);
digitalWrite(RGB3[i], HIGH);
pinMode(RGB4[i], OUTPUT);
digitalWrite(RGB4[i], HIGH);
}
Serial.begin(9600);
Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
}
void loop()
{
currtouched = cap.touched();
for (uint8_t i=0; i<12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" touched");
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" released");
}
}
// reset our state
lasttouched = currtouched;
Potentiometer(0);
Potentiometer(1);
Potentiometer(2);
Potentiometer(3);
// comment out this line for detailed data from the sensor!
return;
// debugging info, what
Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
Serial.print("Filt: ");
for (uint8_t i=0; i<12; i++) {
Serial.print(cap.filteredData(i)); Serial.print("\t");
}
Serial.println();
Serial.print("Base: ");
for (uint8_t i=0; i<12; i++) {
Serial.print(cap.baselineData(i)); Serial.print("\t");
}
Serial.println();
delay(100);
}
void Potentiometer(int i){
int j = analogRead(i);
switch (i+1){
case 1:
detectColor(RGB1, j);
break;
case 2:
detectColor(RGB2, j);
break;
case 3:
detectColor(RGB3, j);
break;
case 4:
detectColor(RGB4, j);
break;
}
}
void detectColor(int i[], int j){
if (j < 25)
RGB_color(i, 0);
if (j > 25 && j < 195)
RGB_color(i, 1);
if (j > 195 && j < 365)
RGB_color(i, 2);
if (j > 365 && j < 535)
RGB_color(i, 3);
if (j > 535 && j < 705)
RGB_color(i, 4);
if (j > 705 && j < 875)
RGB_color(i, 5);
if (j > 875 && j < 1010)
RGB_color(i, 6);
if (j > 1010)
RGB_color(i, 7);
}
void RGB_color(int dioda[], int color)
{
switch (color){
case 0:
digitalWrite(dioda[0], HIGH);
digitalWrite(dioda[1], HIGH);
digitalWrite(dioda[2], HIGH);
break;
case 1:
digitalWrite(dioda[0], LOW);
digitalWrite(dioda[1], HIGH);
digitalWrite(dioda[2], HIGH);
break;
case 2:
digitalWrite(dioda[0], HIGH);
digitalWrite(dioda[1], LOW);
digitalWrite(dioda[2], HIGH);
break;
case 3:
digitalWrite(dioda[0], HIGH);
digitalWrite(dioda[1], HIGH);
digitalWrite(dioda[2], LOW);
break;
case 4:
digitalWrite(dioda[0], LOW);
digitalWrite(dioda[1], LOW);
digitalWrite(dioda[2], HIGH);
break;
case 5:
digitalWrite(dioda[0], LOW);
digitalWrite(dioda[1], HIGH);
digitalWrite(dioda[2], LOW);
break;
case 6:
digitalWrite(dioda[0], HIGH);
digitalWrite(dioda[1], LOW);
digitalWrite(dioda[2], LOW);
break;
case 7:
digitalWrite(dioda[0], LOW);
digitalWrite(dioda[1], LOW);
digitalWrite(dioda[2], LOW);
break;
}
}

Resources