Arduino 16-step sequencer - Notes not in sync - arduino

I am using the following code for my arduino mega to send midi clock out to my drum machine & synth.
The problem appears when I also try to send midi notes exactly on the 'beat'.
Only the first note is perfectly synced, the rest of them are out of sync and you can hear it.
Is there any apparent flaw in the code?
// METRONOME
#include <MIDI.h>
#define LED1PIN 13
#define LED4PIN 7
#define SWITCHAPIN 5
#define SWITCHBPIN 2
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
void setup() {
pinMode(LED1PIN, OUTPUT);
pinMode(LED4PIN, OUTPUT);
pinMode(SWITCHAPIN, INPUT);
pinMode(SWITCHBPIN, INPUT);
digitalWrite(SWITCHAPIN, HIGH);
digitalWrite(SWITCHBPIN, HIGH);
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial.begin(9600);
Serial.println("Setting up");
}
unsigned long nextClockTime = 0;
int clockDelay = 20;
int tickCount = 0;
byte running = 0;
int pula = 0;
int currentStep = 1;
int lastPulse = 0;
int currPulse = 0;
int nextTickCount = 0;
typedef struct {
int noteNumber;
int velocity;
int noteLength;
bool enabled;
}
Step;
Step stepData[100];
void loop() {
stepData[0].enabled = true;
stepData[0].velocity = 127;
stepData[0].noteNumber = 40;
stepData[23].enabled = true;
stepData[23].velocity = 127;
stepData[23].noteNumber = 40;
stepData[47].enabled = true;
stepData[47].velocity = 127;
stepData[47].noteNumber = 40;
stepData[71].enabled = true;
stepData[71].velocity = 127;
stepData[71].noteNumber = 40;
stepData[95].enabled = true;
stepData[95].velocity = 127;
stepData[95].noteNumber = 40;
MIDI.read();
unsigned long milliseconds = millis();
if (milliseconds > nextClockTime) {
if (running)
MIDI.sendRealTime(MIDI_NAMESPACE::Clock);
nextClockTime = milliseconds + clockDelay;
nextTickCount = tickCount + 1;
if (tickCount == 0) {
if (stepData[tickCount].enabled) {
MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
}
}
if (tickCount == 23) {
if (stepData[tickCount].enabled) {
MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
currPulse = millis(); //1500
Serial.println(currPulse - lastPulse); //1500-500
lastPulse = currPulse;
}
}
if (tickCount == 47) {
if (stepData[tickCount].enabled) {
MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
currPulse = millis();
Serial.println(currPulse - lastPulse);
lastPulse = currPulse;
}
}
if (tickCount == 71) {
if (stepData[tickCount].enabled) {
MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
currPulse = millis();
Serial.println(currPulse - lastPulse);
lastPulse = currPulse;
}
}
if (tickCount == 95) {
if (stepData[tickCount].enabled) {
//MIDI.sendNoteOn(stepData[tickCount].noteNumber, stepData[tickCount].velocity, 1);
Serial.print("STEP ENABLED: ");
Serial.println(tickCount);
currPulse = millis();
Serial.println(currPulse - lastPulse);
lastPulse = currPulse;
}
nextTickCount = 0;
}
tickCount = nextTickCount;
} else if (digitalRead(SWITCHBPIN) == LOW) {
if (running) {
MIDI.sendRealTime(MIDI_NAMESPACE::Stop);
running = 0;
digitalWrite(LED4PIN, LOW);
}
} else {
clockDelay = analogRead(A0) / 10;
}
if (pula == 0) {
if (!running) {
MIDI.sendRealTime(MIDI_NAMESPACE::Start);
tickCount = 0;
running = 1;
digitalWrite(LED4PIN, HIGH);
pula = 1;
}
}
}

