I was trying to use a Bluetooth module and a touch sensor with my Arduino to control an AC bulb using relay....... and I was finding it difficult to write the correct coding....
Please can someone help me check my mistakes or even a better solution please
#define ctsPin 2
int ledPin =13;
char Incoming_Value = 0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ctsPin, INPUT);
}
void loop()
{
checkButton();
loop1();
loop2();
}
void checkButton();
{
int button state = digitalRead(INPUT)
if(button state == HIGH)
{ loop1();}
else
{
loop2();
}
}
void loop1()
{
int ctsValue = digitalRead(ctsPin);
if(ctsValue == HIGH)
{
digitalWrite(ledPin, HIGH);
Serial.println("TOUCHED");
}
else
{
digitalWrite(ledPin, LOW);
Serial.println("not touched");
}
delay(100);
}
void loop2()
{
if(Serial.available()>0)
{
InComing_Value = Serial.read();
Serial.println(Incoming_Value);
Serial.print("\n");
if(Incoming_Value =='1')
digitalWrite(ledPin, HIGH);
else if(Incoming_Value =='0')
dogitalWrite(ledPin, LOW);
}
}
Yes I am using the ledPin for the output and the only output is the relay
Thank you
I have corrected it by deleting the space but still facing some challenges
#define ctsPin 2
int ledPin =13;
char Incoming_Value = 0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ctsPin, INPUT);
}
void loop()
{
checkButton();
loop1();
loop2();
}
void checkButton()
{
int buttonState = digitalRead(INPUT)
if(buttonState == HIGH)
{ loop1();
}
else
{
loop2();
}
}
void loop1()
{
int ctsValue = digitalRead(ctsPin);
if(ctsValue == HIGH)
{
digitalWrite(ledPin, HIGH);
Serial.println("TOUCHED");
}
else
{
digitalWrite(ledPin, LOW);
Serial.println("not touched");
}
delay(100);
}
void loop2()
{
if(Serial.available()>0)
{
int InComing_Value = Serial.read();
Serial.println(Incoming_Value);
Serial.print("\n");
if(Incoming_Value =='1')
digitalWrite(ledPin, HIGH);
else if(Incoming_Value =='0')
digitalWrite(ledPin, LOW);
}
}
I many problems with the code itself. Also, it will be difficult for anyone to write the "correct coding" without knowing how everything is wired and what you expect to receive via serial in order to control the bulb.
What button are you checking here? INPUT does not define a button pin. button state is not a valid variable name either. buttonState would be a valid variable name, however.
void checkButton();
{
int button state = digitalRead(INPUT)
if (button state == HIGH)
{
loop1();
}
else
{
loop2();
}
}
1 Like
gcjr
October 26, 2022, 2:52pm
5
good that you tried using sub-functions but it's common to over complicate the problem
consider
const byte LedPin = 13;
const byte CtsPin = 2;
byte ctsState;
// -----------------------------------------------------------------------------
void loop ()
{
// check for button(?) press
byte cts = digitalRead (CtsPin);
if (ctsState != cts) { // state change
ctsState = cts;
delay (10); // debounce
if (LOW == cts) // pressed
digitalWrite (LedPin, ! digitalRead (LedPin)); // toggle
}
// -------------------------------------
// check for serial input
if (Serial.available ()) {
char c = Serial.read ();
Serial.println (c);
if ('1' == c)
digitalWrite (LedPin, HIGH);
else if ('0' == c)
digitalWrite (LedPin, LOW);
}
}
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (9600);
pinMode (LedPin, OUTPUT);
pinMode (CtsPin, INPUT);
ctsState = digitalRead (CtsPin);
}
Capitalize constants
1 Like
I used the INPUT so as if I am using the Bluetooth module it will refer to it as in and likewise the touch sensor
The 1==c
0==c
Is the c both in lower case letters ?
gcjr
October 26, 2022, 2:59pm
8
gcjr:
char c = Serial.read ();
yes lower case 'c'. often better to keep local variables short. 'i', 'n' are often integers, 'f' for float, 'c' for chars, 's' for c-strings
1 Like
Actually the "INPUT" is any output from both the Bluetooth module and the touch sensor to the Arduino as an INPUT........
Well the identifier INPUT is defined by the Arduino core to indicate a pin mode as in:
pinMode(ctsPin, INPUT);
However, digitalRead(INPUT) makes no sense since INPUT does NOT define a pin number.
1 Like
afotech
October 26, 2022, 3:44pm
12
Yes noted..... thank you so much
afotech
October 26, 2022, 3:46pm
13
Thanks alot for the code you gave me....it work ed.......I need to learn alot
system
Closed
April 24, 2023, 3:47pm
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.