Final Code - Still Need Help, Thanks.

Hello all,

To begin with thanks for being here and reading this.

The following is my setup:

Mega 2650 X2

HC-12 Module X2

2 9g Servo’s

8 On/Off Led’s(or whatever)

Before I post the code I would like to present you with my problem. All works well except for one thing. I have been trying to make button4 work. I have focused on that. When I hit the button. It seems that it is interfering with the servo’s position. The servo starts to Jitter back on forth about 1/8 of its total travel. When the button is released, the jitter stops. So it seems when “4444” is sent over serial to the receiver end, the servo is picking that up. I have tried a lot of ways to try to filter that data from hitting the servo. No luck.

Have a peek at the code:

Master.

 //-------------------------------------------------------------HC-12 Send

  #include <Wire.h> 
  #include <LiquidCrystal_I2C.h>
  
  #include <Servo.h>
  Servo Myservo;
  LiquidCrystal_I2C lcd(0x3F,16,2); 
   

  int AnalogInputPin = A5;
  int OutputValue = 0;
  int Button1 = 4;
  int Button2 = 5;
  int Button3 = 6;
  int Button4 = A8;
  int Button5 = 8;
  int Button6 = 9;
  int Button7 = 10;
  int Button8 = 11;
  boolean onOff = 0;
  
 //-----------------------------------------------------------------Setup 
 
void setup()

{ 
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  pinMode(Button3, INPUT);
  pinMode(Button4, INPUT);
  pinMode(Button5, INPUT);
  pinMode(Button6, INPUT);
  pinMode(Button7, INPUT);
  pinMode(Button8, INPUT);
  Serial.begin(9600);
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("Jello, world!");
  lcd.setCursor(0,1);
  lcd.print("HArduino!");  
}

 //------------------------------------------------------------------Loop

void loop()
{ 
  {
    int secs = millis() / 1000;
    lcd.setCursor(10,1);
    lcd.print (secs);
  }
  
 //--------------------------------------------------------------AnalogIn1  

 {
  int AnalogInputValue = analogRead(AnalogInputPin);
  OutputValue = map(AnalogInputValue, 0, 1023, 70, 110);
  Serial.println(OutputValue);
  Serial.println(OutputValue);
  delay(120);
 } 

 //--------------------------------------------------------------AnalogIn2

 

 //---------------------------------------------------------------Button1

 int ButtonState1 = digitalRead(Button1); 
  
  if(ButtonState1 == 1)
 { 
    Serial.println(1111); 
    onOff = 1; 
 }
 
  if(ButtonState1 == 0 && onOff == 1)
 { 
    Serial.println(000);  
    delay(20); 
 }

 //---------------------------------------------------------------Button2

 int ButtonState2 = digitalRead(Button2); 
  
  if(ButtonState2 == 1)
 { 
    Serial.println(2222); 
    onOff = 1; 
 }
 
  if(ButtonState2 == 0 && onOff == 1)
 { 
    Serial.println(000);  
    delay(20); 
 }

 //---------------------------------------------------------------Button3

 int ButtonState3 = digitalRead(Button3); 
  
  if(ButtonState3 == 1)
 { 
    Serial.println(3333); 
    onOff = 1; 
 }
 
  if(ButtonState3 == 0 && onOff == 1)
 { 
    Serial.println(000);  
    delay(20); 
 }

 //---------------------------------------------------------------Button4

 int ButtonState4 = digitalRead(Button4); 
  
  if(ButtonState4 == 1)
 { 
    Serial.println(4444);     
    delay(20);
 }
 
  if(ButtonState4 == 0 && onOff == 1)
 { 
    Serial.println(4440);  
    Serial.println(4440);
    delay(20); 
 }

 //---------------------------------------------------------------Button5

 int ButtonState5 = digitalRead(Button5); 
  
  if(ButtonState5 == 1)
 { 
    Serial.println(5555); 
    onOff = 1; 
 }
 
  if(ButtonState5 == 0 && onOff == 1)
 { 
    Serial.println(000);  
    delay(20); 
 }

 //---------------------------------------------------------------Button6

 int ButtonState6 = digitalRead(Button6); 
  
  if(ButtonState6 == 1)
 { 
    Serial.println(6666); 
    onOff = 1; 
 }
 
  if(ButtonState6 == 0 && onOff == 1)
 {
    Serial.println(000);  
    delay(20); 
 }

 //---------------------------------------------------------------Button7

 int ButtonState7 = digitalRead(Button7); 
  
  if(ButtonState7 == 1)
 { 
    Serial.println(7777); 
    onOff = 1; 
 }
 
  if(ButtonState7 == 0 && onOff == 1)
 { 
    Serial.println(000);  
    delay(20); 
 }

 //---------------------------------------------------------------Button8

 int ButtonState8 = digitalRead(Button8); 
  
  if(ButtonState8 == 1)
 { 
    Serial.println(8888); 
    onOff = 1; 
 }
 
  if(ButtonState8 == 0 && onOff == 1)
 { 
    Serial.println(000);  
    delay(20);
 }
 Serial.flush();
      
}