The beats are not evenly spaced. 24 - 1 is 23, 48 - 24 is 24 etc.
A simple fix would be to put your first beat on 0 not 1. Instead of incrementing tickCount, set a variable nextTickCount to tickCount +1, and assign it to tickCount at the end of the if (milliseconds > nextClockTime) { conditional bracket.
Also, set nextTickCount to 0 when tickCount reaches 95, but don't play any note (it will be played on the 0 beat )

Related

ESP32-WROOM-32 PWD with millis

Beginner in Arduino and ESP-32 needs help.
Hello together,
I’m using the Pololu - VNH5019 Motor Driver Carrier to control a 12v motor with an ESP32.
In the following sketch i can speed up and speed down the ramp with delay();.
I tried to archiv the same result with millis(), but until now i could not make it.
What i am missing in my code.
Thanks in advance.
#define MOTOR_IN1 27
#define MOTOR_IN2 16
#define PWMPIN 14
#define frequency 40000
#define resolutionbit 8
const unsigned long eventInterval = 30;
unsigned long previousTime = 0;
void setup() {
pinMode(MOTOR_IN1, OUTPUT);
pinMode(MOTOR_IN2, OUTPUT);
ledcAttachPin(PWMPIN, 0); // assign the speed control PWM pin to a channel
ledcSetup(0, frequency, resolutionbit);
}
void loop() {
//with_delay();
with_millis();
}
//------------------------------------------
void with_delay() {
// set direction
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
// ramp speed up
for (int i = 0; i <= 255; i++) {
ledcWrite(0, i);
delay(30);
}
// ramp speed down
for (int i = 255; i >= 0; i--) {
ledcWrite(0, i);
delay(30);
}
}
//-------------------------------------------
void with_millis() {
unsigned long currentTime = millis();
if (currentTime - previousTime >= eventInterval) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
for (int i = 0; i <= 255; i++) {
ledcWrite(0, i);
previousTime = currentTime;
}
}
if (currentTime - previousTime >= eventInterval) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
for (int i = 255; i >= 0; i--) {
ledcWrite(0, i);
previousTime = currentTime;
}
}
}
Your problem is that the program gets stuck in the for loop.
You need to also create direction variable so the program that knows which if statement to execute.
You need to create some other logic that will increase the i variable without stopping the whole program.
The code:
//Initialize the i variable globaly:
int i = 0;
bool direction = 0;
//Your function:
void with_millis() {
unsigned long currentTime = millis();
if ((currentTime - previousTime >= eventInterval) && direction == true) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
i++;
if (i <= 255) {
ledcWrite(0, i);
previousTime = currentTime;
} elif (i > 255) {
i = 0;
direction = false;
}
}
if ((currentTime - previousTime >= eventInterval) && direction == false) {
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, HIGH);
i++;
if (i <= 255) {
ledcWrite(0, i);
previousTime = currentTime;
} elif (i > 255) {
i = 0;
direction = true;
}
}
}

Arduino hardware interrupt reliability issue

