0% found this document useful (0 votes)
5 views5 pages

Arduino RF Module Control Code

This code summarizes an Arduino sketch that controls a robot via RF module. It initializes serial communication and pins for the RF module. The scan function reads data from the RF module, isolates the command string, and controls motors and LEDs based on the command string value. The setup function initializes serial ports and pins. The loop continuously calls the scan function to check for and execute new commands received over RF.

Uploaded by

SiddharthMittal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Arduino RF Module Control Code

This code summarizes an Arduino sketch that controls a robot via RF module. It initializes serial communication and pins for the RF module. The scan function reads data from the RF module, isolates the command string, and controls motors and LEDs based on the command string value. The setup function initializes serial ports and pins. The loop continuously calls the scan function to check for and execute new commands received over RF.

Uploaded by

SiddharthMittal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Rf module code

#include<SoftwareSerial.h>

int pin5=5;
int pin6=6;
int pin3=3;
int pin4=4;
int ledpin=13;

SoftwareSerial mySerial(0,1); //setting pins 0,1 for rx,tx resp

//**************scan function****************//
void scan()
{
char temp[10];
char data[10];
int i=0;
for(int i=0;i<10;i++)
{
temp[i]=0;

// initializing by 0

data[i]=0;
}
byte len =0;

// var to store length

//*********algorithm to scan/read data*****************//


if([Link]()) // if any data gets detected, start

{
[Link](); // flush the buffer
[Link](5000);

//set timeout of 5 seconds

if([Link]("$","$")) //checking for $ character for termination of


string
{
len = [Link]('$',temp,10); // saved string in temp and
got length of string

[Link](temp); // for debugging


[Link](len); // for debugging

// ===Algorithm for Sorting main data from Total String======

while(temp[i]!='*') //main data starts after *


{
i++;
}
i++;

// increment once to go to main data

int j=0;
while(temp[i]!='$')
{
data[j]=temp[i];
temp
i++;
j++;
}

// writing main data to data variable from

}
}
if (strcmp("00",data)==0)
{
[Link]("1212");
digitalWrite(pin5,HIGH);//straight
digitalWrite(ledpin,HIGH);
delay(10000);
digitalWrite(ledpin,LOW);
delay(1000);
}
if (strcmp("01",data)==0)
{

digitalWrite(pin6,HIGH);//left
digitalWrite(ledpin,HIGH);
delay(5000);
digitalWrite(ledpin,LOW);
delay(1000);
}
if (strcmp("10",data)==0)
{
digitalWrite(pin3,HIGH); //right
digitalWrite(ledpin,HIGH);
delay(1000);
digitalWrite(ledpin,LOW);
delay(1000);
}

if (strcmp("11",data)==0)
{
digitalWrite(pin4,HIGH); //back
digitalWrite(ledpin,HIGH);
delay(15000);
digitalWrite(ledpin,LOW);
delay(1000);
}
}
void setup() {

// put your setup code here, to run once:

[Link](9600);

// Serial Port initialization by 9600 Bps

[Link](1200);//SoftwareSerial port initialization by 1200 bps

[Link](5000);

// Set time out 5 sec

[Link]();

pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);
pinMode(ledpin,OUTPUT);

void loop() {

// put your main code here, to run repeatedly:

scan();
}

Common questions

Powered by AI

The 'mySerial.flush()' command clears the serial buffer, ensuring any previous, possibly incomplete data is discarded before new data is read, preventing erroneous operations from stale data. 'mySerial.setTimeout(5000)' configures the timeout for serial read operations to 5 seconds, preventing the code from hanging indefinitely waiting for input more than the specified duration. Both commands enhance the robustness of serial communication by managing data flow and ensuring system responsiveness even if the expected data is delayed or malformed .

