Hello,
I am having a problem where multiple scripts that previously worked in showing up on the serial monitor no longer start functioning (water should be released upon startup) or showing up on the serial monitor, despite getting "Done Uploading". Main Script Below:
//All of the baseline stuff - probably extra but makes it easier
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_MCP4725.h>
#include <TrueRandom.h>
#include <elapsedMillis.h>
#include <Average.h>
#define PI 3.1415926535897932384626433832795
Adafruit_MCP4725 dac;
elapsedMillis timeElapsed;
// Pin assignments
int H2OL = 7;
int H2OR = 6;
int odorAirSupply = 4; //"hem" in taskdata , First valve - supplies air to others
int odorAB = 11; //LOW=A;HIGH=B //"stim" in taskdata , Switches between the two odors within trial
int odorPair = 10; //Low = AB, HIGH = CD
int odorCD = 8;
int speakerR = 22;
int speakerL = 23;
// Variables
int delayTimeL = 50; //35; //open time on left solenoid
int delayTimeR = 50; //35; //open time on right solenoid
int waiting = 0;
int LwaterGood = 0;
int RwaterGood = 0;
int Airsupplygood = 0;
int ABvalvegood = 0;
int odorPairValveGood = 0;
int CDvalvegood = 0;
int LspeakerGood = 0;
int RspeakerGood = 0;
int lickedR = 0;
int lickedL = 0;
int lickNumR = 0;
int lickNumL = 0;
int RcircuitGood = 0;
int LcircuitGood = 0;
float lickThresh = 60; //threshold voltage difference for lick detection
int lickPrev = 0;
Average<float> lickAveR(3); //initialize a moving window for detecting right licks
Average<float> lickAveL(3); //initialize a moving window for detecting left licks
int i;
int f = 155; //wave frequency
long i2c = 3120; //i2c clk rate
int bit_depth = 12;
int norm_weight = pow(2,bit_depth); //signal multiplier based on bit_depth
float sig; //normalized wave signal
float wave_steps = (i2c/f);
float final_sig;
void setup() {
//Set up of pin assignments etc.
pinMode(H2OL, OUTPUT);
pinMode(H2OR, OUTPUT);
pinMode(odorAirSupply, OUTPUT);digitalWrite(odorAirSupply, LOW);
pinMode(odorAB, OUTPUT); digitalWrite(odorAB, LOW); //Low=A,High=B;
pinMode(odorPair, OUTPUT);digitalWrite(odorPair, LOW);
pinMode(odorCD,OUTPUT);digitalWrite(odorCD, LOW);
Serial.begin(9600);
Wire.begin();
Wire.setClock(400000); // Set the SCL clock speed to 400kHz
dac.begin(0x62);
pinMode(speakerL, OUTPUT); digitalWrite(speakerL, LOW);
pinMode(speakerR, OUTPUT); digitalWrite(speakerR, LOW);
}
void loop() {
// Checking water valves:
Serial.println("Begin");
digitalWrite(H2OL,HIGH);
delay(delayTimeL);
digitalWrite(H2OL,LOW);
Serial.println("Water in Left lick spout? [1=yes, 2=try again]");
waiting = 1;
while (waiting == 1) {
if (Serial.available() > 0) { // is a character available?
LwaterGood = Serial.parseInt(); // get the character
if (LwaterGood == 1) {
Serial.println("The left lick spout is ready to go!");
waiting = 2;
}
if (LwaterGood == 2) {
digitalWrite(H2OL,HIGH);
delay(delayTimeL);
digitalWrite(H2OL,LOW);
LwaterGood = 0;
}
}
}
if (LwaterGood == 1) {
digitalWrite(H2OR,HIGH);
delay(delayTimeR);
digitalWrite(H2OR,LOW);
Serial.println("Water in Right lick spout? [1=yes, 2=try again]");
while (waiting == 2) {
if (Serial.available() > 0) { // is a character available?
RwaterGood = Serial.parseInt(); // get the character
if (RwaterGood == 1) {
Serial.println("The right lick spout is ready to go!");
waiting = 3;
}
if (RwaterGood == 2) {
digitalWrite(H2OR,HIGH);
delay(delayTimeR);
digitalWrite(H2OR,LOW);
RwaterGood = 0;
}
}
}
}
// Checking air valves
if (RwaterGood == 1) {
Serial.println("Water distribution is good to go!");
Serial.println("Do you smell odor A? [1=Yes, 2=return valve for 10 seconds]");
while (waiting == 3) {
digitalWrite(odorAirSupply, HIGH);
if (Serial.available() > 0) { // is a character available?
Airsupplygood = Serial.parseInt(); // get the character
if (Airsupplygood == 1) {
Serial.println("The A/B odor valve is good!");
waiting = 4;
}
if (Airsupplygood == 2) {
digitalWrite(odorAirSupply,LOW);
delay(10000);
Airsupplygood = 0;
}
}
}
}
if (Airsupplygood == 1) {
Serial.println("Do you smell odor B? [1=yes, 2=return valve for 10 seconds]");
while (waiting == 4) {
digitalWrite(odorAirSupply, HIGH);
digitalWrite(odorAB,HIGH);
if (Serial.available() > 0) { // is a character available?
ABvalvegood = Serial.parseInt(); // get the character
if (ABvalvegood == 1) {
Serial.println("The A/B valve is good!");
digitalWrite(odorAB,LOW);
digitalWrite(odorAirSupply, LOW);
waiting = 5;
}
if (ABvalvegood == 2) {
digitalWrite(odorAB,LOW);
delay(10000);
Airsupplygood = 0;
}
}
}
}
if (ABvalvegood == 1) {
Serial.println("Do you smell odor C? [1=yes, 2=return valve for 10 seconds]");
while (waiting == 5) {
digitalWrite(odorAirSupply, HIGH);
digitalWrite(odorPair, HIGH);
digitalWrite(odorCD,LOW);
if (Serial.available() > 0) { // is a character available?
odorPairValveGood = Serial.parseInt(); // get the character
if (odorPairValveGood == 1) {
Serial.println("The odorPair valve is good!");
digitalWrite(odorCD,LOW);
digitalWrite(odorPair, LOW);
digitalWrite(odorAirSupply, LOW);
waiting = 6;
}
if (odorPairValveGood == 2) {
digitalWrite(odorPair, LOW);
delay(10000);
Airsupplygood = 0;
}
}
}
}
if (odorPairValveGood == 1) {
Serial.println("Do you smell odor D? [1=yes, 2=return valve for 10 seconds]");
while (waiting == 6) {
digitalWrite(odorAirSupply, HIGH);
digitalWrite(odorPair, HIGH);
digitalWrite(odorCD,HIGH);
if (Serial.available() > 0) { // is a character available?
CDvalvegood = Serial.parseInt(); // get the character
if (CDvalvegood == 1) {
Serial.println("The C/D valve is good!");
digitalWrite(odorCD,LOW);
digitalWrite(odorPair, LOW);
digitalWrite(odorAirSupply, LOW);
waiting = 7;
}
if (CDvalvegood == 2) {
digitalWrite(odorCD,LOW);
delay(10000);
Airsupplygood = 0;
}
}
}
}
// Checking lick detection
if (CDvalvegood == 1) {
Serial.println("Connect aluminum tube to RIGHT lick spout");
while (waiting == 7) {
lickAveR.push(analogRead(A5) - analogRead(A0)); //Because of sliding window next 2 runs through loop LickAveR.mean will > lickThresh
if (lickAveR.mean() > lickThresh) { // is a character available?
Serial.println("Right spout can sense licks");
RcircuitGood = 1;
waiting = 8;
}
}
}
// Make mean values for L and R lick spout voltages include values without connectivity
// Otherwise the mean for lickAveL will include one or more values from connection with right spout
delay(1000);
if (RcircuitGood == 1){
Serial.println("Connect aluminum tube to LEFT lick spout");
while (waiting == 8) {
lickAveL.push(analogRead(A0) - analogRead(A5)); //Because of sliding window next 2 runs through loop LickAveL.mean will > lickThresh
if (lickAveL.mean() > lickThresh) {
Serial.println("Left spout can sense licks");
LcircuitGood = 1;
waiting = 9;
}
}
}
//Checking Speakers
if (LcircuitGood ==1){
Serial.println("Do you hear a tone in the left speaker? [1=yes, 2=switch speakers for 10s]");
digitalWrite(speakerL, HIGH);
while (waiting == 9){
timeElapsed = 0;
while (timeElapsed <= 2500){
for (i = 1; i < wave_steps; i++){
sig = (norm_weight/2)*(1+sin((2*PI)*(i/wave_steps)));
final_sig = sig*0.5;
dac.setVoltage(final_sig, false);
}
}
if (Serial.available() > 0) { // is a character available?
LspeakerGood = Serial.parseInt(); // get the character
if (LspeakerGood == 1){
digitalWrite(speakerL, LOW);
dac.setVoltage(0,false);
waiting = 10;
}
if (LspeakerGood ==2){
digitalWrite(speakerL, LOW);
digitalWrite(speakerR, HIGH);
delay(10000);
digitalWrite(speakerR, LOW);
digitalWrite(speakerL, HIGH);
}
}
}
}
if (LspeakerGood ==1){
Serial.println("Do you hear a tone in the right speaker? [1=yes, 2=switch speakers for 10s]");
digitalWrite(speakerR, HIGH);
while (waiting == 10){
timeElapsed = 0;
while (timeElapsed <= 2500){
for (i = 1; i < wave_steps; i++){
sig = (norm_weight/2)*(1+sin((2*PI)*(i/wave_steps)));
final_sig = sig*0.5;
dac.setVoltage(final_sig, false);
}
}
if (Serial.available() > 0) { // is a character available?
RspeakerGood = Serial.parseInt(); // get the character
if (RspeakerGood == 1){
digitalWrite(speakerR, LOW);
dac.setVoltage(0,false);
waiting = 11;
}
if (RspeakerGood ==2){
digitalWrite(speakerR, LOW);
digitalWrite(speakerL, HIGH);
delay(10000);
digitalWrite(speakerL, LOW);
digitalWrite(speakerR, HIGH);
}
}
}
}
if (RspeakerGood == 1){
Serial.println("Input the frequnecy [in Hz] of the desired tone");
digitalWrite(speakerR, HIGH);
digitalWrite(speakerL, HIGH);
while (waiting == 11){
if (Serial.available() > 0) { // is a character available?
f = Serial.parseInt(); // get the character
float sig; //normalized wave signal
float wave_steps = (i2c/f);
float final_sig;
int norm_weight = pow(2,bit_depth); //signal multiplier based on bit_depth
timeElapsed = 0;
while (timeElapsed <= 2500){
for (i = 1; i < wave_steps; i++){
sig = (norm_weight/2)*(1+sin((2*PI)*(i/wave_steps)));
final_sig = sig*0.5;
dac.setVoltage(final_sig, false);
}
}
}
}
}
}
The correct COM port is in, and I have checked to make sure that the code is no different than it has been previously for some reason. I have also confirmed that the Blink example sketch works. Additionally, I have run simpler scripts that require the serial monitor but fewer of the pins relative to the above script and the serial monitor displays fine. I figured that perhaps the 12V connection wasn't working and therefore not providing enough power, but when I unplugged the USB cable the arduino's light remained on with the 12V plug in. I just replaced the Mega board with a new one and am having the same issues. Please advise or let me know if you need additional information