Finally, Battery level is callibrated, sleep mode activated (when on power usage is around 60mA and in sleep mode around 10mA, still a lot, but I'll think about relay switch to completely isolate batteries from unit).Today I fixed Signal Bar, so this is not just picture displayed on the screen. Took whole day to figure out algorithm and redo the code to get it working!
Here is it:
Transmitter sends signal, receiver receives it, then after reading sensors, sending the row of values back to the receiver. On the receiver I've arranged 2 time measurements, one - time1 to read lost signal (packet has not being received) and time unit is up, difference will give me idea how long interruption occure, based on set timeframe boxes will appear on the signal bar, full 6 boxes and G letter, which means that the signal is strong and there were no interruption and lowest, when there are no replies for 1 second (1000 milliseconds) will indicate Lost Signal! mark next to the antenna on the screen. Well I do not know how else I can indicate signal other than that? any ideas will be highly appreciated.
Right now it looks like OK, when I go to the next room, where signal is not so strong, signal bar starts playing around, only the issue I can see is fluctuations are not smooth, but I think it's because of instant reading, if I set local delay I guess it'll get better something like this:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval){
previousMillis = currentMillis;
not to read and display signal level every loop, but maybe once in the 30sec?
Other thing is, I used nRF24L01 module, but do not really appreciate it's signal strength, it's quite short, what is available to rather increase the distance or any known replacement would be highly appreciated?
Who can suggest? I need something stronger to have control over couple kilometers around!
Other question would be, can I use theese modules to transmit clear voice and videostream?
here is the whole code of transmitter:
#include "U8glib.h"
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include "LowPower.h"
U8GLIB_ST7920_128X64 u8g(6, 5, 4, U8G_PIN_NONE);
byte vals [10];
byte sens [10];
int val1;
int val2;
int PosX;
int PosY;
int lcdbl = 9;
const int wakeUpPin = 9;
unsigned long time1;
unsigned long time2;
unsigned long delayReply;
int SignalBar;
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
u8g.setFont(u8g_font_u8glib_4);
}
void wakeUp()
{
// Just a handler for the pin interrupt.
}
// Arduino master setup
void setup(void) {
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(3); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR((byte *)"clie1");
Mirf.payload = sizeof(vals);
Mirf.config();
pinMode(lcdbl, OUTPUT);
}
void loop(void) {
u8g.firstPage();
do {
draw();
//Serial.begin(9600);
digitalWrite(lcdbl, HIGH);
val1 = analogRead(A1);
val1 = map(val1, 0, 1023, 0, 179);
vals[0] = val1;
//Serial.println(val1);
val2 = analogRead(A2);
val2 = map(val2, 0, 1023, 0, 179);
vals[1] = val2;
//Serial.println(val2);
//unsigned long time2 = 0;
Mirf.setTADDR((byte *)"serv1");
Mirf.send((byte *) &vals);
while(Mirf.isSending()){
}
if(Mirf.dataReady()){
time2 = millis();
//Serial.print("time2:");
//Serial.println(time2);
}
time1 = millis();
//Serial.print("time1:");
//Serial.println(time1);
delayReply = (time1-time2);
//Serial.print("delay:");
// Serial.println(delayReply);
Mirf.getData((byte *) &sens);
/*
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval){
previousMillis = currentMillis;
*/
//Serial.println("Waiting for Respond:");
//delayReply = (time2-time1);
//Serial.println(delayReply);
// Signal Bar
//var 1:
if (delayReply < 50 ) {
u8g.drawBox(7, 4, 2, 1);
u8g.drawBox(10, 3, 2, 2);
u8g.drawBox(13, 2, 2, 3);
u8g.drawBox(16, 1, 2, 4);
u8g.drawBox(19, 0, 2, 5);
u8g.drawStr( 22, 5, "G");
}
else if ( 50 <= delayReply && delayReply < 100 ) {
u8g.drawBox(7, 4, 2, 1);
u8g.drawBox(10, 3, 2, 2);
u8g.drawBox(13, 2, 2, 3);
u8g.drawBox(16, 1, 2, 4);
u8g.drawBox(19, 0, 2, 5);
}
else if ( 100 <= delayReply && delayReply < 200 ) {
u8g.drawBox(7, 4, 2, 1);
u8g.drawBox(10, 3, 2, 2);
u8g.drawBox(13, 2, 2, 3);
u8g.drawBox(16, 1, 2, 4);
}
else if ( 200 <= delayReply && delayReply < 400 ) {
u8g.drawBox(7, 4, 2, 1);
u8g.drawBox(10, 3, 2, 2);
u8g.drawBox(13, 2, 2, 3);
}
else if ( 400 <= delayReply && delayReply < 600 ) {
u8g.drawBox(7, 4, 2, 1);
u8g.drawBox(10, 3, 2, 2);
}
else if ( 600 <= delayReply && delayReply < 800 ){
u8g.drawBox(7, 4, 2, 1);
}
if (delayReply >= 800 ){
u8g.drawStr( 8, 4, "lost sig!");
}
u8g.drawStr( 45, 4, "My Joy_V1.0");
u8g.drawStr( 24, 12, "JoyStick Pos");
u8g.drawStr( 90, 12, "Spider");
u8g.drawLine(3, 0, 3, 4); //Anntena
u8g.drawLine(2, 2, 0, 0);
u8g.drawLine(4, 2, 6, 0);
//u8g.drawFrame(13, 6, 115, 58); //Menu Frame
//u8g.drawLine(76, 6, 76, 63);
u8g.drawStr( 69, 42, "x");
u8g.drawLine (15, 35, 74, 35); // X axel
u8g.drawLine (16, 36, 18, 37);
u8g.drawLine (16, 34, 18, 33);
u8g.drawLine (73, 36, 71, 37);
u8g.drawLine (73, 34, 71, 33);
u8g.drawStr( 49, 18, "y");
u8g.drawLine (45, 13, 45, 61); //Y axel
u8g.drawLine (46, 14, 47, 16);
u8g.drawLine (44, 14, 43, 16);
u8g.drawLine (46, 60, 47, 58);
u8g.drawLine (44, 60, 43, 58);
//Serial.begin (9600); // Servo Position on coordinates
PosX = analogRead(A1);
PosX = map(PosX, 0, 1023, 0, 35);
//Serial.println(PosX);
PosY = analogRead(A2);
PosY = map(PosY, 0, 1023, 0, 35);
//Serial.println(PosY);
u8g.drawFrame(PosX+28, PosY+18, 3, 3);
u8g.setPrintPos(80,23);
u8g.print("H Servo:");
u8g.setPrintPos(113,23);
u8g.print(sens[0]);
u8g.setPrintPos(80,28);
u8g.print("V Servo:");
u8g.setPrintPos(113,28);
u8g.print(sens[1]);
u8g.setPrintPos(80,33); // Humidity data
u8g.print("Hum(%): ");
u8g.setPrintPos(113,33);
u8g.print((float)sens[2]);
u8g.setPrintPos(80,38);
u8g.print("Temp C*: "); //Temperature Sensor's readings
u8g.setPrintPos(113,38);
u8g.print(sens[3]);
u8g.setPrintPos(80,43);
u8g.print("Voltage: ");
u8g.setPrintPos(113,43);
float voltage = analogRead(A4); //voltmeter
//Serial.begin(9600);
voltage = map(voltage,0,1023,0,1008);
//Serial.println(voltage);
u8g.print(voltage/100);
u8g.setPrintPos(113,48); //Arduino Voltage
u8g.print( (float) readVcc()/1000);
int battery = voltage;
u8g.drawLine(100, 0, 125, 0); // Battery Bar
u8g.drawLine(100, 5, 125, 5);
u8g.drawLine(100, 0, 100, 5);
u8g.drawLine(125, 0, 125, 1);
u8g.drawLine(125, 1, 127, 1);
u8g.drawLine(127, 1, 127, 4);
u8g.drawLine(127, 4, 125, 4);
u8g.drawLine(125, 4, 125, 5);
battery = map( battery, 670, 820, 0, 27);
u8g.drawBox(101, 1, battery, 4);
if( voltage <660)
{
pinMode(wakeUpPin, INPUT);
u8g.drawFrame(0, 0, 128, 64);
u8g.setColorIndex(3);
u8g.setPrintPos(37,35);
u8g.print("LOW BATTERY !!!");
delay(5000);
digitalWrite(lcdbl, LOW);
delay(5000);
attachInterrupt(0, wakeUp, LOW);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
detachInterrupt(0);
digitalWrite(lcdbl, HIGH);
}
//Serial.begin(9600);
//Serial.println("Start Receiving Data from Spider:");
//Serial.println(sens[2]);
//Serial.println(sens[3]);
//Serial.println(sens[4]);
//Serial.println(sens[5]);
//Serial.println(sens[6]);
//Serial.println(sens[7]);
delay(40);
} while( u8g.nextPage() );
// rebuild the picture after some delay
}