Hello every one
i write my code and every thing is ok , unless this line
New[t]=Serial.read();
i don't know what is the problem , so pleas i need you help
and this is the code
EmanSarkez:
Hello every one
i write my code and every thing is ok , unless this lineNew[t]=Serial.read();
i don't know what is the problem , so pleas i need you help
and this is the code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int RPWM_Output1 = 9;
int LPWM_Output1 = 8;
int RPWM_Output2 = 7;
int LPWM_Output2 = 6;
int LL = 34;
int L = 33;
int M =32;
int R = 31;
int RR= 30;
int a=0;
int counter =6;
char* A_path[]={"f","t","f","l","f","s"};
char* B_path[]={"f","t","f","l","f","s"};
char* New[]={"0","0","0","0","0","0"};
void setup()
{
Serial.begin(9600);
for(int thispin = 6 ; thispin < 10 ; thispin++)
{
pinMode(thispin,OUTPUT);
}
for(int S = 30 ; S < 35 ; S++)
{
pinMode(S,INPUT);
}
lcd.init();
lcd.backlight();
lcd.setCursor(4,0);
lcd.print("S L M R U");
Serial.println(" WELCOME ");
Serial.println("===================================================================================");
delay(15);
Serial.println(" if you need one of the items press 1 , if you need to add new path press 2");
Serial.println("====================================================================================");
}
void loop()
{
int LL_sensor=digitalRead(LL);
int L_sensor=digitalRead(L);
int M_sensor=digitalRead(M);
int R_sensor=digitalRead(R);
int RR_sensor=digitalRead(RR);
// the LCD display
lcd.setCursor(4,1);
lcd.print(LL_sensor);
lcd.setCursor(6,1);
lcd.print(L_sensor);
lcd.setCursor(8,1);
lcd.print(M_sensor);
lcd.setCursor(10,1);
lcd.print(R_sensor);
lcd.setCursor(12,1);
lcd.print(RR_sensor);
if(Serial.available()>0)
{
char x= Serial.read();
Serial.println(x);
Serial.println("====================================================================================");
if (x=='1')
{
Serial.println("====================================================================================");
delay(15);
Serial.println("press one of the items A or B");
delay(15);
Serial.println("====================================================================================");
while(Serial.available()==0); // wait for input
char z = Serial.read();
Serial.println(z);
if (z=='a') {
Serial.println("Thank you for chooes item AAAAA ");
delay(1000);
for (int y=0 ; y < counter ; y++)
{
if (A_path[y]=="f")
{
Serial.println("Forward");
}
if (A_path[y]=="l")
{
Serial.println("L section Right");
}
if (A_path[y]=="t")
{
Serial.println("T section Left");
}
if (A_path[y]=="s")
{
Serial.println("Stop");
}
}
}
else if(z=='b')
{
Serial.println("Thank you for chooes item BBBB ");
for (int i=0 ; i < counter ; i++)
{
if (B_path[i]=="f")
{
Serial.println("Forward");
}
if (B_path[i]=="l")
{
Serial.println("L section Left");
}
if (B_path[i]=="t")
{
Serial.println("T section Right");
}
if (B_path[i]=="s")
{
Serial.println("Stop");
}
}
}
}
if (x=='2') {
reset(); // loop to resets the array befor eash reading is stored
Serial.println("Enter your letters ");
for(int t=0; t< counter ; t++)
{
while(Serial.available()==0);
New[t]=Serial.read();
Serial.print(New[t]);
Serial.print(" , ");
}
Serial.println(" ");
delay(100);
Serial.println("====================================================================================");
Serial.println("Now we going to see each letter what it do");
delay(1000);
for (int y=0 ; y<counter ; y++)
{
if(New[y]=="f")
{
Serial.println("forward");
delay(500);
}
else if(New[y]=="t")
{
Serial.println("T section");
delay(500);
}
else if(New[y]=="l")
{
Serial.println("L section");
delay(500);
}
else if(New[y]=="s")
{
Serial.println("Stop");
delay(500);
}
else{ Serial.println("No Match letters ");
}
}
}
}
}
// loop to resets the array befor eash reading is stored
void reset()
{ int k =0;
while(k<counter)
{ New[k]="0";
k=k+1;
}
}
if (A_path[y]=="f")
Did you mean to use strcmp?
.New[t]=Serial.read();
You can't input a whole string with a single read.
immm i can only read one letter
but i need to read two letters how can i do this
'New' is an array of character pointers each initialized to point to a character string "0". To change that '0' to the character read from Serial.read() you have to go through the character pointer with the '*' operator:
*(New[t])=(char)Serial.read();
It is generally not a good idea to write into character string constants.
To read two characters:
char* New[]={"00","00","00","00","00","00"};
while(Serial.available()<2); // Make sure two bytes are available
Serial.readBytes(New[t], 2); // Read two bytes