Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

LAMPIRAN

1. Program Samsak Tendangan


int fsrPin = 0; // the FSR and 10K pulldown are connected to a0
int fsrPin2 = 1;
int fsrPin3 = 2;
int fsrReading; // the analog reading from the FSR resistor divider
int fsrReading2;
int fsrReading3;
int fsrVoltage; // the analog reading converted to voltage
int fsrVoltage2;
int fsrVoltage3;
int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int flagkirim = 0;
int sampling;
int fsrForces;
int fsrForces2;
int fsrForces3;
int randomNumber;
int flagrandom = 0;
int flagpukul;

unsigned long fsrResistance; // The voltage converted to resistance,


can be very big so make "long"
unsigned long fsrResistance2;
unsigned long fsrResistance3;
unsigned long fsrConductance;
unsigned long fsrConductance2;
unsigned long fsrConductance3;

long fsrForce; // Finally, the resistance converted to force


long fsrForce2;
long fsrForce3;

long timer1 = millis();

String data;

35
void setup(void) {
Serial.begin(9600); // We'll send debugging information via the
Serial monitor
Serial2.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(fsrPin, INPUT);
pinMode(fsrPin2, INPUT);
pinMode(fsrPin3, INPUT);
randomSeed(analogRead(A4));

void loop() {
//Randomize
if (flagrandom == 0)
{
randomNumber = random(1, 4);
if (randomNumber == 1) digitalWrite(ledPin1, HIGH);
else if (randomNumber == 2) digitalWrite(ledPin2, HIGH);
else if (randomNumber == 3) digitalWrite(ledPin3, HIGH);
flagrandom=1;
}

//Sensor 1
fsrReading = analogRead(fsrPin);
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
if (fsrVoltage > 3000)
{
flagpukul = 1;
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V yay math!
fsrResistance = 5000 - fsrVoltage; // fsrVoltage is in millivolts so
5V = 5000mV
fsrResistance *= 10000; // 10K resistor
fsrResistance /= fsrVoltage;
fsrConductance = 1000000; // we measure in micromhos so
fsrConductance /= fsrResistance;

36
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
if (flagkirim == 1) flagkirim = 0;
}
else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
}
}

///Sensor 2
fsrReading2 = analogRead(fsrPin2);
// analog voltage reading ranges from about 0 to 1023 which maps to
0V to 5V (= 5000mV)
fsrVoltage2 = map(fsrReading2, 0, 1023, 0, 5000);
if (fsrVoltage2 > 3000)
{
flagpukul = 2;
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V yay math!
fsrResistance2 = 5000 - fsrVoltage2; // fsrVoltage is in millivolts
so 5V = 5000mV
fsrResistance2 *= 10000; // 10K resistor
fsrResistance2 /= fsrVoltage2;
fsrConductance2 = 1000000; // we measure in micromhos so
fsrConductance2 /= fsrResistance2;
if (fsrConductance2 <= 1000) {
fsrForce2 = fsrConductance2 / 80;
if (flagkirim == 2) flagkirim = 0;
} else {
fsrForce2 = fsrConductance2 - 1000;
fsrForce2 /= 30;
}
}

///Sensor 3
fsrReading3 = analogRead(fsrPin3);
// analog voltage reading ranges from about 0 to 1023 which maps to
0V to 5V (= 5000mV)

