I'm making an Arduino Up/Down counter whose output can be controlled by the Processing Program. It displays the count down on a 3-digit 7 segment display of a training shield, and push buttons can be used to make it count up, down, stop, and reset it. The Processing code should be able to do the same, but via the software instead of push buttons. My implementation idea is by having boxes that once hovered over, sends signals that toggles counting up and down, stopping, and reset, so that I can use the SerialWrite example. Shown below is my up/down counter Arduino code including the serial connections necessary in order to interface it with Processing.
unsigned char digit_1 = 7;
unsigned char digit_2 = 8;
unsigned char digit_3 = 10;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int state = 0;
int halt = 0;
char val; //data received from serial port
void setup()
{
Serial.begin(9600);
for (int x = 11; x < 19; x++)
{
pinMode (x, OUTPUT);
}
pinMode (digit_1, OUTPUT);
pinMode (digit_2, OUTPUT);
pinMode (digit_3, OUTPUT);
// attachInterrupt(0, s1_press, RISING);
// attachInterrupt(1, s2_press, RISING);
}
void loop()
{
while (Serial.available())
{ val = Serial.read();
}
if (val == 1)
{
state = 1;
}
else //if (val == 0)
{
state = 0;
halt = 0;
}
if (val == 2)
{
halt = 1;
}
else
{
state = 0;
halt = 0;
}
if (halt == 0) {
if (state == 0) {
num3++;
if (num3 == 10) {
num3 = 0;
num2++;
}
if (num2 == 10) {
num2 = 0;
num1++;
}
if (num1 == 10) {
num1 = 0;
num2 = 0;
num3 = 0;
}
}
if (state == 1) {
num3--;
if (num3 == -1) {
num3 = 9;
num2--;
}
if (num2 == -1) {
num2 = 9;
num1--;
}
if (num1 == -1) {
num3 = 9;
num2 = 9;
num1 = 9;
}
}
}
for (int x = 0; x < 80; x++) {
digitalWrite (digit_3, HIGH);
digitalWrite (digit_2, LOW);
digitalWrite (digit_1, LOW);
display_ (num1);
delay(1);
digitalWrite (digit_3, LOW);
digitalWrite (digit_2, HIGH);
digitalWrite (digit_1, LOW);
display_ (num2);
delay(1);
digitalWrite (digit_3, LOW);
digitalWrite (digit_2, LOW);
digitalWrite (digit_1, HIGH);
display_ (num3);
delay(1);
}
}
void display_ (unsigned char num)
{
switch (num)
{
case 0:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, HIGH);
digitalWrite (16, HIGH);
digitalWrite (17, LOW);
digitalWrite (18, LOW);
break;
}
case 1:
{
digitalWrite (11, LOW);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, LOW);
digitalWrite (15, LOW);
digitalWrite (16, LOW);
digitalWrite (17, LOW);
digitalWrite (18, LOW);
break;
}
case 2:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, LOW);
digitalWrite (14, HIGH);
digitalWrite (15, HIGH);
digitalWrite (16, LOW);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 3:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, LOW);
digitalWrite (16, LOW);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 4:
{
digitalWrite (11, LOW);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, LOW);
digitalWrite (15, LOW);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 5:
{
digitalWrite (11, HIGH);
digitalWrite (12, LOW);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, LOW);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 6:
{
digitalWrite (11, HIGH);
digitalWrite (12, LOW);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, HIGH);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 7:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, LOW);
digitalWrite (15, LOW);
digitalWrite (16, LOW);
digitalWrite (17, LOW);
digitalWrite (18, LOW);
break;
}
case 8:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, HIGH);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
case 9:
{
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
digitalWrite (14, HIGH);
digitalWrite (15, LOW);
digitalWrite (16, HIGH);
digitalWrite (17, HIGH);
digitalWrite (18, LOW);
break;
}
}
}
void s1_press() {
if (state == 0)
state = 1;
else if (state == 1)
state = 0;
delay (100);
}
void s2_press() {
if (halt == 0)
halt = 1;
else
halt = 0;
delay(100);
}
Here's the Processing code I'm using:
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
color c1 = #75C58E;
color c2 = #75C5FF;
color c3 = #D07633;
void setup()
{
size(420, 200);
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}
//0 - count up and start
//1 - count down
//2 - stop
//3 - reset
void draw() {
background(255);
if (mouseOverRect() == true) { // If mouse is over square,
fill(c1); // change color and
myPort.write('1'); // send a '1' to indicate mouse is over square
} else //will be equivalent to the count up/count down trigger
{
fill(0);
myPort.write('0');
}
rect(50, 50, 100, 100); // Draw a square
if (mouseOverRect2() == true) { // If mouse is over square,
fill(c2); // change color and
myPort.write('2'); // send a 2 to indicate mouse is over square
} else //stop trigger
{
fill(0);
myPort.write('0');
}
rect(160, 50, 100, 100);
if (mouseOverRect3() == true) { // If mouse is over square,
fill(c3); // change color and
myPort.write('3'); // send a '3' to indicate mouse is over square
} else //reset
{
fill(0);
myPort.write('0');
}
rect(270, 50, 100, 100);
}
boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
boolean mouseOverRect2() { // Test if mouse is over square
return ((mouseX >= 160) && (mouseX <= 260) && (mouseY >= 50) && (mouseY <= 150));
}
boolean mouseOverRect3() { // Test if mouse is over square
return ((mouseX >= 270) && (mouseX <= 370) && (mouseY >= 50) && (mouseY <= 150));
}
Any tips on how to properly implement this? It doesn't seem to affect the output on the up/down counter. Thanks!
The code you Processing code you posted seems to do mostly what you intended it to do.
There a few things I'd advise to pay attention to on the Processing side:
When a box is hovered, you are continuously (many times a second) sending a message to Arduino. You might want to move the Arduino sending logic to a inside a mousePressed() or mouseReleased() function to do it less often ? (Otherwise you're values will change very very fast and it will be harder to see what's going on).
Similarly, when nothing is hovered, you are sending the '0' character 3 times. Otherwise it's sent twice (for each other box that isn't hovered): that's high chances of cancelling/overriding one of the previous command message ('1','2','3')
Bare in mind you are reading the an int in Arduino but sending a char from Processing. So if you expect 1 in Arduino you should do myPort.write(1); in Processing. myPort.write('1'); will send the ASCII char 1 which, represented by int 49.
Here's a slightly reorganised version of your Processing code with the above suggestions implemented:
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
color c1 = #75C58E;
color c2 = #75C5FF;
color c3 = #D07633;
void setup()
{
size(420, 200);
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}
//0 - count up and start
//1 - count down
//2 - stop
//3 - reset
void draw() {
background(255);
if (mouseOverRect() == true) { // If mouse is over square,
fill(c1); // change color and
} else //will be equivalent to the count up/count down trigger
{
fill(0);
}
rect(50, 50, 100, 100); // Draw a square
if (mouseOverRect2() == true) { // If mouse is over square,
fill(c2); // change color and
} else //stop trigger
{
fill(0);
}
rect(160, 50, 100, 100);
if (mouseOverRect3() == true) { // If mouse is over square,
fill(c3); // change color and
} else //reset
{
fill(0);
}
rect(270, 50, 100, 100);
}
void mousePressed(){
if (mouseOverRect() == true) { // If mouse is over square,
myPort.write(1); // send a '1' to indicate mouse is over square
}
else
if (mouseOverRect2() == true) { // If mouse is over square,
myPort.write(2); // send a 2 to indicate mouse is over square
} else //stop trigger
if (mouseOverRect3() == true) { // If mouse is over square,
myPort.write(3); // send a '3' to indicate mouse is over square
} else //reset
{
fill(0);
myPort.write(0);
}
rect(270, 50, 100, 100);
}
boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
boolean mouseOverRect2() { // Test if mouse is over square
return ((mouseX >= 160) && (mouseX <= 260) && (mouseY >= 50) && (mouseY <= 150));
}
boolean mouseOverRect3() { // Test if mouse is over square
return ((mouseX >= 270) && (mouseX <= 370) && (mouseY >= 50) && (mouseY <= 150));
}
Your Arduino code looks a lot less organised. I would go test one thing at a time:
increasing a digit and displaying
decreasing a digit and displaying
resetting
handling serial input
You can use CoolTerm to easily send an int, rather than an ASCII character,
otherwise, if you plan to use Serial Monitor, send '1' instead of 1 from Processing, but make sure that's what you compare against in Arduino too (e.g. if (val == '1') instead of if (val == 1)
Related
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 designing an automatic changeover of a generator using Arduino. When the EB power is off, I need to switch on the generator starting motor for five seconds, then switch it off and then switch on the contactor C2. When the generator is ON and now if EB power comes in, I have to switch OFF the contactor C2, switch ON the generator stop motor for five seconds and then switch ON the contactor C2. My code is
int ebin = 2; // EB input
int gin = 3; // Generator input
int GonR1 = 4; // Generator ON (Relay 1) starting motor
int GoffR2 = 5; // Generator OFF (Relay 2) stopping motor
int C1 = 6; // Contactor 1
int C2 = 7; //Contactor 2
int e = 0;
int g = 0;
void setup()
{
pinMode(ebin, INPUT);
pinMode(gin, INPUT);
pinMode(GonR1, OUTPUT);
pinMode(GoffR2, OUTPUT);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
}
void loop()
{
e = digitalRead(ebin);
g = digitalRead(gin);
if (e == LOW) {
digitalWrite(GoffR2, LOW);
digitalWrite(C1, LOW);
delay(4000);
digitalWrite(GonR1, HIGH);
delay(5000);
digitalWrite(GonR1, LOW);
digitalWrite(C2, HIGH);
}
else if (e == HIGH && g == LOW) {
digitalWrite(GoffR2, LOW);
digitalWrite(GonR1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C1, HIGH);
}
else if (g == HIGH && e == HIGH) {
digitalWrite(GonR1, LOW);
delay(5000);
digitalWrite(C2, LOW);
digitalWrite(GoffR2, HIGH);
delay(4000);
digitalWrite(GoffR2, LOW);
digitalWrite(C1, HIGH);
}
}
When e==LOW, I want this part
digitalWrite(GoffR2, LOW);
digitalWrite(C1, LOW);
delay(4000);
digitalWrite(GonR1, HIGH);
delay(5000);
digitalWrite(GonR1, LOW);
To execute only once and I want this part
digitalWrite(C2, HIGH);
to be executed continuously until the condition fails.
But the reality is when the condition (e==LOW) is true the entire loop gets executed continuously. This will make the generator starter motor to run every 4 seconds. So I need help to solve this problem.
I haven't tried to understand all of your code, but this demonstrates the problem you have asked about:
You need to keep the previous state of e and if it was not LOW when the low if statement is called then you need to run the once off code:
int e = 0;
int e_previous = 1;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < 10; i++) {
if (e == 0) {
if (e_previous != 0) {
Serial.println("e LOW once off");
e_previous = 0;
}
Serial.println("e LOW continual");
}
else {
Serial.println("e HIGH continual");
e_previous = 1;
}
delay(2000);
// This emulates e changing state, it isn't important to understaning the code.
if (i == 3)
e = 1;
else if (i == 6)
e = 0;
}
}
will give you:
e LOW once off
e LOW continual
e LOW continual
e LOW continual
e LOW continual
e HIGH continual
e HIGH continual
e HIGH continual
e LOW once off
e LOW continual
e LOW continual
e LOW continual
Correcting your code:
int ebin = 2; // EB input
int gin = 3; // Generator input
int GonR1 = 4; // Generator ON (Relay 1) starting motor
int GoffR2 = 5; // Generator OFF (Relay 2) stopping motor
int C1 = 6; // Contactor 1
int C2 = 7; //Contactor 2
int e = 0;
int e_previous = High; //Change the initial value to whatever makes sense
int g = 0;
void setup() {
pinMode(ebin, INPUT);
pinMode(gin, INPUT);
pinMode(GonR1, OUTPUT);
pinMode(GoffR2, OUTPUT);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
}
void loop() {
e = digitalRead(ebin);
g = digitalRead(gin);
if (e == LOW)
{
if (e_previous != LOW) { //This part will be run once
digitalWrite(GoffR2, LOW);
digitalWrite(C1, LOW);
delay(4000);
digitalWrite(GonR1, HIGH);
delay(5000);
digitalWrite(GonR1, LOW);
e_previous = LOW; // This stops this inner if from being run again
}
digitalWrite(C2, HIGH);
}
else if (e == HIGH && g == LOW)
{
digitalWrite(GoffR2, LOW);
digitalWrite(GonR1, LOW);
digitalWrite(C2, LOW);
digitalWrite(C1, HIGH);
e_previous = HIGH; // Reset it
}
else if (g == HIGH && e == HIGH)
{
digitalWrite(GonR1, LOW);
delay(5000);
digitalWrite(C2, LOW);
digitalWrite(GoffR2, HIGH);
delay(4000);
digitalWrite(GoffR2, LOW);
digitalWrite(C1, HIGH);
e_previous = HIGH; // Reset it
}
else { // just in case
e_previous = HIGH; // Reset it
}
}
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);
}
}
}
I can't get the random LED to 'LOW' if the LDRValue is lower than 500. The code doesn't read the if statement in the case. It sets a LED to 'HIGH' and reads the value of the lightsensor, if the value is lower than 500 the LED has to go 'LOW' and then another LED has to turn 'HIGH' and over and over.
int randNumber;
int LDR1 = A1;
int LDR2 = A2;
int LDR3 = A3;
int LDR4 = A4;
int LDR5 = A5;
int LS = 500;
int LDRValue = 0;
void setup() {
Serial.begin(9600);
randNumber = random(1, 6);
}
void loop() {
switch (randNumber) {
case 1:
digitalWrite(2, HIGH);
LDRValue = analogRead(LDR1);
Serial.println(LDRValue);
if (LDRValue < LS) {
digitalWrite(2, LOW);
}
break;
case 2:
digitalWrite(3, HIGH);
LDRValue = analogRead(LDR2);
Serial.println(LDRValue);
if (LDRValue < LS) {
digitalWrite(3, LOW);
}
break;
case 3:
digitalWrite(4, HIGH);
LDRValue = analogRead(LDR3);
Serial.println(LDRValue);
if (LDRValue < LS) {
digitalWrite(4, LOW);
}
break;
case 4:
digitalWrite(5, HIGH);
LDRValue = analogRead(LDR4);
Serial.println(LDRValue);
if (LDRValue < LS) {
digitalWrite(5, LOW);
}
break;
case 5:
digitalWrite(6, HIGH);
LDRValue = analogRead(LDR5);
Serial.println(LDRValue);
if (LDRValue < LS) {
digitalWrite(6, LOW);
}
break;
}
}
Rewrite your casestatements like this:
case 5:
LDRValue = analogRead(LDR5);
Serial.println(LDRValue);
if (LDRValue < LS)
{
digitalWrite(6, LOW);
}
else
{
digitalWrite(6, HIGH);
}
break;
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;
}
}