Hello again guys!
I have a question regarding using the scheduler library that you can download with the SCoop library. I have tried using shceduler library designed to be used with the ardiuno uno to implement multiple tasking but have been on partially successful. My program has to display a value input by the user using serial monitor and also read a value from an external sensor(loadcell) and display that as well. both these task work well independently of one another but when I combine and try them using scheduler I am only able to light both the seven segment displays. One shows 000(the one thats supposed to show sensor readings) and the other has its digits flickering. I am using 2, 3 digit seven segment displays and using 74hc595 shift registers to drive them. Also like I said the codes for doing each of these tasks works well independently how ever combining them causes problems. I think its the while(1) statement on line 171
I would really appreciate any help that you could give me. Also do you think I'd be better off using the SCoop to do this. If so could you give me some pointers on doing that because I am really confused how to do it.
I have attached the code with this as well.
#include <SchedulerARMAVR.h>
//variables for actual_value
const int digitPins1[3] = {
4,5,6}; //4 common anode pins of the actual_value
const int clockPin1 = 11; //74HC595 Pin 11
const int latchPin1 = 12; //74HC595 Pin 12
const int dataPin1 = 13; //74HC595 Pin 14
const byte digit1[10] = //actual seven segment digits in bits
{
B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4
B01101101, //5
B01111101, //6
B00000111, //7
B01111111, //8
B01101111 //9
};
int digitBuffer1[3] = {
0};
int digitScan1 = 0;
float pressurekg = 0;
//variables for desired_value
const int digitPins2[3] = {
A4,A3,A2}; //4 common anode pins of the desired_display
const int clockPin2 = 7; //74HC595-2 Pin 11
const int latchPin2 = 8; //74HC595-2 Pin 12
const int dataPin2 = 10; //74HC595-2 Pin 14
const byte digit2[10] = //desired seven segment digits in bits
{
B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4
B01101101, //5
B01111101, //6
B00000111, //7
B01111111, //8
B01101111 //9
};
int digitBuffer2[3] = {
0};
int digitScan2 = 0;
unsigned int desiredValue = 0; //desiredValue input from the computer using serial coomunication
char incomingByte;
//variable for averaging
const int numReadings = 10;
int readings[numReadings]; // the readings from the difference of analog inputs
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void setup(){
//setup for actual_display
for(int i=0;i<3;i++)
{
pinMode(digitPins1[i],OUTPUT);
}
pinMode(latchPin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(dataPin1, OUTPUT);
//setup for desired_display
for(int i=0;i<3;i++)
{
pinMode(digitPins2[i],OUTPUT);
}
pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
//setup for averaging process
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
Scheduler.startLoop(loop2); //initiate our second loop
//void loop always started by default//
}
//writes the actual_pressure value on display
void updateDispActual(){
for(byte j=0; j<3; j++)
digitalWrite(digitPins1[j], LOW);
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, MSBFIRST, B11111111);
digitalWrite(latchPin1, HIGH);
delayMicroseconds(100);
digitalWrite(digitPins1[digitScan1], HIGH);
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, MSBFIRST, ~digit1[digitBuffer1[digitScan1]]);
digitalWrite(latchPin1, HIGH);
digitScan1++;
if(digitScan1>2) digitScan1=0;
}
//writes desired_pressure value to display
void updateDispDesired(){
for(byte j=0; j<3; j++)
digitalWrite(digitPins2[j], LOW);
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, B11111111);
digitalWrite(latchPin2, HIGH);
delayMicroseconds(100);
digitalWrite(digitPins2[digitScan2], HIGH);
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, ~digit2[digitBuffer2[digitScan2]]);
digitalWrite(latchPin2, HIGH);
digitScan2++;
if(digitScan2>2) digitScan2=0;
}
//does the averaging process
void averaging(){
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
int a = analogRead(A0);// reads the loadcell +ve voltage
int b = analogRead(A1);//reads the loadcell -ve voltage
int c = a-b; // calculates the differential voltage
readings[index] = c;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
pressurekg = (average*1.11)-1;//converts average to pressure in kg
}
//runs the loop for actual value of pressure
void loop(){
averaging();
//Pressure display in kgs
digitBuffer1[2] = (int(pressurekg)/100);
digitBuffer1[1] = (int(pressurekg)%100)/10;
digitBuffer1[0] = (int(pressurekg)%10);
updateDispActual();
Scheduler.delay(2);
yield();
}
//runs the loop for input value from the computer
void loop2(){
if (Serial.available() > 0) { // something came across serial
desiredValue = 0; // throw away previous integerValue
while(1) { // force into a loop until 'n' is received
incomingByte = Serial.read();
if (incomingByte == '\n') break; // exit the while(1), we're done receiving
if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
desiredValue *= 10; // shift left 1 decimal place
// convert ASCII to integer, add, and shift left 1 decimal place
desiredValue = int(((incomingByte - 48) + desiredValue));
if (desiredValue > 420)
desiredValue = 420;
Serial.println(desiredValue);
}
}
digitBuffer2[2] = int(desiredValue)/100;
digitBuffer2[1] = (int(desiredValue)%100)/10;
digitBuffer2[0] = (int(desiredValue)%10);
updateDispDesired();
Scheduler.delay(2);
yield();
}
edit by mod: please include the code using the proper tags
finaly_display_sketch.ino (5.28 KB)