Hi.
I have written quite a few working codes, but never one with serial.
I am trying to write serial for the very first time using arduino ide and atmega16A.
but getting some glitches
First I started with an uno at one end (board2) to see what was reading/writing.
but the uno died, so switches to another 16A.
right now I am using two breadboards before I attempt to fill in the blanks and a larger more complete board and code.
for some reason, I can't remove the Serial.println lines without all the code stopping to work ??? any ideas as to why ?
it works with those there even though I won't have a use for it.
on the next thing.
I can't seem to write higher end 3 digit numbers (above 255) so figured I would try string, but my attempts are completely shutting down the rest of the functions.
Scenario 1 works, but If I try 2 or 3 in place of 1, NOTHING works.
PS: The atmegas are running on internal 8mhz.
and, the reason I need the upper numbers is that I have to add 2 potentiometers map values into the serial and I assume they will take up the whole lower band of numbers,
Any help would be appreciated
thanks
//16A BOARD 1
const int SW1 = 3;
const int SW2 = 4;
const int SW5 = 2;
//const int SW7 = 7;
const int SW8 = 8;
const int SW20 = 21;
const int LED1 = 19;
const int LED2 = 17;
const int LED3 = 18;
const int LED4 = 5;
char LED2on = 123;
char LED2off = 113;
char SW20off = 20;
char SW20on = 21;
void setup() {
pinMode(LED1, OUTPUT);
digitalWrite(LED1, LOW);
pinMode(LED2, OUTPUT);
digitalWrite(LED2, LOW);
pinMode(LED3, OUTPUT);
digitalWrite(LED3, LOW);
pinMode(LED4, OUTPUT);
digitalWrite(LED4, LOW);
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
pinMode(SW5, INPUT_PULLUP);
// pinMode(SW7, INPUT_PULLUP);
pinMode(SW8, INPUT_PULLUP);
pinMode(SW20, INPUT_PULLUP);
Serial.begin(115200);
//Serial.write("ready");
while (!Serial);
delay(2000);
}
void loop() {
if (digitalRead (SW1) == LOW)
{digitalWrite (LED2 ,HIGH);}
if (digitalRead (SW2) == LOW)
{digitalWrite (LED2 ,LOW); }
if (Serial.available())
{
{ digitalWrite(LED4, HIGH);
}
/*
* //#############
// SCENARIO 3
String L1 = Serial.readString();
if (L1 == "666" )
{digitalWrite (LED3 ,HIGH);
}
if (L1 == "777" )
{digitalWrite (LED3 ,LOW);
}
//#############
//#############
// SCENARIO 2
if (Serial.readString()== "666" )
{digitalWrite (LED3 ,HIGH);
}
if (Serial.readString() == "777" )
{digitalWrite (LED3 ,LOW);
}
//#############
*/
//#############
// SCENARIO 1
char L1 = Serial.read();
// ON MOM SW
if (L1 == 14 )
{digitalWrite (LED3 ,HIGH);delay(5);
}
if (L1 == 19 )
{digitalWrite (LED3 ,LOW);delay(10);
}
//#############
// ON 2SWITCHES
char L2 = Serial.read();
if (L2 == 15 )
{digitalWrite (LED1 ,HIGH);delay(5);
}
if (L2 == 20 )
{digitalWrite (LED1 ,LOW);delay(10);
}
if (digitalRead (LED2) == LOW)
{ Serial.write(LED2off);
delay(2);}
if (digitalRead (LED2) == HIGH)
{Serial.write(LED2on);
delay(2);}
if (digitalRead (SW20) == LOW)
{ Serial.write(SW20on);
delay(2);}
if (digitalRead (SW5) == LOW)
{ Serial.write(SW20off);
delay(2);}
}
}
// 16A BOARD 2
const int SW1 = 31;
const int SW2 = 29;
const int SW3 = 27;
const int SW4 = 25;
const int SW5 = 11;
const int LED1 = 16;
const int LED2 = 17;
const int LED3 = 18;
const int LED4 = 19;
const int LED5 = 20;
const int LED6 = 21;
const int LED7 = 14;
char SW20off = 20;
char SW20on = 21;
void setup() {
pinMode(LED1, OUTPUT);
//digitalWrite(LED1, LOW);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
pinMode(SW3, INPUT_PULLUP);
pinMode(SW4, INPUT_PULLUP);
pinMode(SW5, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("PIA");//REMOVING THIS STOPS EVERYTHING ??
while (!Serial);
delay(2000);
}
void loop() {
if (Serial.available())
{
/*
//#############
// SCENARIO 3 OR 2
if (digitalRead (SW1) == LOW)
{Serial.write("666");delay(2);
digitalWrite(LED1, HIGH);
}
if (digitalRead (SW2) == LOW)
{Serial.write("777");delay(2);
digitalWrite(LED1, LOW);
}
//#############
*/
//#############
// SCENARIO 1
if (digitalRead (SW1) == LOW)
{Serial.write(14);delay(2);
digitalWrite(LED1, HIGH);
}
if (digitalRead (SW1) == HIGH)
{Serial.write(19);delay(2);
digitalWrite(LED1, LOW);
}
//#############
if (digitalRead (SW3) == LOW)
{Serial.write(15);delay(2);
digitalWrite(LED3, HIGH);
}
if (digitalRead (SW4) == LOW)
{ Serial.write(20);delay(2);
digitalWrite(LED3, LOW);
}
char LED2on = Serial.read () ;
if (LED2on == 123 )
{digitalWrite (LED2 ,HIGH);delay(5);
Serial.println("LED2on");//REMOVING THIS STOPS THE LED2 OFF ??
}
char LED2off = Serial.read () ;
if (LED2off == 113 )
{
Serial.println("LED2off");//REMOVING THIS STOPS EVERYTHING ??
digitalWrite (LED2 ,LOW);
delay(10);
}
if (Serial.read() == 21 )
{digitalWrite (LED6 ,HIGH);
delay(5);}
if (Serial.read() == 20 )
{digitalWrite (LED6 ,LOW);
delay(10);}
{ digitalWrite(LED7, HIGH);
//delay(200);
//digitalWrite(LED5, LOW);
}
delay(10);
}
}