Slave.

 //------------------------------------------------------HC-12 Receive


  
  #include <Servo.h>
  Servo MyServo;
  Servo MyServo2;
 

  int input = 0;
  int Button1 = 4;
  int Button2 = 5;
  int Button3 = 6;
  int Button4 = 7;
  int Button5 = 8;
  int Button6 = 9;
  int Button7 = 12;
  int Button8 = 13;
 
   

 //------------------------------------------------------------Setup 

void setup() 
{
  Serial.begin(9600);
  Serial.begin(9600);
  MyServo.attach(10);   
}

 //-------------------------------------------------------------Loop

void loop() 

 //---------------------------------------------------------Servo1&2

{    
   int input = Serial.parseInt();
   if (input > 1 && input < 180); 
  {
     MyServo.write(input);
     delay(30);
  }
  
   if (input > 180 && input < 360)
  {
     MyServo2.write(input - 180);
     delay(230);
  }
 

 //-----------------------------------------------------------Button1
  
   if(Serial.available() > 180)
       
   int input = Serial.parseInt();    
   if(input == 1111)
  { 
     digitalWrite(Button1, HIGH);
     delay(20); 
  }
   if(input == 000)
  { 
     digitalWrite(Button1, LOW); 
     delay(20); 
  }

 //---------------------------------------------------------Button2

   if(Serial.available() > 180)
  
   int input = Serial.parseInt();    
   if(input == 2222)
  { 
     digitalWrite(Button2, HIGH);
     delay(20); 
  }
   if(input == 000)
  { 
     digitalWrite(Button2, LOW); 
     delay(20); 
  }

 //---------------------------------------------------------Button3

   if(Serial.available() > 180)
  
   int input = Serial.parseInt();    
   if(input == 3333)
  { 
     digitalWrite(Button3, HIGH);
     delay(20); 
  }
   if(input == 000)
  { 
     digitalWrite(Button3, LOW); 
     delay(20); 
  }

   //---------------------------------------------------------Button4

   if(Serial.available() > 180)
  
   int input = Serial.parseInt();    
   if(input == 4444)
  { 
     digitalWrite(Button4, HIGH);
     delay(20); 
  }
   if(input == 4440)
  { 
     digitalWrite(Button4, LOW); 
     delay(20); 
  }

   //---------------------------------------------------------Button5

   if(Serial.available() > 180)
  
   int input = Serial.parseInt();    
   if(input == 5555)
  { 
     digitalWrite(Button5, HIGH);
     delay(20); 
  }
   if(input == 000)
  { 
     digitalWrite(Button5, LOW); 
     delay(20); 
  }

   //---------------------------------------------------------Button6

   if(Serial.available() > 180)
  
   int input = Serial.parseInt();    
   if(input == 6666)
  { 
     digitalWrite(Button6, HIGH);
     delay(20); 
  }
   if(input == 000)
  { 
     digitalWrite(Button6, LOW); 
     delay(20); 
  }

   //---------------------------------------------------------Button7

   if(Serial.available() > 180)
  
   int input = Serial.parseInt();    
   if(input == 7777)
  { 
     digitalWrite(Button7, HIGH);
     delay(20); 
  }
   if(input == 000)
  { 
     digitalWrite(Button7, LOW); 
     delay(20); 
  }

   //---------------------------------------------------------Button8

   if(Serial.available() > 180)
  
   int input = Serial.parseInt();    
   if(input == 8888)
  { 
     digitalWrite(Button8, HIGH);
     delay(20); 
  }
   if(input == 000)
  { 
     digitalWrite(Button8, LOW); 
     delay(20); 
  }
 Serial.flush();
}

//Serial.flush();

I have looked in to switches and servo timers. I assume that starting over is an option. However I would like to make this work if somehow feasible! thank you soo much if you know what to do.

What do you think is happening here?
if(Serial.available() > 180)

.

Similar post here

Massive over-use of braces too.
Needs more array

Hey guys,

Maybe I should add. I am just learning. What do I think is happening there? Well. I think at that point If it reads incoming data less the numerals 180 run the next line. Why? I don't see that as the problem. I see it is the point just beyond that. Servo starts to write at whatever position that triggered the line of code before it. Lets say 160. Servo does what it should at that very instant. However that is not the only numbers coming in. right?

