Basis Codes
Basis Codes
Basis Codes
#include<SoftwareSerial.h>
#include "HX711.h"
#define forward1 2
#define forward2 4
#define backward1 3
#define backward2 5
#define conveyOil 7
#define conveyGarbage 6
#define trigPinGarbage 8
#define echoPinGarbage 9
#define oilSensor 12
#define battLedPin A3
//#define battPin A4
#define DOUT A1
#define CLK A0
char f;
float weight;
long duration;
float sensorVoltage = 0;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
xbeeSerial.begin(9600);
Serial.println("START1");
// delay(3000);
scale.set_scale(19000.0);
scale.tare();
Serial.println(zero_factor);
// testGarbage();
// testW();
Serial.println("START2");
void loop() {
// testBatt();
// testOil();
batt();
f = xbeeSerial.read(); //Serial.println(f);
if (f == 'F') { //forward
if (f == 'X') { //on
flag = 0;
if (f == 'Z') { //off
}
if (f == 'K') { //reset
if (flag == 0) {
oilRead = digitalRead(oilSensor);
if (oilRead == 1) {
if (flagOil == 0) {
} else {
xbeeSerial.write('O'); delay(15);
measureGarbage();
if (flagGarbage == 0) {
} else {
xbeeSerial.write('G'); delay(15);
weight = scale.get_units();
Serial.println(weight);
}
if (flagWeight == 1) {
xbeeSerial.write('W'); delay(15);
} else {
xbeeSerial.write('E'); delay(15);
}
Transmitter:
#include <RadioHead.h>
#include <RHCRC.h>
#include <RHDatagram.h>
#include <RHEncryptedDriver.h>
#include <RHGenericDriver.h>
#include <RHGenericSPI.h>
#include <RHHardwareSPI.h>
#include <RHMesh.h>
#include <RHNRFSPIDriver.h>
#include <RHReliableDatagram.h>
#include <RHRouter.h>
#include <RHSoftwareSPI.h>
#include <RHSPIDriver.h>
#include <RHTcpProtocol.h>
#include <RH_ASK.h>
#include <RH_CC110.h>
#include <RH_E32.h>
#include <RH_MRF89.h>
#include <RH_NRF24.h>
#include <RH_NRF51.h>
#include <RH_NRF905.h>
#include <RH_RF22.h>
#include <RH_RF24.h>
#include <RH_RF69.h>
#include <RH_RF95.h>
#include <RH_Serial.h>
#include <RH_TCP.h>
#include<SoftwareSerial.h>
#include <SPI.h>
#define START
#define btnOff 7
#define btnOn 8
#define ledBattRed 9
#define ledBattGreen 10
#define ledGarbage 11
#define ledWeight 12
#define ledOil 12
#define CE 7
#define CSN 8
#define MOSI 11
#define MISO 12
#define SCK 13
#define JOYV A0
#define JOYH A1
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
RH_NRF24 RadioDriver;
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
int datar=0;
int samples=0;
int analogvalue=0;
int outputV=0;
int totalC=0;
int batteryV=0;
int motorcontrol = 0;
char f;
void setup() {
Serial.begin(9600);
pinMode(joyVert, INPUT_PULLUP);
pinMode(joyHorz, INPUT_PULLUP);
pinMode(btnOn, INPUT_PULLUP);
pinMode(btnOff, INPUT_PULLUP);
pinMode(btnReset, INPUT_PULLUP);
pinMode(ledOil, OUTPUT);
pinMode(ledGarbage, OUTPUT);
pinMode(ledWeight, OUTPUT);
digitalWrite(ledOil, LOW);
digitalWrite(ledGarbage, LOW);
digitalWrite(ledWeight, LOW);
digitalWrite(ledBattRed, LOW);
digitalWrite(ledBattGreen, LOW);
Serial.println("START");
// Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");
}
void loop() {
//MOTORCONTROL
joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);
// This is Backward
motorcontrol[2] = 1;
// This is Forward
motorcontrol[2] = 0;
else
// This is Stopped
motorcontrol[0] = 0;
motorcontrol[1] = 0;
motorcontrol[2] = 0;
{
// Move Left
// Move Right
motorcontrol++;
Serial.print("Motor A: ");
Serial.print(motorcontrol[0]);
Serial.print(motorcontrol[1]);
Serial.println(motorcontrol[2]);
uint8_t from;
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
else
else
Serial.println("sendtoWait failed");
//BATTERY CAPACITY!!!!!!!
if (datar==0)
samples=0;
averaging=0;
while (samples<1000)
analogvalue = analogRead(analog);
averaging=averaging + analogvalue;
numberofsamples++;
averaging=averaging/1000;
batteryV = totalcurrent*110000; //total current of R1 and R2, R1= 100K and R2 = 10,000
//Serial.println(batteryvoltage);
if (batteryvoltage<=6.2) //7.2
digitalWrite(ledBattGreen,LOW);
digitalWrite(ledBattRed,HIGH);
}
else
digitalWrite(ledBattGreen,HIGH);
digitalWrite(ledBattRed,LOW);
if (incomingdata==1)
delayMicroseconds(1);
donewaitingtodata++;
if (donewaitingtodata>=10000) //10ms
{donewaitingtodata=0;incomingdata=0;}