y
You don't need the last line loop();
Less importantly, you don't need Serial.flush() - it is only to do with serial output - it's very confusingly named.
...R
Robin2 but u didn't solve out the main problem which i have asked
Your question is not clear.
Is this a one time decision on power up or can it happen any time?
Is "start with time" a seprate block of code? If so I don't see it.
thanks for your reply Grumpy_Mike...
in my earlier post include two programs , second one is for start with time ,which i have to put in the first program , problem is that how should i put this into 1st so that all the program works well
ashali1234:
Robin2 but u didn't solve out the main problem which i have asked
Well post your revised code and tell us what it is supposed to do and what it actually does.
...R
How can you have "trunk" and "bonnet" ?
ashali1234:
thanks for your reply Grumpy_Mike...in my earlier post include two programs , second one is for start with time ,which i have to put in the first program , problem is that how should i put this into 1st so that all the program works well
You still haven't said if this is a one time at start up or an at any time?
Basically rename the two steup functions setup_time and setup_noTime. Do the same for the two loop functions.
Then make a new setup where you test the button and set a global boolean variable that fixes your choice. Then call either of the two renamed setups according to this variable.
Then make a new loop function that simply looks at the boolean variable and calls either of the two renamed loop functions according to this variable.
michinyon:
How can you have "trunk" and "bonnet" ?
hi every one thanks for your reply , the program which i have posted is just a sample program , when the bluetooth recieve different commands and i f anyone of the command match with the command in the program then only that part of the program will be executed and program will again jump back to the loop , like if the bluetooth which i have connected with arduino recieve a command "a" then just this part of the program will be executed,and this part of program
else if (data== 'a'){
Serial.println("turn on the accessory");
} i will add program for turn on accessory later on , there is no problem in that part, i have just problem with the serial commands.
like if i send 's' first then i get the correct result i-e led pin gets high and i got this statement Serial.println("today is thursday"); on serial monitor, but when i send k after that why i dont get the serial statement Serial.println("today is friday");on serial monitor and why the led dont go low
Are we supposed to guess how long you wait between sending 's' and sending 'k'?
You should have ONE function that reads serial data. NOT two.
First, when you get your code to compile, press the Ctrl-T keys to reformat it before you post it. Second, why even have the special function named aisha()? As Paul points out, it's almost never a good idea to duplicate functionality in different places...makes debugging harder. Third, what if they enter a K instead of k?
To simplify things, does this do what you're trying to accomplish?
int LED = 13;
char data;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.println("today is saturday");
}
void loop() {
if (Serial.available() > 0)
{
data = toupper(Serial.read()); //Now they can enter upper or lower case and it works
Serial.println(data);
switch (data) {
case '\n': // Ignore a newline character...
break;
case 'K':
digitalWrite(LED, LOW);
Serial.println("today is friday");
break;
case 'S':
Serial.println("today is thursday");
digitalWrite(LED, HIGH);
delay(1000);
break;
case 'T':
Serial.println("today is sunday");
break;
default:
Serial.println("Unrecognized letter.");
break;
}
}
}
hi econjack and pauls,,
thnks a lot for your reply first,,,but what if i want to send a command when some command has already been recieved by the arduino, like if i want to send a command 'k', and this command can only be recieved and executed if i have already recived a command 's', what i want to say is that if i send directly 'k' it should not be executed untill and unless 's' is send first
ashali1234:
but what if i want to send a command when some command has already been recieved by the arduino, like if i want to send a command 'k', and this command can only be recieved and executed if i have already recived a command 's',
You need to plan the logical steps that are required. I am doing something similar on my PC
On the Android you need something like this.
receive data from the Arduino
is it an 'S'
if so, send a response
repeat
And on the Arduino side it needs to be
send an 'S'
receive data from the Android
is it valid data
if so, act on the data
repeat
Depending on exactlyw aht you are doing it may be more sensible for the Arduino to send the S after it has validated the data but before it has acted on the data. That way the new data can arrive in the Serial input buffer while the current data is being acted on.
...R
thanks for you reply robin but its being really difficult for me to understand...is it not possible that the old data from the serial input buffer should be removed when the new data arrives??
is it not possible that the old data from the serial input buffer should be removed when the new data arrives??
No. The new data has no idea whether the old data should be replaced, no way to know whether there is old data, and no way to communicate with the HardwareSerial class, even if it DID know the answers.
Why would you want to throw away random amounts of unread data, anyway? If you don't plan to read it, don't send it.
ashali1234:
is it not possible that the old data from the serial input buffer should be removed when the new data arrives??
If you want to ensure the Serial input buffer is empty you must write some code to throw away any unwanted characters. For example
while (Serial.available > 0) {
byte wasteByte = Serial.read();
}
but its being really difficult for me to understand
If you say what you don't understand I will try to help.
...R
ashali1234:
here is my complete code of the project
I'm lazy. There is far too much code in that and it would take me too long to figure out what it all does. As well as which it is very poorly laid out so it is impossible to see where one function ends and another begins.
I suggest you write a short sketch that ONLY receives data from the Android and calls the appropriate function that just contains Serial.println("functionX"); so you know that the correct function is being called.
When that works properly you can start filling out the functions with the actual code to make things happen.
This Thread planning and implementing an Arduino program may help to illustrate a simpler way to organize things
...R
I am going to state the obvious. Your code looks like shit.
There is a reason that the Tools menu has an Auto Format item on it. Learn what that reason is by using it.
There is no sense in what you are doing. You have stuff embedded in if statements that should not be, but that is nearly impossible to see because your code is so poorly laid out.
something like this maybe:
certainly needs work to turn off the ignition, accessories, etc...
int led=13;
char data=0;
int ign_control=4;
int acc_control=5;
int AC=6;
int HT=7;
int state = 0;
//
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(ign_control, OUTPUT);
pinMode(acc_control, OUTPUT);
pinMode( AC, OUTPUT);
pinMode( HT, OUTPUT);
digitalWrite(led, LOW);
}
//
void loop()
{
if (state == 0)
{
if (Serial.available() > 0)
{
char data = Serial.read();
Serial.println(data);
if(data=='a')
{
Serial.println("turn on the accessory");
digitalWrite(acc_control, HIGH);
delay(1000);
state = 1;
}
else if(data == 'i')
{
Serial.println("turn on ignition ");
digitalWrite(ign_control, HIGH);
}
}
}
if (state == 1)
{
if (Serial.available() > 0)
{
char data = Serial.read();
Serial.println(data);
if(data == 'A')
{
Serial.println("turn on AC ");
digitalWrite(AC, HIGH);
}
else if(data == 'H')
{
Serial.println("turn on heater ");
digitalWrite(HT, HIGH);
}
else if(data == 'i')
{
Serial.println("turn on ignition ");
digitalWrite(ign_control, HIGH);
}
}
}
}