Massive over use of braces {im not sure i see where} {i did that} {could use more help guys!}

serious though. I did not place those. so help me add array? not even sure what that is. Kinda
got a clue though by the name.

You're still misunderstanding what the available method returns.
It returns the number of characters in the receive buffer, not the numeric value the characters may or may not represent.

O Wow,
Please help me wrap me head around this… This is the way my brain perceives this, thus far:

Master side.
in a loop looking to see if anything meets a requirement

spits out 4444 when button pressed

magically goes to the HC-12 via serial data

and yes I see “4444” going thru the air to the receiver

Slave side.

receiver sends numbers “4444” to the arduino via serial data

it reads “4444” in my mind that data should go past serial.avialable <180 since its larger

please educate me. this is obviously critical to my understanding! yikes.

it reads "4444

,. . . and "available ()" returns 4.

Just as it would return 4 if it received "1234" or "9999", or"0000"

Note, available () returns 4, not '4' or "4".

So sorry I still do not understand. Im trying really hard.

4444 = 4

4532 = 4

0000 = 4

a sum of the incoming digits. right?

342 = 3 ? not sure

2049583849 = 10 same

but i think thats what you are saying…

so what does Serial.avialable < 180?

does that mean less then 3? 180 = 3?

B.t.w. I used quotes when I said “4444” simply as to say i was quoting my code. I see now why you said don’t do that. Serial(“some_int”) vs Serial(some_int). pretty sure thats what your talking about.

Not the sum, the number of digits (aka characters)

Serial.available is always going to be less than 180.
Always.

so what does Serial.avialable < 180?

Is the number of characters that has been received less than one hundred and eighty.

.

the sum of the digits is what i was meaning.

if 3623463 = 7

i consider 7 a sum of the amound of digits, numerals, chars. whatever they are. ahhhh! idk.

if Serial.avialable() is always going to be less then 180 I sure dont understand. if serial avialable...

to me, that is asking is the serial data aviailable? is it coming in? the number value after that expression is saying how much that incoming value has to be in order to meet the requirment of triggering the next action.

I now know I am tottally lost and what you have given me so far has let me know Im ignorant to this but I've yet to see a way to enlighten myself. could you please just spell it out for me a little better again I am soo sorry...

Shannon,
that is starting to make since to me.

i send 4444 or 5555 for that matter

on the receive end it can pick up and read 4444 however. serial.avialable < x.

that statement is only going to count the number of digits it has read.

this is going to look rough but i think youll get it…

serial.avial < 1500

i send serial.println(9999999… the number 9 1499 times)

true.

serial.avial < 1500

i send serial.println(99999999999… the number 9 1501 times)

false.

Are these what we call strings?

The number 1234 is represented in ASCII by four ASCII characters, '1' then '2' then '3' and finally '4'.

Four characters.
Four bytes.

I really don't know how to make it plainer.

You could try explaining how that applies to what we are doing? that we be a good start. I freaking understand you said it twice now. Your obviously very bright so... I know you don't think telling me it reads it as 4 chars thus meaning 4 is enough for me to understand all of what I'm doing here. I know that much. So if your not here to help. Why are you here? Shannon please! continue. Or anyone that wants to chime in.

Who the Hell is Shannon?
Is there a part of this thread I can't see?

I wonder,
If I can send 4444 or 5555 just the same. What is the reason for using unique chars. Or is there a reason? I'm guessing Serial.avail may read the amount of incoming chars but, other parts of the code understand the unique chars?

ahhhhhh i need Serial.avial < 4 omg guys why didnt someone just say that. grrr.

Serial.avial < 4 will see the pot values… 90 90 90 89 87 86 so on.

those serial.avial will see as 2’s right guys?

then when i send 4444 thinking… ya omg why is everyone so harsh when you ask for answers. i bet you guys knew the answer the whole time.

I know you did. why?

Aggravated but I did still get my answer. This is identical to almost every single time I post for help here or at All About Circuits. Like everyone just wants to toy with you. I guess its fun to retain your wisdom from the peasants. Or at least make it like pulling teeth to get. I on a normal basis will spend two days looking for the answer before I ask any one of you!

Thanks. See you in two days.

Go to bed.
You're tired and desperately confused.
Sleep on it.

"ahhhhhh i need Serial.avial < 4 omg guys why didnt someone just say that. grrr. "

Alluded to the problem in post number #1 :wink:
Suggest you will never make the same error.

.

Am I still wrong. That will not fix it?