This may seem like a foolish problem and maybe its description is not the best I could have devised.
I am making a velocity sensor that uses two IR beams to calculate velocity based on the time it takes to break both beams.
I have two testing methods.
My hand (5-10 m/s)
A high speed cannon (30-60 m/s).
I have ruled out that it's a problem with the signal from the IR beams with an oscilloscope, when the code fails/works the data is identical on the scope.
My problem is that my code works when I use my hand, but still irregularly fails, while it fails more often at high speed. All the conditions are the same in both scenarios. What could be the issue?
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
const int rs = 9, en = 8, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
File root;
int fileNo = 0;
String currentFileName;
const int CS = 10;
const byte interruptPinStart = 2;
const byte interruptPinFinish = 3;
volatile unsigned long int startTimeMillis = 0;
volatile unsigned long int stopTimeMillis = 0;
volatile unsigned long int startTimeMicros = 0;
volatile unsigned long int stopTimeMicros = 0;
volatile unsigned long int microsDifference = 0;
volatile unsigned long int millisDifference = 0;
int launchNo = 0;
float currentVelocity = 0;
volatile boolean started = false;
String inputString = "";
boolean stringComplete = false;
const int txLed1 = 14;
const int statusLed1 = 15;
const int statusLed2 = 16;
volatile boolean triggerDone = false;
float velocity = 0;
String temp;
unsigned long int lockout = 0;
boolean lockedOut = false;
boolean fileFail = false;
int testNo = 0;
void setup() {
inputString.reserve(200);
pinMode(statusLed1, OUTPUT);
pinMode(statusLed2, OUTPUT);
pinMode(txLed1, OUTPUT);
Serial.begin(9600);
while (!Serial) {
;
}
lcd.begin(16, 2);
pinMode(interruptPinStart, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPinStart), startTrigger, RISING);
pinMode(interruptPinFinish, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPinFinish), stopTrigger, RISING);
Serial.print("Initializing SD card...");
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
root = SD.open("/");
newDirectory(root);
Serial.println("done!");
lcd.clear();
lcd.print(currentFileName);
tone(txLed1, 38000);
}
void loop() {
int millsDiff = millis() - stopTimeMillis;
if (triggerDone) {
lockedOut = true;
Serial.print("Micros Diffrence: ");
Serial.println(microsDifference);
Serial.print("Millis Difference: ");
Serial.println(millisDifference);
float millDiff = (float) millisDifference;
float microDiff = (float) microsDifference;
if (microDiff > 0) {
velocity = (float) 0.09 / (microDiff/1000000);
testNo++;
temp = String(launchNo) + "%" + String(microsDifference) + "%" + String(velocity);
if (velocity > 10.0) {
root = SD.open(currentFileName, FILE_WRITE);
if (root) {
root.println(temp);
root.close();
Serial.println(temp);
launchNo++;
} else {
Serial.println("error opening file, " + currentFileName);
fileFail = true;
}
}
if (fileFail) {
lcd.clear();
lcd.print("File Error");
lcd.setCursor(0, 1);
lcd.print("Vel " + String(launchNo) + ": " + String(velocity) + " m/s");
fileFail = false;
} else {
lcd.clear();
lcd.print("Test Number: " + String(testNo));
lcd.setCursor(0, 1);
lcd.print("Vel " + String(launchNo) + ": " + String(velocity) + " m/s");
}
}
triggerDone = false;
Serial.println("Test Number: " + String(testNo));
}
if (digitalRead(interruptPinStart) == LOW) {
digitalWrite(statusLed1, HIGH);
} else {
digitalWrite(statusLed1, LOW);
}
if (digitalRead(interruptPinFinish) == LOW) {
digitalWrite(statusLed2, HIGH);
} else {
digitalWrite(statusLed2, LOW);
}
}
void startTrigger() {
startTimeMicros = micros();
startTimeMillis = millis();
volatile int diff1 = startTimeMicros - startTimeMillis;
volatile int diff2 = startTimeMillis - stopTimeMillis;
if (diff2 > 200) {
if (started == false || diff1 > 1000) {
started = true;
triggerDone = false;
}
}
}
void stopTrigger() {
stopTimeMicros = micros();
stopTimeMillis = millis();
microsDifference = stopTimeMicros - startTimeMicros;
millisDifference = stopTimeMillis - startTimeMillis;
if ((millisDifference > 0 && millisDifference < 800) && started) {
microsDifference = stopTimeMicros - startTimeMicros;
millisDifference = stopTimeMillis - startTimeMillis;
started = false;
triggerDone = true;
}
}

Storing the value read previously until new pulse