The 'scan' function is designed to read serial data from the SoftwareSerial interface, specifically looking for strings starting and ending with the '$' character. It extracts the main data segment indicated between '*' and '$'. Depending on this data segment, the code executes different digital write commands to control pins, thereby performing actions such as moving in different directions (straight, left, right, back) and controlling an LED. For example, if the data is '00', the system is set to move straight by writing HIGH to pin5 and the LED. The code includes conditions for the data strings '01', '10', and '11', triggering respective changes in the system's behavior .

The 'pinMode' commands in the 'setup' function configure specific pins as output, preparing them to control external devices such as motors and LEDs based on the incoming data. By setting pin5, pin6, pin3, and pin4 as OUTPUT, the system enables these pins to send HIGH or LOW signals to operate actuators for movement actions ('straight', 'left', 'right', 'back'). Additionally, the ledpin is set as OUTPUT to signal operations visually via an LED, providing an essential mechanism for interaction and feedback in the hardware setup .

The 'delay' function introduces a pause in the execution flow, providing timed control over how long pins remain in the HIGH (active) state. This affects how long actions such as movement direction activation and LED illumination persist, effectively controlling the duration of each executed command. By setting specific delays—e.g., 10000 ms for straight, 5000 ms for left, 1000 ms for right, and 15000 ms for back—the code ensures predictable actuation times, synchronizing hardware actions with expected operational behaviors. Properly timed delays are crucial for synchronizing sequences and preventing premature state changes or misoperations .

The 'ledpin' is used to provide visual feedback on the system's state and operations. It is activated (set to HIGH) when a particular data command is processed, indicating which movement or action is currently being executed. This allows operators to easily see the system responding to commands, serving as both a status indicator and a debug aid. The LED blinks on completion of an action within the specific time defined in the 'delay' function, visually confirming instruction processing and aiding troubleshooting or user interaction .

The system differentiates between the actions using a series of conditional if statements that compare the processed 'data' string to specific expected values. Each action corresponds to a unique two-character string: '00' for straight, '01' for left, '10' for right, and '11' for back. Once the main data is extracted, the code checks for these specific values using 'strcmp'. If a match is found, it executes digitalWrite commands to change the state of specific pins controlling the movement direction and LED. For instance, '00' triggers writing HIGH to pin5 and the LED, while '01' sets HIGH on pin6 .

The 'Serial.println' commands within the 'scan' function provide real-time feedback on the data being processed, printing the temporary string 'temp' and its length. This output allows developers to verify the correct reading and parsing of serial data and ensure the logic for finding and extracting the main data functions as expected. It helps identify logical errors or discrepancies in the data capture and parse process, facilitating debugging and ensuring the system behaves as intended during testing and deployment .

The function first sets up an empty buffer for 'temp' and 'data' arrays to store the incoming string and the critical segment, respectively. After verifying the presence of a string using 'mySerial.findUntil', it reads the main string up to the next '$' symbol into 'temp'. It then iterates through 'temp', discarding characters until a '*' is found, which denotes the start of the main data. Incrementing the index bypasses '*', and a loop begins to copy subsequent characters into the 'data' array until the end marker '$' is encountered. This methodical parsing ensures only the intended critical data segment is utilized for subsequent logical operations .

The condition 'if(mySerial.findUntil("$","$"))' is crucial as it ensures that the function starts processing only when a data string bracketed by the '$' character is detected. This acts as a mechanism to validate or frame the incoming data, ensuring that only complete and correctly formed strings are processed further. It prevents the function from acting on incomplete or corrupted data, maintaining the integrity of operations such as movement control and LED status changes .

The initialization routine involves setting up both SoftwareSerial and regular Serial interfaces. Using 'Serial.begin(9600)', the standard serial port is initialized for communication at 9600 baud rate. Then, 'mySerial.begin(1200)' sets up communication speed for the SoftwareSerial instance at 1200 baud rate, specifically tailored for this project. The timeout is set using 'mySerial.setTimeout(5000)', ensuring that communication attempts do not hang the system. Flushing any preexisting data ensures a clean start state. These configurations are vital for clear, reliable communication between components, synchronizing data exchange processes and ensuring the system operates correctly across different scenarios .

You might also like