Serial.read() Serial.write() LEDs not turning on [FIXED]

[SOLVED - THANKS TO THE EYES OF GRUMPY_MIKE]

I have a strange suspicion as always, that this may be a simple error hidden from my little designer trying to program mind but I’ve literally gone in and out of anything I could find to solve it.

This is my first post so let me know if there’s anything additional I need to post up.

I would like my LED to fade in and then out as it does currently. But for some reason when my twitter responds and i try to trigger serial.write(‘H’) to my Arduino from Processing, my lights aren’t responding.

Could someone please help? I’d be eternally greatful.

const int numReadings = 1;
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;


int SENSOR = 1;
int ledPin = 3;

char val;

int fade = 0;



void setup() {
  pinMode(SENSOR,INPUT);
  pinMode(ledPin,OUTPUT);

  Serial.begin(9600);
  
   if (Serial.available() > 0) {   
    while (Serial.available() > 0) val = Serial.read(); //arduino-processing evening read speed
}              
      
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  readings[thisReading] = 0;  
}

void loop() {

  
  bool bBelow = false;

  total= total - readings[index];           // subtract the last reading:  
  readings[index] = analogRead(SENSOR);  


  total= total + readings[index];          // add the reading to the total:
  index = index + 1;                       // advance to the next position in the array:    
  if (index >= numReadings) {             //back to beginning
    index = 0;            
  }
  
  average = total / numReadings;             // calculate the average:
 
 
 while (val == 'H') {
   digitalWrite(ledPin, 100);
 }
 
  if ( average < 30)
  {
    bBelow = true;
    Serial.write(bBelow);
  }
  else {
    bBelow = false;
    Serial.write(bBelow);
  }
    
  
  
  
  if (bBelow)
  {   
    fade = constrain(fade,0,255);
    analogWrite(ledPin, fade);
    fade += 1;
  }
  else
  {
    fade = constrain(fade,0,255);
    analogWrite(ledPin, fade);
    fade -= 1;
  }
  
}
import processing.serial.*;

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;

import ddf.minim.*;
Minim minim;
AudioSample bird;
AudioSample crowd;


static String OAuthConsumerKey = "1HSzRRYB9ut4dJNCpslrCw";
static String OAuthConsumerSecret = "8SzVraC7Ga579OLuElqMUkuYpDA2pjNUrKLSrHcv6w";
static String AccessToken = "416197785-tTqgADnyKE6NpGCU4YLa41WznlSFMmb9CjYov9LG";
static String AccessTokenSecret = "tpRs2tLubcRc5PcbKb0KSJklDATdcBAT5bFRD9EoSQ";

Serial myPort;

Twitter twitter = new TwitterFactory().getInstance();

String currentMention = "";
String currentHash ="";
String currentTweet ="";
String hashlist ="";

int trigger = 0;
int tweetTrigger = 0;

void setup() {
  size(125, 125);
//  frameRate(10);
  background(0);
  
  println(Serial.list());
  String arduinoPort = Serial.list()[0]; 
  myPort = new Serial(this, arduinoPort, 9600);
  
  minim = new Minim(this);
  crowd = minim.loadSample("hallway-crowd.mp3",1026);
  crowd.mute();
  
  bird = minim.loadSample("Morning2.wav",1026);
  bird.mute();
  
  loginTwitter();

}

void loginTwitter() {
  twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
  AccessToken accessToken = loadAccessToken();
  twitter.setOAuthAccessToken(accessToken);
}

private static AccessToken loadAccessToken() {
  return new AccessToken(AccessToken, AccessTokenSecret);
}

void draw() {
  background(0);
  
  if ( myPort.available() > 0) {  // If data is available,
    while ( myPort.available() > 0)  trigger = myPort.read();  //arduino-processing evening read speed
      println(trigger);
     
      if (trigger == 1) {
        crowd.trigger();
        crowd.unmute();  
      } else {
        crowd.mute();
      }

  } else {
  
  text("@tweet", 35, 65);   
  getHash();
  getMention();  
  getNewTweet();
  //frameRate(10);
  delay(10000); // wait 15 seconds to avoid Twitter Rate Limit
  }
}


  

void getMention() {

  List mentions = null;
  try {
    mentions = twitter.getMentions();
  }
  catch(TwitterException e) { 
    println("Exception: " + e + "; statusCode: " + e.getStatusCode());
  }
  
  Status status = (Status)mentions.get(0);
  String newMention = str(status.getId());
  
  if (currentMention.equals(newMention) == false){
    currentMention = newMention;
    println(status.getText()+", by @"+status.getUser().getScreenName());
    bird.trigger();
    bird.unmute();
    
    myPort.write('H');
  
} else {
    bird.mute();
    myPort.write('L');
  }

}


void getHash() {

  String queryStr = "#grAAD";

  try {
    Query query = new Query(queryStr);    
    query.setRpp(1); // Get 10 of the 100 search results  
    
    QueryResult result = twitter.search(query);      
    List<Tweet>tweets = result.getTweets();
    
    for (Tweet tweet: tweets) {
      hashlist = tweet.getText();
      String newHash = hashlist;
      
        if(currentHash.equals(newHash)==false) {
            currentHash = newHash;
            println("@" + tweet.getFromUser() + " - " + tweet.getText());
            
            myPort.write('H');            
            
            bird.trigger();
            bird.unmute();     
        } else {
            bird.mute();
            myPort.write('L');
        }
    } 
  }  catch (TwitterException e) {    
    println("Search tweets: " + e);  
    }
}

void getNewTweet() {
  try {
    List statuses = twitter.getUserTimeline(new Paging(1,1)); 
    
    for(int i=0; i<statuses.size(); i++) {
      Status status = (Status)statuses.get(i);
      
      String newTweet = status.getText();
      
          if(currentTweet.equals(newTweet)==false) {
            
            currentTweet = newTweet;
            println(status.getUser().getName() + ": " + status.getText());
            
            myPort.write('H');
           
            bird.trigger();
            bird.unmute();
            
          } else {
          bird.mute();
          myPort.write('L');
        }
    }
      
   
  } catch(TwitterException e) { 
    println("Get timeline: " + e + " Status code: " + e.getStatusCode());
  }

}

I’ve also tried an if else if:

if ( average < 30)
{
bBelow = true;
Serial.write(bBelow);
}
else if (val == ‘H’) {
bBelow = true;
} else {
bBelow = false;
Serial.write(bBelow);
}

with no success. :sleeping: :sleeping:

Please modify that post, select the code hit the # icon and save it.

You are only looking at the serial input in the setup() function, this gets executed once only on start up. I think you need to be looking at the serial input each time round the loop.

Now that's just silly. Thankyou thankyou thankyou for being my eyes and life saver Grumpy_Mike, I really could not see that. You've saved my poor laptops life, its been chugging on for days putting up with my nonsense.