if (Serial.available() != 0) ----

Greetings to you all,

Could you please help me out with the fallowing code .
The main idea is that i want the loop to keep moving until i send a new input from my keyboard
The code bellow works but after 1 iteration , c loses it’s value and when i print it i does not return any value .
Hope you can help . :o
Thank you!

#include<Servo.h>
#include <SimpleDHT.h>

int motor = 13 ;// 

char c;
char x;


void setup() { 
  Serial.begin(9600);
  pinMode(motor, OUTPUT);
// <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>loop
}
void loop ()
{
  Serial.print( "before  serial ::::    ");

Serial.println( );
Serial.println( c);//
Serial.println( );
Serial.println( );

 :D 
  {
     x = Serial.read();
     c =  x ;
     
   }

    Serial.print( "after serial ::::    ");
     Serial.println( );
    Serial.print(c);
     Serial.println( );

 if(c == 'a'){
  
    digitalWrite(motor, HIGH);
    Serial.print("if statement    ");
   Serial.println( );
    Serial.print(c);
    Serial.println( );
    Serial.println( );
    Serial.println( );
    Serial.print( "----------------------------------------------------------------");
  }
  else if(c == 'b'){
    digitalWrite(motor, LOW);
  }
   delay (2000);

  }

:D Uh-oh.

Why not check that there is something to read, before reading it?

funny how that got there :slight_smile:
here is the code … compiled and shorter

#include<Servo.h>
#include <SimpleDHT.h>

int motor = 13 ;// 

char c;
char x;


void setup() { 
  Serial.begin(9600);
  pinMode(motor, OUTPUT);

}
void loop ()
{
  

if (Serial.available() != 0)
  {
     x = Serial.read();
     c =  x ;
     
   }

 if(c == 'a'){
  
    digitalWrite(motor, HIGH);
    
  }
  else if(c == 'b'){
    digitalWrite(motor, LOW);
  }
   delay (2000);

  }

Nothing appears to be wrong.

What are you seeing ‘exactly’ ?

You do not need these 3 lines:
#include<Servo.h>
#include <SimpleDHT.h>
. . .
delay (2000);

The code in reply #2 bears very little resemblance to the original code.

funny how that got there

You have a very strange sense of humour.

AWOL:
The code in reply #2 bears very little resemblance to the original code.
You have a very strange sense of humour.

regargin the code … i just removed all my prints

larryd:
Nothing appears to be wrong.

What are you seeing ‘exactly’ ?

You do not need these 3 lines:
#include<Servo.h>
#include <SimpleDHT.h>
. . .
delay (2000);

the program is bigger and ai strippet the code a little

the problem i have is that if ( c==‘a’ ) i want it to execute an entire function and keep on going till a new value is inputed from the keyboard .

my program runs good but a the 2nd run of void loop it’s value changes even tho i don’t input anything …

the program is bigger and ai strippet the code i'm having problmes

Show us the complete sketch.

regargin the code

I have no idea what that means

larryd:
Show us the complete sketch.

i’m sorry but i can’t post it all because it will be screened at a later date to check for fraud /cheating .

i will try to make it a little clear .

i want this to run forever , till i enter lets say ‘u’ from keyboard.

