#include #include #include "HX711.h" #include #include EthernetUDP Udp; //added this to fix the UDP error #include IPAddress ip(2, 0, 0, 100); IPAddress outIp(2, 0, 0, 1); const unsigned int outPort = 53000; byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // HX711 circuit wiring const int LOADCELL_DOUT_PIN = A5; const int LOADCELL_SCK_PIN = A4; long reading; HX711 scale; int potPin = 0; float readVal = 0; float mult = 0; float ran; bool triggered = false; const int switchPin = 2; int switchState = 0; const int chipSelect = 4; String prize; const char * cue; void setup() { Serial.begin(9600); Ethernet.begin(mac, ip); Udp.begin(8888); scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); pinMode(switchPin, INPUT); Serial.print("Initializing SD card..."); // see if the card is present and can be initialized: if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); // don't do anything more: while (1); } Serial.println("card initialized."); File dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) { dataFile.println("------New Session------"); dataFile.close(); } else { Serial.println("Error opening log file"); } } void loop() { switchState = digitalRead(switchPin); // Serial.println(switchState); if (scale.is_ready()) { reading = scale.read(); Serial.println(reading); } if (reading < 1200000 && triggered == true) { delay(2000); //debounce removal triggered = false; } if (reading > 1400000 && triggered == false) { prize = "handset"; //set prize to handset incase always win is turned on cue = "/cue/1/start"; triggered = true; if (switchState == LOW) { //check if always win not on ran = random(1, 100); readVal = analogRead(potPin); readVal = map(readVal, 0,1023,50,150); mult = (readVal / 100); // Serial.println(mult); if (ran <= 12*mult) { prize = "handset"; cue = "/cue/1/start"; } else if (ran > 12*mult && ran <= 37*mult) { prize = "voucher"; cue = "/cue/2/start"; } else if (ran > 37*mult && ran <= 45*mult) { prize = "Home"; cue = "/cue/3/start"; } else if (ran > 45*mult && ran <= 65*mult) { prize = "Mini"; cue = "/cue/4/start"; } else { prize = "Nothing"; cue = "/cue/5/start"; } }//end always win checking Serial.println(prize); Serial.println(cue); OSCMessage msg(cue); Udp.beginPacket(outIp, outPort); msg.send(Udp); Udp.endPacket(); msg.empty(); File dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) { dataFile.println(prize); dataFile.close(); } else { Serial.println("Error opening log file"); } delay(2000); } }