Basis Codes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Receiver:

#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

HX711 scale(DOUT, CLK);

SoftwareSerial xbeeSerial (10, 11);

char f;

float weight;

int oilRead, garbageDist, flagGarbage, flagOil, flagWeight, flag;

long duration;

float voltageConversionConstant = .004882814;

float sensorVoltage = 0;

int sensorValue = 0;

float lowcharge = 3.80;


const int battPin = A5;

void setup() {

Serial.begin(9600);

xbeeSerial.begin(9600);

pinMode(oilSensor, INPUT); pinMode(trigPinGarbage, OUTPUT); pinMode(echoPinGarbage, INPUT);

pinMode(forward1, OUTPUT); pinMode(forward2, OUTPUT); pinMode(backward1, OUTPUT);


pinMode(backward2, OUTPUT);

pinMode(conveyOil, OUTPUT); pinMode(conveyGarbage, OUTPUT); pinMode(battLedPin, OUTPUT);

digitalWrite(forward1, HIGH); digitalWrite(forward2, HIGH); digitalWrite(backward1, HIGH);


digitalWrite(backward2, HIGH);

digitalWrite(conveyOil, HIGH); digitalWrite(conveyGarbage, HIGH); digitalWrite(battLedPin, LOW);

Serial.println("START1");

// delay(3000);

scale.set_scale(19000.0);

scale.tare();

long zero_factor = scale.read_average();

Serial.print("Zero factor: ");

Serial.println(zero_factor);

flagOil = 0; flagGarbage = 0; flag = 0;

// testGarbage();

// testW();

Serial.println("START2");

void loop() {

// testBatt();

// testOil();
batt();

//while (xbeeSerial.available() > 0) {

while (xbeeSerial.available() > 0) {

f = xbeeSerial.read(); //Serial.println(f);

if (f == 'F') { //forward

digitalWrite(forward1, LOW); // forward1 as forward

digitalWrite(forward2, LOW); // forward2 as forward

else if (f == 'B') { //backward

digitalWrite(backward1, LOW); // backward1 as backward

digitalWrite(backward2, LOW); // backward2 as backward

else if (f == 'L') { //left

digitalWrite(forward2, LOW); digitalWrite(backward1, LOW);

else if (f == 'R') { //right

digitalWrite(forward1, LOW); digitalWrite(backward2, LOW);

else if (f == 'J') { //stop

digitalWrite(forward1, HIGH); digitalWrite(forward2, HIGH); digitalWrite(backward1, HIGH);


digitalWrite(backward2, HIGH);

if (f == 'X') { //on

flag = 0;

if (f == 'Z') { //off

flag = 1; digitalWrite(conveyOil, HIGH); digitalWrite(conveyGarbage, HIGH);

}
if (f == 'K') { //reset

flagOil = 0; flagGarbage = 0; flagWeight = 0; flag = 0;

if (flag == 0) {

oilRead = digitalRead(oilSensor);

if (oilRead == 1) {

digitalWrite(conveyOil, HIGH); flagOil = 1;

if (flagOil == 0) {

digitalWrite(conveyOil, LOW); xbeeSerial.write('I'); delay(15);

} else {

xbeeSerial.write('O'); delay(15);

measureGarbage();

if (flagGarbage == 0) {

digitalWrite(conveyGarbage, LOW); xbeeSerial.write('A'); delay(15);

} else {

xbeeSerial.write('G'); delay(15);

weight = scale.get_units();

Serial.println(weight);

if (weight >= 20.0) {

flagOil = 1; flagGarbage = 1; flagWeight = 1; digitalWrite(conveyOil, HIGH);


digitalWrite(conveyGarbage, HIGH);

}
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;

RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);

uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

int trigStateForward, trigStateBackward, trigStateLeft, trigStateRight;

int trigStateOn, trigStateOff, trigStateReset;

int datar=0;

int samples=0;

int analogvalue=0;

int outputV=0;

int totalC=0;

int batteryV=0;

int joyposVert = 512;

int joyposHorz = 512;

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

// Print to Serial Monitor

Serial.println("Reading motorcontrol values ");

// Read the Joystick X and Y positions

joyposVert = analogRead(joyVert);

joyposHorz = analogRead(joyHorz);

// Determine if this is a forward or backward motion

// Do this by reading the Verticle Value

// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)

// This is Backward

// Set Motors backward

motorcontrol[2] = 1;

//Determine Motor Speeds

// As we are going backwards we need to reverse readings

motorcontrol[0] = map(joyposVert, 460, 0, 0, 255);


motorcontrol[1] = map(joyposVert, 460, 0, 0, 255);

else if (joyposVert > 564)

// This is Forward

// Set Motors forward

motorcontrol[2] = 0;

//Determine Motor Speeds

motorcontrol[0] = map(joyposVert, 564, 1023, 0, 255);

motorcontrol[1] = map(joyposVert, 564, 1023, 0, 255);

else

// This is Stopped

motorcontrol[0] = 0;

motorcontrol[1] = 0;

motorcontrol[2] = 0;

// Now do the steering

// The Horizontal position will "weigh" the motor speed

// Values for each motor

if (joyposHorz < 460)

{
// Move Left

// As we are going left we need to reverse readings

// Map the number to a value of 255 maximum

joyposHorz = map(joyposHorz, 460, 0, 0, 255);

motorcontrol[0] = motorcontrol[0] - joyposHorz;

motorcontrol[1] = motorcontrol[1] + joyposHorz;

// Don't exceed range of 0-255 for motor speeds

if (motorcontrol[0] < 0)motorcontrol[0] = 0;

if (motorcontrol[1] > 255)motorcontrol[1] = 255;

else if (joyposHorz > 564)

// Move Right

// Map the number to a value of 255 maximum

joyposHorz = map(joyposHorz, 564, 1023, 0, 255);

motorcontrol[0] = motorcontrol[0] + joyposHorz;

motorcontrol[1] = motorcontrol[1] - joyposHorz;

// Don't exceed range of 0-255 for motor speeds

if (motorcontrol[0] > 255)motorcontrol[0] = 255;

if (motorcontrol[1] < 0)motorcontrol[1] = 0;

// Adjust to prevent "buzzing" at very low speed


if (motorcontrol[0] < 8)motorcontrol[0] = 0;

if (motorcontrol[1] < 8)motorcontrol[1] = 0;

motorcontrol++;

//Display the Motor Control values in the serial monitor.

Serial.print("Motor A: ");

Serial.print(motorcontrol[0]);

Serial.print(" - Motor B: ");

Serial.print(motorcontrol[1]);

Serial.print(" - Direction: ");

Serial.println(motorcontrol[2]);

//Send a message containing Motor Control data to manager_server

if (RadioManager.sendtoWait(motorcontrol, sizeof(motorcontrol), SERVER_ADDRESS))

// Now wait for a reply from the server

uint8_t len = sizeof(buf);

uint8_t from;

if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))

Serial.print("got reply from : 0x");

Serial.print(from, HEX);

Serial.print(": ");

Serial.println((char*)buf);

else

Serial.println("No reply, is nrf24_reliable_datagram_server running?");


}

else

Serial.println("sendtoWait failed");

delay(100); // Wait a bit before next transmission

//BATTERY CAPACITY!!!!!!!

if (datar==0)

samples=0;

averaging=0;

while (samples<1000)

analogvalue = analogRead(analog);

averaging=averaging + analogvalue;

numberofsamples++;

averaging=averaging/1000;

outputV = averaging*(5/1023.0); //the real value of 5 volts divided by 1023;

totalC = (outputvoltage/10000); //Divide by R2 = 10,000

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;}

You might also like