I'm currently doing a project on an Arduino Uno. The project is based on receiving an IR Signal from an IR Remote and then based on the signal received, perform other operations.
The problem is that the signal gets reset every time. I want to store the value received from the IR Remote and then resets it if detects another pulse.
Here is my code :
int brojac = 0;
int pinData = 10;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
int vrime = 1000 ;
int storeValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinData, INPUT);
}
void loop() {
lengthHeader = pulseIn(pinData, LOW);
if (lengthHeader > 1500)
{
for (int i = 1; i <= 32; i++) {
bit = pulseIn(pinData, HIGH);
if (i > 16 && i <= 24)
if (bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
}
Serial.print("byteValue = ");
Serial.println(byteValue);
if(byteValue == 66){
digitalWrite(11,HIGH);
}
else{
digitalWrite(11,LOW);
}
delay(vrime);
byteValue = 0;
delay(250);
}
I got the answer by storing the value in a variable until a new variable is detected.
int pinData = 10;
int led = 11;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
int storeValue = 0;
int previousValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinData, INPUT);
pinMode(led, LOW);
}
void loop() {
lengthHeader = pulseIn(pinData, LOW);
if (lengthHeader > 1500)
{
for (int i = 1; i <= 32; i++) {
bit = pulseIn(pinData, HIGH);
if (i > 16 && i <= 24)
if (bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
}
Serial.print("byteValue = ");
Serial.println(byteValue);
**storeValue = byteValue;
if (storeValue != 0){
previousValue = storeValue;
}
Serial.print("Previous value = ");
Serial.println(previousValue);**
byteValue = 0;
delay(500);
}

Counter loop on pressure sensors arduino

I'm using four fsr pressure sensors in parallel connection to each other. Below are the coding for my project.
I have a problem with the counter values where it display four added counter values for each of the sensor.
I need to get a counter values where it is added each time pressure is detected and if there is no pressure anymore, it will remain the previous counter value.
int fsrPin[] = {0, 1, 2, 3};
int fsrReading;
int fsrVoltage;
unsigned long fsrResistance;
unsigned long fsrConductance;
long fsrForce;
int pinCount = 4;
int counter = 0;
void setup(void) {
for(int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(fsrPin[thisPin], OUTPUT);
}
Serial.begin(9600);
}
void loop(void) {
for(int thisPin = 0; thisPin < pinCount; thisPin++) {
fsrReading = analogRead(fsrPin[thisPin]);
Serial.print("Analog reading ");
Serial.print(fsrPin[thisPin]);
Serial.print("=> ");
Serial.println(fsrReading);
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
Serial.print("Voltage reading in mV = ");
Serial.println(fsrVoltage);
if (fsrVoltage == 0) {
Serial.println("No pressure");
} else {
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V
fsrResistance = 5000 - fsrVoltage;
fsrResistance *= 10000;
fsrResistance /= fsrVoltage;
Serial.print("FSR resistance in ohms = ");
Serial.println(fsrResistance);
fsrConductance = 1000000;
fsrConductance /= fsrResistance;
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance);
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
}
}
for(int thisPin=0; thisPin < pinCount; thisPin++){
if (fsrForce != 0) {
counter++;
Serial.print("Counter = ");
Serial.println(counter);
} else {
counter;
Serial.print("Counter = ");
Serial.println(counter);
}
}
Serial.println("--------------------");
}
delay(3000);
}
The actual problem is as below where I need to find a correct condition for looping the value of the counter:
for(int thisPin=0; thisPin < pinCount; thisPin++){
if (fsrForce != 0) {
counter++;
Serial.print("Counter = ");
Serial.println(counter);
} else {
counter;
Serial.print("Counter = ");
Serial.println(counter);
}
}
FOUR COUNTER VALUES IN EACH ANALOG READING
I believe the main problem is that you are checking if fsrForce != 0, However fsrForce does not really have a chance to get to zero because it's dependent on fsrVoltage and is set only if fsrVoltage is positive.
Just set fsrForce = 0 when fsrVoltage < VOLTAGE_THRESHOLD and you should get the expected behavior.
Try this:
int fsrPin[] = {
0,
1,
2,
3
};
int fsrReading;
int fsrVoltage;
unsigned long fsrResistance;
unsigned long fsrConductance;
long fsrForce;
int pinCount = 4;
int counter = 0;
// add threshold
const float VOLTAGE_THRESHOLD = 0.2;
void setup(void) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(fsrPin[thisPin], OUTPUT);
}
Serial.begin(9600);
}
void loop(void) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
fsrReading = analogRead(fsrPin[thisPin]);
Serial.print("Analog reading ");
Serial.print(fsrPin[thisPin]);
Serial.print("=> ");
Serial.println(fsrReading);
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
Serial.print("Voltage reading in mV = ");
Serial.println(fsrVoltage);
if (fsrVoltage < VOLTAGE_THRESHOLD) {
// reset the fsrForce when there is no pressure
fsrForce = 0
Serial.println("No pressure");
} else {
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V
fsrResistance = 5000 - fsrVoltage;
fsrResistance *= 10000;
fsrResistance /= fsrVoltage;
Serial.print("FSR resistance in ohms = ");
Serial.println(fsrResistance);
fsrConductance = 1000000;
fsrConductance /= fsrResistance;
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance);
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
}
}
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
if (fsrForce != 0) {
counter++;
}
Serial.print("Counter = ");
Serial.println(counter);
}
Serial.println("--------------------");
}
delay(3000);
}

Arduino - cannot get buzzer to buzz