if(c == 'a'){
  
    digitalWrite(motor, HIGH);
 

Serial.print(  " print this until c changes value    ");
     
/

Source code :

#include<Servo.h>
#include <SimpleDHT.h>

int motor = 13 ;// 

char c;
char x;


void setup() { 
  Serial.begin(9600);
  pinMode(motor, OUTPUT);

}
void loop ()
{
  

if (Serial.available() != 0)
  {
     x = Serial.read();
     c =  x ;
     
   }

 if(c == 'a'){
  
    digitalWrite(motor, HIGH);
 

Serial.print(  " print this until c changes value    ");
     
    
  }
  else if(c == 'b'){
    digitalWrite(motor, LOW);
  }
   delay (2000);

  }

[/quote]

Are you using the serial monitor?
(I'm sorry I can't post all of my answer)

Ares111111111:
i'm sorry but i can't post it all because it will be screened at a later date to check for fraud /cheating .

Does that mean you are cheating and are hoping to not get caught?

Ares111111111:
i'm sorry but i can't post it all because it will be screened at a later date to check for fraud /cheating .

So you just want us to help you cheat in a way that won't get you caught ?

...R

AWOL:
Are you using the serial monitor?
(I'm sorry I can't post all of my answer)

yes .
if i define global variable c ='h' it will hod value in all stages of the program
but if i input from keyboard lets say 'h' the value will not be retained in c and will not go in that IF for the second time .

In a later stage i will have an App on my Android phone ( receive data from different senors and control parts of a system.

you sir are not helping .
i'm not trying to get something that is already done , i'm stuck at this point and don't have a clue to work around it.
if it make you any happier i will explain , if i post all the code here , and by a change they find it , it will be considered that i got it from here ( even tho i'm the source ) and dismiss my paper

Robin2:
So you just want us to help you cheat in a way that won't get you caught ?

...R

Ares111111111:
if i post all the code here , and by a change they find it , it will be considered that i got it from here ( even tho i’m the source ) and dismiss my paper

That would make the people responsible for such decisions unreasonable and incompetent. That seems unlikely.

Was “no outside help” included in your assignment?

you can get all the help in the world ... but they check your paper with a computer program ... and if that program finds your paper like ... let's say 60 % similar to other projects ... then that is considered cheating ..

by posting all of my code here i open up to that and after all my work i'm not happy or willing to do it .

now that you know it all ... could someone shine a lite upon my "little" problem ... please ?????

i turn to this forum as a last resort ( i spend half a day writing different codes to make it work with no luck )

Just start answering the questions.

Or stop wasting time.

What have you got the line ending set to in the Serial monitor ?
Consider what will happen in your program if more than one character is in the input buffer

I made a few changes: I substituted the built in LED for the motor, and I have it print the value of c. it toggles the LED - we don't all have a servo laying about. a = on, b = off. it runs continuously. I wonder if you are hitting enter after you type a or b?

int LED = 13 ;//

char c;
char x;

void setup() 
{
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop ()
{
 

  if (Serial.available() != 0)
    {
     x = Serial.read();
     c =  x ;
    }

  if(c == 'a')
    {
     digitalWrite(LED, HIGH);
    }

  else if(c == 'b')
   {
    digitalWrite(LED, LOW);
   }
  Serial.println(c);
  delay (2000);
}

Ares111111111:
i want this to run forever , till i enter lets say 'u' from keyboard.

#include<Servo.h>

#include <SimpleDHT.h>

int motor = 13 ;//

char c;
char x;

void setup()
{
  Serial.begin(9600);
  pinMode(motor, OUTPUT);
}

void loop ()
{
  if (Serial.available() != 0)
  {
     x = Serial.read();
     c =  x ;    
  }

if(c == 'a')
  {
     digitalWrite(motor, HIGH);
     Serial.print(  " print this until c changes value    ");
   }
   else
     if(c == 'b')
     {
        digitalWrite(motor, LOW);
     }
  delay (2000);

}

Currently, your above sketch is behaving like these:
1. You enter 'a' from the InputBox (with Line ending tab at No line ending option), the L (built-in LED of UNO) becomes ON and the Serial Monitor keeps showing this message " print this until c changes value " in its OutputBox.
SerialMonitor.png

2. You enter 'b' from the InputBox, the L and the message " print this until c changes value " go OFF.

3. You enter 'a' from the InputBox, the process of Step-1 repeats.

4. You enter 'u' from the InputBox, the L remains ON; but, the message gets stopped printing.

Under the above circumstances, what you want exactly? Your statement "i want this to run forever , till i enter lets say 'u' from keyboard" is not clear to me. What is 'this' that will keep running until you enter 'h' from the InputBox/keyboard.

SerialMonitor.png

Thank you all .
I made the changes and everything works like a charm .

Have a good day !