My relay won't get HIGH after reading serial read

My relay won't get HIGH after reading serial read

Arduino code

#include <Wire.h>
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
/////////////////
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
//A Coin has been inserted flag
bool coins = false;
int relay = 8;
void setup(){
Serial.begin(9600);
  //Start Serial Communication
  attachInterrupt(coinInt, coinInserted, RISING);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
}
void coinInserted()
{
  coinsValue = coinsValue + 1;
  coinsChange = 1;
}


void loop
{
if (coinsChange == 1)
  {
    coinsChange = 0;
    Serial.print("Credit: £");
    Serial.println(coinsValue);
  }
if (coinsValue == 5)
  {
    digitalWrite(relay, HIGH);

  }
    
   while(coins == true)
{
   
  
if (Serial.available() > 0 )
        
        {
          
          char ltr = Serial.read();
          
          if (ltr == 'a')

            digitalWrite(relay, HIGH);
            coinsValue = 0;
            coins = false;
             Serial.flush();
             return;
           }
           else
            {
              break;
            }

    

}

}
Unity code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO.Ports;
using System.Threading;

public class GameTimer : MonoBehaviour

{
SerialPort sp = new SerialPort("COM3", 9600);
 public Text gameTimerText;
 public static float gameTimer = 10f;

void start()
{
 sp.Open();
  sp.ReadTimeout = 1;

}

void update()
{
 void Update()
    {
        if (sp.IsOpen)
        {
            try
            {
                
                print(sp.ReadByte());
            }
            catch (System.Exception)
            {

            }
        }
gametimer-= 1 * Time.deltaTime;
gameTimerText.text = gameTimer.ToString("0");

if (gameTimer <= 0)
            {
                sp.Write("a");
               
            }
}


}

Your Arduino code does not compile
Quite apart from other errors it does not have a setup() function

Have you posted the right version ?

Oh sorry I put the wrong code it should be void setup() not void start().

It still does not compile

void coinInserted()
{
  coinsValue = coinsValue + 1;
  coinsChange = 1;
}

{
  void loop
  {

There is an extra { in your code and look carefully at the name of the loop function. Is something missing ?

If you post revised code again please put it in new post rather than revising an old one so that comments in the thread make sense

The revised arduino code

Arduino code

#include <Wire.h>
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
/////////////////
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
//A Coin has been inserted flag
bool coins = false;
int relay = 8;
void setup(){
Serial.begin(9600);
  //Start Serial Communication
  attachInterrupt(coinInt, coinInserted, RISING);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
}
void coinInserted()
{
  coinsValue = coinsValue + 1;
  coinsChange = 1;
}


void loop
{
if (coinsChange == 1)
  {
    coinsChange = 0;
    Serial.print("Credit: £");
    Serial.println(coinsValue);
  }
if (coinsValue == 5)
  {
    digitalWrite(relay, HIGH);

  }
   
   while(coins == true)
{
   
if (Serial.available() > 0 )     
        {       
          char ltr = Serial.read();
         
          if (ltr == 'a')

            digitalWrite(relay, HIGH);
            coinsValue = 0;
            coins = false;
             Serial.flush();
             return;
           }
           else
            {
              break;
            }
}

}

Have you tried to compile that code ?

Obviously not, because it won't

Sorry I forgot to put () in void loop

#include <Wire.h>
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
/////////////////
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
//A Coin has been inserted flag
bool coins = false;
int relay = 8;
void setup(){
Serial.begin(9600);
  //Start Serial Communication
  attachInterrupt(coinInt, coinInserted, RISING);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
}
void coinInserted()
{
  coinsValue = coinsValue + 1;
  coinsChange = 1;
}


void loop()
{
if (coinsChange == 1)
  {
    coinsChange = 0;
    Serial.print("Credit: £");
    Serial.println(coinsValue);
  }
if (coinsValue == 5)
  {
    digitalWrite(relay, HIGH);

  }
   
   while(coins == true)
{
   
if (Serial.available() > 0 )     
        {       
          char ltr = Serial.read();
         
          if (ltr == 'a')

            digitalWrite(relay, HIGH);
            coinsValue = 0;
            coins = false;
             Serial.flush();
             return;
           }
           else
            {
              break;
            }
}

}

UKHeliBob:
Have you tried to compile that code ?

Obviously not, because it won't

I revised the code did you find the solution?

Thank you in advance

I revised the code did you find the solution?

Thank you in advance

I don't appreciate a nudge after only 30 minutes. When is your assignment due to be submitted ?

Take a look at the coins variable
It is declared false
Later you have this line

 while (coins == true)

But the value of coins has not been changed. In fact it is never set to true in the program

UKHeliBob:
I don't appreciate a nudge after only 30 minutes. When is your assignment due to be submitted ?

Take a look at the coins variable
It is declared false
Later you have this line

 while (coins == true)

But the value of coins has not been changed. In fact it is never set to true in the program

I revised it but still not been solved.

#include <Wire.h>
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
/////////////////
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
//A Coin has been inserted flag
bool coins = false;
int relay = 8;
void setup(){
Serial.begin(9600);
  //Start Serial Communication
  attachInterrupt(coinInt, coinInserted, RISING);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);
}
void coinInserted()
{
  coinsValue = coinsValue + 1;
  coinsChange = 1;
}


void loop()
{
if (coinsChange == 1)
  {
    coinsChange = 0;
    Serial.print("Credit: £");
    Serial.println(coinsValue);
  }
if (coinsValue == 5)
  {
     coins = true;
    digitalWrite(relay, HIGH);

  }
   
   while(coins == true)
{
   
if (Serial.available() > 0 )     
        {       
          char ltr = Serial.read();
         
          if (ltr == 'a')

            digitalWrite(relay, HIGH);
            coinsValue = 0;
            coins = false;
             Serial.flush();
             return;
           }
           else
            {
              break;
            }
}

}
//Volatile as this variable changes any time the Interrupt is triggered

So have you done this? All variables used in an interrupt service routine need to be volatile.

Grumpy_Mike:

//Volatile as this variable changes any time the Interrupt is triggered

So have you done this? All variables used in an interrupt service routine need to be volatile.

When I send 'a' from serial monitor it works but when the data is from unity it doesn't work.

Badly needed cause tomorrow is our thesis defense. Thank you

Did you note that all variables used in an interrupt service routine need to be volatile.

UKHeliBob:
Did you note that all variables used in an interrupt service routine need to be volatile.

I used volatile to increment the coinsValue until it reach 5.

Should I change it?

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
void coinInserted()
{
  coinsValue = coinsValue + 1;
  coinsChange = 1;
}

Are all of the variables used in the ISR declared as volatile ?

UKHeliBob:

volatile float coinsValue = 0.00;

//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;





void coinInserted()
{
  coinsValue = coinsValue + 1;
  coinsChange = 1;
}




Are [u]all[/u] of the variables used in the ISR declared as volatile ?

Yeah. When inserted a coin the output will be

Credit: £1
Credit: £2
Credit: £3
Credit: £4
Credit: £5

Yeah

int coinsChange = 0;

What about this one ?

UKHeliBob:

int coinsChange = 0;

What about this one ?

const int coinInt = 0; 
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;                  
//A Coin has been inserted flag

void setup()
{
  Serial.begin(9600);                 
//Start Serial Communication
  attachInterrupt(coinInt, coinInserted, RISING);   
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()    
//The function that is called every time it recieves a pulse
{
  coinsValue = coinsValue + 1;  
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
  coinsChange = 1;                           
//Flag that there has been a coin inserted
}

void loop()
{
  if(coinsChange == 1)          
//Check if a coin has been Inserted
  {
    coinsChange = 0;              
//unflag that a coin has been inserted
  
    Serial.print("Credit: £");
    Serial.println(coinsValue);    
//Print the Value of coins inserted
  }
}

That's the full code of my coinslot

Read this comment from your code

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered

Now, what about the coinsChange ?
Does that change when the interrupt is triggered ?

UKHeliBob:
Read this comment from your code
Now, what about the coinsChange ?
Does that change when the interrupt is triggered ?

Yes it change.