I've hooked up a buzzer to pin 13 & ground on my Arduino.
The "Blink" example works fine and the buzzer sounds every second off and on as expected.
However, when I try to do the same buzz with my code, I can't get it to buzz upon a specific event. This is a homegrown security system - when the door is opened, I want the Arduino to check a PHP page which returns "armed" if the system has been armed.
Everything else seems to work except the buzzer part.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xD3, 0x6C };
char serverName[] = "mysite.com";
String currentLine = "";
String armed = "No";
int nFrontWindow = 0;
int sFrontWindow = 1;
int kitchenWindow = 2;
int bedroomWindow = 3;
int frontDoor = 4;
int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int threshold0 = 20;
int threshold1 = 20;
int threshold2 = 20;
int threshold3 = 20;
int threshold4 = 20;
int breach0 = 0;
int breach1 = 0;
int breach2 = 0;
int breach3 = 0;
int breach4 = 0;
int alarm0 = 0;
int alarm1 = 0;
int alarm2 = 0;
int alarm3 = 0;
int alarm4 = 0;
EthernetClient client;
void setup() {
pinMode(13, OUTPUT);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
if(Ethernet.begin(mac) == 0) { // start ethernet using mac & IP address
Serial.println("Failed to configure Ethernet using DHCP");
while(true) // no point in carrying on, so stay in endless loop:
;
}
delay(1000); // give the Ethernet shield a second to initialize
}
void loop() {
// read the analog in values:
val0 = analogRead(nFrontWindow);
val1 = analogRead(sFrontWindow);
val2 = analogRead(kitchenWindow);
val3 = analogRead(bedroomWindow);
val4 = analogRead(frontDoor);
// print the analog in values:
if (val0 > threshold0)
{
Serial.print("nFrontWindow: ");
Serial.print(val0);
Serial.print("\n");
if (alarm0)
{
if (breach0 < 10)
breach0++;
}
else
{
if (breach0 > 9)
{
alarm0 = 1;
send_alert(0);
}
else
breach0++;
}
}
else
{
if (alarm0)
{
if (breach0 > 0)
breach0--;
else
{
alarm0 = 0;
send_alert(10);
}
}
else
{
if (breach0 > 0)
breach0--;
}
}
if (val1 > threshold1)
{
Serial.print("sFrontWindow: ");
Serial.print(val1);
Serial.print("\n");
if (alarm1)
{
if (breach1 < 10)
breach1++;
}
else
{
if (breach1 > 9)
{
alarm1 = 1;
send_alert(1);
}
else
breach1++;
}
}
else
{
if (alarm1)
{
if (breach1 > 0)
breach1--;
else
{
alarm1 = 0;
send_alert(11);
}
}
else
{
if (breach1 > 0)
breach1--;
}
}
if (val2 > threshold2)
{
Serial.print("kitchenWindow: ");
Serial.print(val2);
Serial.print("\n");
if (alarm2)
{
if (breach2 < 10)
breach2++;
}
else
{
if (breach2 > 9)
{
alarm2 = 1;
send_alert(2);
}
else
breach2++;
}
}
else
{
if (alarm2)
{
if (breach2 > 0)
breach2--;
else
{
alarm2 = 0;
send_alert(12);
}
}
else
{
if (breach2 > 0)
breach2--;
}
}
if (val3 > threshold3)
{
Serial.print("bedroomWindow: ");
Serial.print(val3);
Serial.print("\n");
if (alarm3)
{
if (breach3 < 10)
breach3++;
}
else
{
if (breach3 > 9)
{
alarm3 = 1;
send_alert(3);
}
else
breach3++;
}
}
else
{
if (alarm3)
{
if (breach3 > 0)
breach3--;
else
{
alarm3 = 0;
send_alert(13);
}
}
else
{
if (breach3 > 0)
breach3--;
}
}
if (val4 > threshold4)
{
Serial.print("frontDoor: ");
Serial.print(val4);
Serial.print("\n");
if (alarm4)
{
if (breach4 < 10)
breach4++;
}
else
{
if (breach4 > 9)
{
alarm4 = 1;
send_alert(4);
}
else
breach4++;
}
}
else
{
if (alarm4)
{
if (breach4 > 0)
breach4--;
else
{
alarm4 = 0;
send_alert(14);
}
}
else
{
if (breach4 > 0)
breach4--;
}
}
delay(100);
}
void send_alert(int pin)
{
if (client.connect(serverName, 80)>0) {
client.flush();
Serial.print("\nconnected... ");
String link = "GET [PHP FILE GOES HERE]";
link += pin;
link += " HTTP/1.1";
Serial.print("Sending alert code: ");
Serial.print(pin);
Serial.print("\n\n");
client.println(link);
client.println("Host: mysite.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
delay(3000);
} else {
Serial.println("connection failed");
//handle this
}
Serial.print("Server response:\n");
Serial.print("----------------\n");
while(client.available()){
char c = client.read();
Serial.print(c);
currentLine += c;
if (c == '\n') {
currentLine = "";
}
if (currentLine.endsWith("armed"))
{
if (pin == 4 || pin == 3 || pin == 2 || pin == 1 || pin == 0)
{
Serial.print("\nBZZZZZZZZZZ\n");
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
}
Serial.print("\n----------------\n");
//else
// Serial.println("result not found");
client.stop();
client.flush();
}
I think your problem is that pin 13 is used by the Ethernet Shield. So are pin 10, 11, and 12. So use a pin other then them.
I think you problem is that you aren't generating a square wave to make sound the buzzer.
Since you are using Arduino you can just use tone() and noTone() functions, well explained in Arduino site.

Resources