I have been trying to control the state of an LED which depend on the FSR value using virtualwire library. I was able to read the values from FSR but unable to control the state of LED. The codes compile without any error. Can someone help me solve the problem. Below is the tx and rx code.
//--------------------------------------------------------------------------------------
// Tx Code
// Wireless Force Sensor
// By Yaameen Faisal
//--------------------------------------------------------------------------------------
#include <VirtualWire.h> // Include VirtualWire library for wireless transmission
int fsrPin = 1; // define pin A0 as FSR pin
int analog[8]; // define array to get multiple analog readings
int voltage[8]; // define array to get multiple voltage readings
int j;
int awakeCount = 0, restlessCount = 0, asleepCount = 0 ; // define and initialise counters
void setup()
{
Serial.begin(9600); // setup serial communication with baud rate (bits per second) to diplay on serial monitor
vw_setup(2000);
pinMode(fsrPin, INPUT); // set pin A0 as input
}
void loop()
{
for(j=0; j<=7; j++)
{
analog[j] = analogRead(fsrPin); // read pin A0, 8 times
Serial.print("Analog Reading = ");
Serial.print(analog[j],DEC);
voltage[j] = map(analog[j], 0, 1023, 0, 5000); // convert analog reading (0-1023) to voltage (0-5V)
Serial.print(", Voltage in mV = ");
Serial.print(voltage[j],DEC);
if (voltage[j] >= 3500) //check if voltage >= 3500, if so restless counter = +1
{
restlessCount = restlessCount + 1;
}
else if (voltage[j] > 1500 && voltage[j] < 3500) //check if 1500 < voltage < 3500, if so awake counter = +1
{
awakeCount = awakeCount + 1;
}
else if (voltage[j] > 100 && voltage[j] <= 1500) //check if 100 < voltage <= 1500, if so asleep counter = +1
{
asleepCount = asleepCount + 1;
}
Serial.print(", Awake count = ");
Serial.print(awakeCount, DEC);
Serial.print(", Restless count = ");
Serial.print(restlessCount, DEC);
Serial.print(", Asleep count = ");
Serial.println(asleepCount, DEC);
delay (3000);
}
int asleepcountLed = asleepCount; // store asleep counter value in alseepcountLED
int awakecountLed = awakeCount; // store awake counter value in awakecountLED
int restlesscountLed = restlessCount; // store restless counter value in restlesscountLED
Serial.println("-----------------------------------------------------------------------");
if (asleepcountLed > awakecountLed && asleepcountLed > restlesscountLed) // if asleep > awake & asleep > restless, led = green colour
{
char data = 'g';
vw_send((uint8_t *)&data, strlen(&data));
vw_wait_tx();
}
asleepCount = 0; // reset asleep counter
awakeCount = 0; // reset awake counter
restlessCount = 0; // reset restless counter
delay (500);
}
//--------------------------------------------------------------------------------------
// Rx Code
// Wireless Force Sensor
// By Yaameen Faisal
//--------------------------------------------------------------------------------------
#include <VirtualWire.h> // include Virtualwire library for wireless transmission
void setup()
{
Serial.begin(9600); // setup serial communication with baud rate (bits per second) to diplay on serial monitor
pinMode(4, OUTPUT);
vw_set_rx_pin(8);
vw_setup(2000);
vw_rx_start();
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
if (buf[0] == 'g')
{
digitalWrite(4, HIGH);
}
else if (buf[0] != 'g')
{
digitalWrite(4, LOW);
}
}
}