Hello guys,
I am a newbie in the programming domain.
For a class project, I am making a smart chair concept prototype.
The idea is this- in the chair, there is a force sensor attached to Arduino which detects if someone is sitting. when the sensor triggers (i.e. someone sitting) it start communicating with a toy robot. This robot has 4 servo motors attached to its legs ( 2 on each) and servos are controlled by another Arduino. when the sensor senses that person is sitting for a long duration (10 sec for testing) it wirelessly trigger the servos to make the robot dance. Thus indicating the person that he/she sitting for a long time.
The code is posted below. The issue is everything is working fine if we use the trigger to light a led but as soon as we are putting the servos in the code, the servo starts moving immediately without waiting for the trigger. Why is happening and how can I make servo respond to the trigger?
The FSR side of the code
#include <SoftwareSerial.h>
#include <XBee.h>
unsigned long pressureOnTime; //millis() time when pressure switch is first turned on
const unsigned long ledOnAfterTimeThreshold = 3000; //3 seconds
int pressureThreshold = 100; //minimum analog reading from pressure sensor considered to be "on"
// constants won't change. They're used here to set pin numbers:
const int fsrAnalogePin = 0; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int pressureState = LOW;
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 2000; // the debounce time; increase if the output flickers
SoftwareSerial XBee(2, 3); //RX, TX
void setup() {
Serial.begin(9600);
pinMode(11, OUTPUT);
XBee.begin(9600);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
checkPressureSwitch();
//do other stuff here
// set the LED:
digitalWrite(ledPin, ledState);
XBee.write(ledState);
Serial.println(ledState);
}
void checkPressureSwitch() {
if( analogRead(fsrAnalogePin) >= pressureThreshold )
{
handlePressureOn();
}
else
{
handlePressureOff();
}
digitalWrite(ledPin, ledState);
}
void handlePressureOn() {
if ( pressureState == LOW )
{
//pressure switch was off, but is now on
pressureOnTime = millis();
pressureState = HIGH;
}
else if ( millis() - pressureOnTime >= ledOnAfterTimeThreshold )
{
ledState = HIGH;
}
}
void handlePressureOff() {
ledState = LOW;
pressureState = LOW;
}
// set the LED:
//digitalWrite(ledPin, ledState);
//XBee.write(ledState);
//Serial.println(ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
//lastButtonState = reading;
The Sero (or the robot) side of the code
#include <SoftwareSerial.h>
#include <XBee.h>
#include <Servo.h>
Servo rightfoot;
Servo rightthigh;
Servo leftfoot;
Servo leftthigh;
SoftwareSerial XBee(2, 3);
int data = 0;
void setup() {
rightfoot.attach(9);
rightthigh.attach(5);
leftfoot.attach(6);
leftthigh.attach(11);
leftfoot.write(10);
leftthigh.write(90);
rightthigh.write(100);
rightfoot.write(180);
XBee.begin(9600);
pinMode(7, OUTPUT);
}
void loop() {
while(XBee.available()){
data = XBee.read();
Serial.println(data);
}
if(data== 1) {
digitalWrite(7,HIGH);
rightfoot.write(172);
delay(60);
rightfoot.write(164);
delay(600);
rightfoot.write(156);
delay(60);
rightfoot.write(148);
delay(60);
rightfoot.write(140);
delay(60);
rightfoot.write(132);
delay(60);
leftfoot.write(16);
delay(60);
leftfoot.write(22);
delay(60);
leftfoot.write(28);
delay(60);
leftfoot.write(34);
delay(60);
leftfoot.write(40);
delay(60);
leftfoot.write(46);
delay(60);
}
if(data== 0) {
digitalWrite(7,LOW);
}
}