Hi Im looking for a little help.
I am trying to dim an LED via either udp (from iphone) or by using a push button by holding it down.
I can do these individually but im having trouble putting them both together giving me the 2 switching options.
i have managed to dim the LED with the push button but as soon as i let go of the button the LED will go back to the udp state.
im hoping its something obvious so please forgive me if it is as i am quite new to all this.
here is the code(bit messy)
#include <SPI.h> // for Arduino later than ver 0018
#include <EthernetUdp.h> // UDP library from bjoern@cs.stanford.edu
#include <Ethernet.h>
// source: http://code.google.com/p/arduino/source/browse/trunk/libraries/?r=1094#libraries%2FEthernet
////////// NETWORK INFO ////////////////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Set your Ethernet Shield's MAC address here - make sure you replace the ZZs with your shield's values!
byte ip[] = { 192,168,0, 177 }; // Set your shield's desired IP address here - check your network for configuration details
//byte gateway[] = { 192,168,0,1 }; //if you need to set a gateway IP
//byte subnet[] = { 255,255,255,0 }; // Change this to your subnet address
unsigned int localPort = 7777; // local port to listen on (set this the same as Port # on Ardumote Params Screen)
///////////////////////////////////////////
///////// Pin Assignments /////////////////
int LEDPin2 = 2; //Set LEDPin to Pin 2 - Place an LED on Pin 2 of your ethernet shield for testing this code (pwm)
int Button43 = 43;
int buttonState2;
int ledBrightness2 = 0;
int fadeAmount2 = 5;
int reading2;
long lastDebounceTime2 = 0;
long debounceDelay2 = 50;
/////////////////////////////////////////
// buffers for receiving and sending data
char packBuff[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
/////////////////////////////////////////////////
EthernetUDP Udp;
void setup() {
// More info on Ethernet on Arduino's website: http://arduino.cc/en/Reference/EthernetBegin
// start the Ethernet and UDP:
Ethernet.begin(mac,ip); // If you don't need to set your default gateway or subnet manually, use this
// Ethernet.begin(mac,ip,gateway,subnet); // Use this line instead if you've manually set all the parameters
Udp.begin(localPort); //Setup UDP socket on port defined earlier
Serial.begin(9600); //Start Serial Communications with PC
pinMode(LEDPin2,OUTPUT); //Designate pin 2 as Output Pin
pinMode(Button43,INPUT);
}
void loop()
{
int pwmVal; // Integer that will hold our PWM values for later use
// if there's data available, read a packet
int packetSize = Udp.parsePacket(); // note that this includes the UDP header}
if(packetSize)
packetSize = packetSize - 8; // subtract the 8 byte header
Serial.print("Packet size: ");
Serial.println(packetSize);
// read the packet into packetBuffer and get the senders IP addr and port number
Udp.read(packBuff,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Message: ");
Serial.println(packBuff);
pwmVal = (packBuff[3] - '0')*100 + (packBuff[4] - '0')*10 + (packBuff[5] - '0'); //Get PWMXXX message, and use XXX to set an int between 0 and 255.
//////////////////////// Pin 2 (LED_Pin) /////////////////////////////////////
if (packBuff[0] = 'R' && packBuff[1]=='E' && packBuff[2]=='D') // Wait for "REDXXX" and use XXX as value for PWM
{
analogWrite(LEDPin2,pwmVal); //Set LED_Pin to PWM Value
Serial.println("RED on Pin 2"); //Write notification
}
else if (packBuff[0] = 'P' && packBuff[1]=='2' && packBuff[2]=='H') // If we get the message "P2H", then set LED_Pin (2) HIGH
{
digitalWrite(LEDPin2,HIGH); //Turn on LED_Pin
Serial.println("LED ON"); //Write notification
}
else if (packBuff[0] = 'P' && packBuff[1]=='2' && packBuff[2]=='L') // If we get the message "P2L", then set LED_Pin (6) LOW
{
digitalWrite(LEDPin2,LOW); //Turn off LED_Pin
Serial.println("LED OFF"); //Write notification}
}
else if (reading2 != HIGH) {
lastDebounceTime2 = millis();
}
reading2 = digitalRead(Button43);
if ((millis() - lastDebounceTime2) > debounceDelay2) {
buttonState2 = reading2;
}
if (reading2 != HIGH) {
ledBrightness2 = ledBrightness2 + fadeAmount2;
if (ledBrightness2 <= 0 || ledBrightness2 >= 255) {
fadeAmount2 = -fadeAmount2;
}
analogWrite(LEDPin2, ledBrightness2);
delay(100);
}
delay(20);
}