37
fsrVoltage3 = map(fsrReading3, 0, 1023, 0, 5000);
if (fsrVoltage3 > 3000)
{
flagpukul = 3;
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V yay math!
fsrResistance3 = 5000 - fsrVoltage3; // fsrVoltage is in millivolts
so 5V = 5000mV
fsrResistance3 *= 10000; // 10K resistor
fsrResistance3 /= fsrVoltage3;
fsrConductance3 = 1000000; // we measure in micromhos so
fsrConductance3 /= fsrResistance3;
// Use the two FSR guide graphs to approximate the force
if (fsrConductance3 <= 1000) {
fsrForce3 = fsrConductance3 / 80;
if (flagkirim == 3) flagkirim = 0;
} else {
fsrForce3 = fsrConductance3 - 1000;
fsrForce3 /= 30;
}
}
Serial.println(flagrandom);
if (fsrForce > 90 || fsrForce2 > 90 || fsrForce3 > 90)
{
if (flagpukul == randomNumber)
{
sampling++;
fsrForces += fsrForce;
fsrForces2 += fsrForce2;
fsrForces3 += fsrForce3;

if (sampling > 2)
{
fsrForces = fsrForces / 3;
fsrForces2 = fsrForces2 / 3;
fsrForces3 = fsrForces3 / 3;
Serial.println(fsrForce);
String datakirim = "*" + (String)fsrForces + "," +
(String)fsrForces2 + "," + (String)fsrForces3 + "#";

38
Serial2.println(datakirim);
Serial.print(datakirim);
if (fsrForce > 90)
{
flagkirim = 1;
Serial.println("Sensor1");
//digitalWrite(ledPin1, HIGH);
}
else if (fsrForce2 > 90)
{
flagkirim = 2;
Serial.println("Sensor2");
//digitalWrite(ledPin2, HIGH);
}
else if (fsrForce3 > 90)
{
flagkirim = 3;
Serial.println("Sensor3");
//digitalWrite(ledPin3, HIGH);
}
fsrForce = 0;
fsrForce2 = 0;
fsrForce3 = 0;
fsrForces = 0;
fsrForces2 = 0;
fsrForces3 = 0;
sampling = 0;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(2000);
flagrandom = 0;
flagpukul = 0;
}
else
{
Serial.println(fsrForce);
}
}

39
}

/*////TIMER///
if (millis() - timer1 > 1000)
{
timer1 = millis();
//Sensor 1
Serial.println("----------------------------SENSOR 1-----------------------
------");
Serial.print("Analog reading = ");
Serial.println(fsrReading);
Serial.print("Voltage reading in mV = ");
Serial.println(fsrVoltage);
if (fsrVoltage <= 1000) {
Serial.println("No pressure");
}
else {
Serial.print("FSR resistance in ohms = ");
Serial.println(fsrResistance);
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance);
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
}

///Sensor 2
Serial.println("--------------------");
Serial.println("----------------------------SENSOR 2-----------------------
------");
Serial.print("Analog reading2 = ");
Serial.println(fsrReading2);
Serial.print("Voltage2 reading in mV = ");
Serial.println(fsrVoltage2);
if (fsrVoltage2 <= 1000) {
Serial.println("No pressure");
}
else {
Serial.print("FSR resistance2 in ohms = ");
Serial.println(fsrResistance2);

40
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance2);
Serial.print("Force2 in Newtons: ");
Serial.println(fsrForce2);
}
Serial.println("--------------------");

///Sensor 3
Serial.println("----------------------------SENSOR 3-----------------------
------");
Serial.print("Analog reading3 = ");
Serial.println(fsrReading3);
Serial.print("Voltage2 reading in mV = ");
Serial.println(fsrVoltage2);
if (fsrVoltage3 <= 1000) {
Serial.println("No pressure");
}
else
{
Serial.print("FSR resistance3 in ohms = ");
Serial.println(fsrResistance3);
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance3);
Serial.print("Force3 in Newtons: ");
Serial.println(fsrForce3);
}
Serial.println("--------------------");
}*/
}

2. Program Koneksi anatara Arduino ke ESP32 Untuk WEB


#include <WiFi.h>
#include <HTTPClient.h>

// Replace with your network credentials


const char *ssid = "Oktavianus";
const char *password = "okta1810";
String serverName = "http://sandsack-
counter.herokuapp.com/data/send?";

41
String dataIn;
String dt[10];
int i;
boolean parsing = false;

void setup()
{
Serial.begin(9600);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
dataIn = "";
}

void loop()
{
if (Serial.available() > 0) {
delay(10);
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}

if (parsing) {
parsingData();
parsing = false;
dataIn = "";

42
}
}

void parsingData() {
int j = 0;
dt[j] = "";

for (i = 1; i < dataIn.length(); i++) {


if ((dataIn[i] == '#') || (dataIn[i] == ','))
{
j++;
dt[j] = "";
}
else
{
dt[j] = dt[j] + dataIn[i];
}
}
Serial.println("data1=" + dt[0]);
Serial.println("data2=" + dt[1]);
Serial.println("data3=" + dt[2]);

if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;

String serverPath = serverName + "data1=" + (String)dt[0] +


"&data2=" + (String)dt[1] + "&data3=" + (String)dt[2];

// Your Domain name with URL path or IP address with path


http.begin(serverPath.c_str());

// Send HTTP GET request


int httpResponseCode = http.GET();

if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);

43
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}

BIODATA PENULIS

44

You might also like